By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,153 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,153 IT Pros & Developers. It's quick & easy.

How can I generate a GUID?

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
George wrote:
I want to create a unique id for each of a set of objects.


Dim strGUID As String
strGUID = System.Guid.NewGuid().ToString
Nov 21 '05 #2

P: n/a
That gives me an empty GUID (all zeros) not a newly generated unique
identifier.

Daniel.

"Leon Mayne [MVP]" <l.*********@uea.ac.uk> wrote in message
news:uV****************@TK2MSFTNGP10.phx.gbl...
George wrote:
I want to create a unique id for each of a set of objects.


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

Nov 21 '05 #3

P: n/a
George wrote:
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/247referenc...aspx?id=189797
Nov 21 '05 #4

P: n/a
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

"Leon Mayne [MVP]" <l.*********@uea.ac.uk> wrote in message
news:uY**************@tk2msftngp13.phx.gbl...
George wrote:
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/247referenc...aspx?id=189797

Nov 21 '05 #5

P: n/a
George wrote:
Thanks for your help,

George


No Prob :-)
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.