How to convert a string to a GUID

W

Wolf

Hi

I am trying to set a property(PartyHomeAddressID) = to a guid in a ini
file.
But everytime when the ini file has an empty guid it breaks with an
error tellin me a guid is 32 char long with 4 dashes.


client.PartyHomeAddressID = new Guid(contact.Substring(1542,
36).Trim());

If its not Empty GUID, it doesnt break.

Is the reason maybe because I am specifying new Guid( around a empty
guid?
If this is the case, how do I convert it to read a GUID

Thanks a million in advance
 
J

Jon Skeet [C# MVP]

Wolf said:
I am trying to set a property(PartyHomeAddressID) = to a guid in a ini
file.
But everytime when the ini file has an empty guid it breaks with an
error tellin me a guid is 32 char long with 4 dashes.

client.PartyHomeAddressID = new Guid(contact.Substring(1542,
36).Trim());

If its not Empty GUID, it doesnt break.

Is the reason maybe because I am specifying new Guid( around a empty
guid?
If this is the case, how do I convert it to read a GUID

The constructor Guid(string) assumes that you're passing it a valid
string representation of a Guid. If you're not, it's (not unreasonably)
complaining.

What do you *want* it to do if your ini file has an empty guid? Should
it create a new Guid, should it use an "all zero" Guid? Your code needs
to decide this - check whether or not a guid has been supplied in the
ini file, use it if it has, and take appropriate action if not.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top