5

how can I convert an object type to a GUID type in VB.NET?

jerbersoft
  • 4,344
  • 9
  • 42
  • 48

3 Answers3

10

I'm not sure what exactly you want but this might help:

Dim g = CType(obj, System.Guid)

If you want to convert a string to a Guid:

Dim g = New Guid(myString)
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 4
    You should use `DirectCast` instead of `CType` here (or anywhere for unboxing). Here's why: http://stackoverflow.com/questions/102084/hidden-features-of-vbnet#103285 – Konrad Rudolph May 12 '09 at 11:06
1

If you are looking to create the object as a new guid, use the following call:

dim objvar as guid = System.GUID.NewGuid()

edit Your question is a little unclear when you say "convert". If you already have the object created and assigned, use DirectCast to create an object that the Visual Studio environment will recognize.

-3

Mehrdad's sample will work, however it is always best to declare the data type for all your variables:

Dim g As Guid = objectVariable

In this case there is no need to use CType or DirectCast.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89