Create a unique GUID

Create a unique GUID

The System.Guid type exposes several shared and instance methods that can help you work with GUIDs, that is, those 128-bit numbers that serve to uniquely identify elements and that are ubiquitous in Windows programming. The most important member is the NewGuid shared method is useful for generating a new unique identifier:

' Create a new GUID.Dim guid1 As Guid = Guid.NewGuid' By definition, you'll surely get a different output here.Console.WriteLine(guid1.ToString)    '=> 3f5f1d42-2d92-474d-a2a4-1e707c7e2a37

There are two things you can do with a Guid object: you can convert it to a Byte array with the ToByteArray method, and you can compare two Guid values for equality using the Equals method (inherited from System.Object):

' Convert to an array of bytes.Dim bytes() As Byte = guid1.ToByteArrayDim b As ByteFor Each b In bytes    Console.Write(b.ToString & " ")        ' => 239 1 161 57 143 200 172 70 185 64 222 29 59 15 190 205Next' Compare two GUIDs.If Not guid1.Equals(guid2) Then    Console.WriteLine("GUIDs are different.")End If


Share the Post: