Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Generate a random GUID
 

Generate a random GUID

Hi,

I'm trying to generate a random GUID, but the method i'm using creates one full of 0's!

The code i'm trying is:

Public Shared Function NewGuid() As Guid
End
Function

Then i'm calling this in my code:

Dim guidString As String
guidString = NewGuid.ToString()

I'm using VB.NET 1.1 On the Compact Framework.

Any help would be appreciated!

Thanks,

Simon

Simon.UK  Tuesday, January 06, 2009 1:16 PM
Hi TechGnome,

You're right, less than helpful of Microsoft! On the bright side i've found a solution! There's a custom class on MSDN here:http://msdn.microsoft.com/en-us/library/aa446557.aspx#ppcguidgen_topic4

This class returns a random GUIDjust asthe NewGuid() method of Guid does in the full-blown framework. Not as elegant, but if it works...

Many thanks for your input!

Best regards,

Simon
  • Marked As Answer bySimon.UK Tuesday, January 06, 2009 3:54 PM
  •  
Simon.UK  Tuesday, January 06, 2009 3:15 PM
DimmyGuidAsGuid
DimmyGuidStrAsString
myGuid=Guid.NewGuid()
myGuidStr=myGuid.ToString

dbasnett  Tuesday, January 06, 2009 1:58 PM
Hi dbasnett,

Thanks for your response. I came across that code as well, but it doesn't work on the framework i'm using . I get the following compile-time error:

'NewGuid' is not a member of 'System.Guid'.

The only option in intellisense in the properties and methods list for Guid is Guid.Empty, which is presumably what's being created in my original code, where the Guid is full of 0's. I've been using the code i found here:http://msdn.microsoft.com/en-us/library/system.guid.newguid(VS.71).aspx

Kind regards,

Simon
Simon.UK  Tuesday, January 06, 2009 2:18 PM
I think you misinterpreted the code from MSDN there. The "code" sample that you linked to shows how THEY (MS) implemented it. What you need to do is define a variable as type GUID, then assign it a value returned from system.guid.newguid. However, the fact that you are using the CF.... throws a wrench into the mix...
According to the chart on this pagehttp://msdn.microsoft.com/en-us/library/system.guid_members(VS.71).aspx... NewGuid isn't supported by the CF. In fact it seems to be the only thing not supported by the CF. How lame is that?

-tg
Chris Anderson [VB-MVP]  Tuesday, January 06, 2009 2:50 PM
Hi TechGnome,

You're right, less than helpful of Microsoft! On the bright side i've found a solution! There's a custom class on MSDN here:http://msdn.microsoft.com/en-us/library/aa446557.aspx#ppcguidgen_topic4

This class returns a random GUIDjust asthe NewGuid() method of Guid does in the full-blown framework. Not as elegant, but if it works...

Many thanks for your input!

Best regards,

Simon
  • Marked As Answer bySimon.UK Tuesday, January 06, 2009 3:54 PM
  •  
Simon.UK  Tuesday, January 06, 2009 3:15 PM
dbasnett  Tuesday, January 06, 2009 3:20 PM
Thanks for the links dbasnett - they're now bookmarked! They'll undoubtedly come in useful in the future.

Simon
Simon.UK  Tuesday, January 06, 2009 3:29 PM

You can use google to search for other answers

Custom Search

More Threads

• How do I Permanently overwrite culture settings, not just for thread
• How to place TextBoxes on a PictureBox
• How to Override the TEXT property of a CLASS? Anyone please?
• Delete text from a cell/row in a spreadsheet using VB Script
• FTP Download File
• Reverse Engineering
• Number in textboxes
• embeding wmv as a resource
• NullReferencieException in Strings Question
• Accessing the Default Cursor