Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VBNET
Expertise: Intermediate
Apr 4, 2002

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.ToByteArray
Dim b As Byte
For Each b In bytes
    Console.Write(b.ToString & " ")
        ' => 239 1 161 57 143 200 172 70 185 64 222 29 59 15 190 205
Next

' Compare two GUIDs.
If Not guid1.Equals(guid2) Then
    Console.WriteLine("GUIDs are different.")
End If
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

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