tags:

views:

388

answers:

3

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
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
@Konrad: I have no idea what the actual object is... So I used CType to handle more possibilities..
Mehrdad Afshari
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
+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