dcsimg

Making a GUID Creator in .NET

WEBINAR:
On-Demand

Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


Introduction

Welcome to my article. Knowledge is relative. Education is relative. Experience is relative. It all boils down to what you can remember and how fast you can remember stuff. As you get older, you tend to forget little things like telephone numbers and passwords and so on—not to mention GUIDs or long serial numbers! Because it is difficult to remember GUIDs and to generate them each time, this program has been born. Today, you will learn what GUIDs are, how to randomly generate GUIDs, and how to copy them.

GUID

A GUID (Globally Unique IDentifier) is a unique 128-bit number used to identify information in computer systems.

Practical

The aim of your project today is to generate a few unique GUIDs in various formats. Let's start!

Create a new Visual Basic or C# Windows Forms project. After the project has been created and the Form displayed, design your Form to resemble Figure 1.

Design
Figure 1: Design

You may name your objects anything you want, but keep in mind that my naming might be different than yours. Add the following strings to the Type Combobox:

  • No Braces
  • Square Brackets
  • Braces
  • Parentheses
  • Hex

The Combobox will show the different styling types for the GUIDs that will be output. The Upper Case and Lower-case radio buttons determine the case of the GUIDs and the ListBox will display the generated GUIDs.

Add the following Enumerations and fields.

C#

   private List<string> GUIDs;
   private enum GUIDTypes {

      NoBraces,
      Brackets,
      Braces,
      Parentheses,
      Hexidecimal

   }
   private enum GUIDCase {

      Upper,
      Lower
   }

   GUIDCase gcase;

VB.NET

   Private GUIDs As List(Of String)

   Private Enum GUIDTypes

      NoBraces
      Brackets
      Braces
      Parentheses
      Hexidecimal

   End Enum

   Private Enum GUIDCase

      Upper
      Lower

   End Enum

   Private gcase As GUIDCase

GUIDs is a List of Strings. The generated GUIDs will be added to it, and it will be added to the ListBox. GUIDTypes is an enumeration holding the types we added into the ComboBox. GUIDCase holds the values Upper and Lower case. Gcase is a GUIDCase object that can be either Upper or Lower.

Add the Form_Load event.

C#

   private void Form1_Load(object sender, EventArgs e)
   {

      GUIDs = new List<string>();

   }

VB.NET

   Private Sub Form1_Load(ByVal sender As Object, _
         ByVal e As EventArgs) Handles Me.Load

      GUIDs = New List(Of String)()

   End Sub

In the Form Load event, you instantiate the GUIDs object. Add the Create Function.

C#

   private List<string> Create(int iNum, GUIDCase cCase,
      GUIDTypes tTypes = GUIDTypes.NoBraces)
   {

      List<string> lstTemp = new List<string>();

      string strGUID = string.Empty;
      string strNewGUID = string.Empty;

      for (int i = 0; i < iNum; i++)
      {
         strGUID = Guid.NewGuid().ToString();

         switch (tTypes)
         {
            case GUIDTypes.NoBraces:

               strNewGUID = strGUID;
               break;

            case GUIDTypes.Brackets:

               strNewGUID = "[" + strGUID + "]";
               break;

            case GUIDTypes.Braces:

               strNewGUID = "{" + strGUID + "}";
               break;

            case GUIDTypes.Parentheses:

               strNewGUID = "(" + strGUID + ")";
               break;

            case GUIDTypes.Hexidecimal:

               strNewGUID = strGUID.Replace("-", string.Empty);
               break;
         }

         if (cCase == GUIDCase.Upper)
         {

            strNewGUID = strNewGUID.ToUpper();

         }

         if (cCase == GUIDCase.Lower)
         {

            strNewGUID = strNewGUID.ToLower();

         }

         lstTemp.Add(strNewGUID);

      }

      strGUID = string.Empty;

      return lstTemp;

   }

VB.NET

   Private Function Create(ByVal iNum As Integer, _
         ByVal cCase As GUIDCase, ByVal Optional tTypes As _
         GUIDTypes = GUIDTypes.NoBraces) As List(Of String)

      Dim lstTemp As List(Of String) = New List(Of String)()
      Dim strGUID As String = String.Empty
      Dim strNewGUID As String = String.Empty

      For i As Integer = 0 To iNum - 1

         strGUID = Guid.NewGuid().ToString()

         Select Case tTypes
            Case GUIDTypes.NoBraces

               strNewGUID = strGUID

            Case GUIDTypes.Brackets

               strNewGUID = "[" & strGUID & "]"

            Case GUIDTypes.Braces

               strNewGUID = "{" & strGUID & "}"

            Case GUIDTypes.Parentheses

               strNewGUID = "(" & strGUID & ")"

            Case GUIDTypes.Hexidecimal

               strNewGUID = strGUID.Replace("-", String.Empty)

         End Select

         If cCase = GUIDCase.Upper Then

            strNewGUID = strNewGUID.ToUpper()

         End If

         If cCase = GUIDCase.Lower Then

            strNewGUID = strNewGUID.ToLower()

         End If

         lstTemp.Add(strNewGUID)

      Next

      strGUID = String.Empty
      Return lstTemp

   End Function

The Create function generates a new GUID by using the Guid.NewGuid method. After the GUID is created, some manipulation is done to format the output according to the Type and Case that was selected and returns the finished manipulated GUID string. Add the code for the button labeled 'Generate.'

C#

   private void btnGenerate_Click(object sender, EventArgs e)
   {

      GUIDs = Create(20, gcase, (GUIDTypes)cbGUIDType
         .SelectedIndex);

      lstGUID.DataSource = GUIDs;

   }

VB.NET

   Private Sub btnGenerate_Click(ByVal sender As Object, _
         ByVal e As EventArgs) Handles btnGenerate.Click

      GUIDs = Create(20, gcase, CType(cbGUIDType.SelectedIndex, _
         GUIDTypes))
      lstGUID.DataSource = GUIDs

   End Sub

This calls the Create method to generate the GUIDs. Add the code for when an item has been selected from the Combobox.

C#

   private void cbGUIDType_SelectedIndexChanged(object sender,
      EventArgs e)
   {

      if (lstGUID.Items.Count > 0)
      {

         btnGenerate_Click(sender, e);

      }

   }

VB.NET

   Private Sub cbGUIDType_SelectedIndexChanged(ByVal sender _
         As Object, ByVal e As EventArgs) Handles _
         cbGUIDType.SelectedIndexChanged

      If lstGUID.Items.Count > 0 Then

         btnGenerate_Click(sender, e)

      End If

   End Sub

If there are no items shown inside the Listbox, the program generates new GUIDs. Add the code for the Radio buttons.

C#

   private void rdUpper_CheckedChanged(object sender, EventArgs e)
   {

      gcase = GUIDCase.Upper;

      if (GUIDs.Count > 0)
      {

         for (int i = 0; i < GUIDs.Count; i++)
         {

            GUIDs[i] = GUIDs[i].ToUpper();

         }

         lstGUID.DataSource = null;
         lstGUID.DataSource = GUIDs;

      }
   }

   private void rdLower_CheckedChanged(object sender, EventArgs e)
   {
      gcase = GUIDCase.Lower;

      if (GUIDs.Count > 0)
      {

         for (int i = 0; i < GUIDs.Count; i++)
         {

            GUIDs[i] = GUIDs[i].ToLower();

         }

         lstGUID.DataSource = null;
         lstGUID.DataSource = GUIDs;

      }
   }

VB.NET

   Private Sub rdUpper_CheckedChanged(ByVal sender As Object, _
         ByVal e As EventArgs) Handles rdUpper.CheckedChanged

      gcase = GUIDCase.Upper

      If GUIDs.Count > 0 Then

         For i As Integer = 0 To GUIDs.Count - 1

            GUIDs(i) = GUIDs(i).ToUpper()

         Next

         lstGUID.DataSource = Nothing
         lstGUID.DataSource = GUIDs

      End If

   End Sub

   Private Sub rdLower_CheckedChanged(ByVal sender As Object, _
         ByVal e As EventArgs) Handles rdLower.CheckedChanged

      gcase = GUIDCase.Lower

      If GUIDs.Count > 0 Then

         For i As Integer = 0 To GUIDs.Count - 1

            GUIDs(i) = GUIDs(i).ToLower()

         Next

         lstGUID.DataSource = Nothing
         lstGUID.DataSource = GUIDs

      End If

   End Sub

Depending on which Radio button was selected, the code loops through the GUID list and changes each item's case; it then displays them inside the ListBox. Add the code to clear the ListBox.

C#

   private void btnClear_Click(object sender, EventArgs e)
   {

      GUIDs.Clear();
      lstGUID.DataSource = null;

   }

VB.NET

   Private Sub btnClear_Click(ByVal sender As Object, _
         ByVal e As EventArgs) Handles btnClear.Click

      GUIDs.Clear()
      lstGUID.DataSource = Nothing

   End Sub

The above clears the GUIDs list and the ListBox.

Figures 2-6 show the application during Run time with different options set.

Lower case Hexadecimal
Figure 2: Lower case Hexadecimal

No Braces
Figure 3: No Braces

Parentheses
Figure 4: Parentheses

Square Brackets
Figure 5: Square Brackets

Upper case Hexadecimal Conclusion
Figure 6: Upper case Hexadecimal

Conclusion

Generating random GUIDs can come in quite handy for when definite unique values are needed.



About the Author

Hannes DuPreez

Hannes du Preez is an ex MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic and C#. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date