How can I generate a GUID?

  • Thread starter Thread starter George
  • Start date Start date
G

George

I want to create a unique id for each of a set of objects.

These ids may be generated on multiple machines simultaneously and must all
be different to each other.

There are an undefined number of machines involved (1 to dozens) and an
indeterminate number of objects (many to very many on each machine).

My best guess for a solution would be to use a GUID (all the machines have
an ethernet connection so a GUID generated on any of them should not
conflict with any other).

The question is how do I generate a GUID from VB.NET?

I don't really want to use unmanaged code if I can avoid it but I can write
some sort of managed wrapper for it if I have to.

Thanks, in advance,

George.
 
That gives me an empty GUID (all zeros) not a newly generated unique
identifier.

Daniel.
 
George said:
That gives me an empty GUID (all zeros) not a newly generated unique
identifier.

Are you sure you're using Guid.NewGuid() and not just Guid? Apparently just
Guid.ToString returns all zeros.

Copy and paste this code and run your app:

Dim strGUID As String
strGUID = System.Guid.NewGuid().ToString
msgbox (strGUID)

And see if it still brings up a messagebox full of zeros. For me, this
returns a proper GUID.

Also see this:
http://www.dotnet247.com/247reference/message.aspx?id=189797
 
Aha, yes you are right. NewGuid does generate a new guid. That is what I
expected it to do but ...

My problem it seems was in getting the value of this new guid.

I have the following:-

Private mGUID as Guid

Public Sub New()
Me.New(Guid.NewGuid)
End Sub

Private Sub New(ByVal pGUID as Guid)
MyBase.New()
mGUID = pGUID
End Sub

I put a break point on the line after setting mGUID and investigated the
results using the command window:-

?mGUID
{System.Guid}
Empty: {System.Guid}
?mGUID.ToString
"188372b2-b106-40c6-89d8-6005f20369f5"

Thanks for your help,

George
 
Back
Top