Search code examples
c#vb.netguidact

Creating a Guid -> conversion from VB to C#


I have the following code in VB.Net and I'm trying to convert it to C#.

 listContacts = ACT_FRAMEWORK.Contacts.GetContactsByID(Nothing, New Guid() {New Guid(ContactID)})

Below is my attempt so far:

 Guid[] test = new Guid[1];
 test[0] = Guid.NewGuid(ContactID);   
 contactList = actApp.Contacts.GetContactsByID(null, test);

The abover errors because NewGuid() takes no arguments. I have also tried.

test[0] = contactID1;

However, you can't convert from string to Guid. Can anyone help with this?


Solution

  • Guid.NewGuid is a method, and you're not calling that method from your VB code - you're calling the constructor accepting a string. You can call that same constructor in C#:

    // Using an array initializer for simplicity
    Guid[] test = { new Guid(ContactID) };
    

    Or if you want to make the call in one line as per your VB code:

    contactList = actApp.Contacts.GetContactsByID(null, new[] { new Guid(ContactID) });
    

    I think it's a shame that NewGuid has that name rather than CreateGuid, but such is life :(

    Using Guid.Parse will work too of course - judging by the .NET Core source code they behave extremely similarly; the constructor just handles overflow exceptions slightly differently.