Hi guys... how can I convert an object type to a GUID type in VB.NET?
+1
A:
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
2009-05-12 11:02:12
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
2009-05-12 11:06:14
@Konrad: I have no idea what the actual object is... So I used CType to handle more possibilities..
Mehrdad Afshari
2009-05-12 11:08:38
A:
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.
Boo
2009-05-12 20:26:49
+1
A:
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.
FaultyLogic
2009-05-13 16:14:22