Convert String To Guid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does somebody know if there's a faster way to convert a string to a guid, than:

Try
gID = New Guid(myString)
Catch ex As Exception
gID = Guid.Empty
End Try

even if it's not clear that the string contains a valid guid?

tia
Martin
 
Exception handling is expensive.... and this if this is not "exceptional",
as in....only happens in a blue moon, Id avoid the try/catch

From
http://geekswithblogs.net/jawad/archive/2005/05/20/GuidVerifier.aspx

private static Regex isGuid = new
Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F
]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);

internal static bool IsGuid(string
candidate, out Guid output)

{

bool isValid = false;

output=Guid.Empty;

if(candidate!=null)

{



if
(isGuid.IsMatch(candidate))

{


output=new Guid(candidate);


isValid = true;

}

}

return isValid;

}



rewrite that as your procedure. it avoids the try/catch, and gets the
purpose accomplished.
 
Hi,

I've already tried to use RegularExpressions. RegEx is faster if the string
doesn't contain a valid guid, but it's slower if the string contains a valid
guid.


sloan said:
Exception handling is expensive.... and this if this is not "exceptional",
as in....only happens in a blue moon, Id avoid the try/catch

From
http://geekswithblogs.net/jawad/archive/2005/05/20/GuidVerifier.aspx

private static Regex isGuid = new
Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F
]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);

internal static bool IsGuid(string
candidate, out Guid output)

{

bool isValid = false;

output=Guid.Empty;

if(candidate!=null)

{



if
(isGuid.IsMatch(candidate))

{


output=new Guid(candidate);


isValid = true;

}

}

return isValid;

}



rewrite that as your procedure. it avoids the try/catch, and gets the
purpose accomplished.



Martin Moser said:
Does somebody know if there's a faster way to convert a string to a guid, than:

Try
gID = New Guid(myString)
Catch ex As Exception
gID = Guid.Empty
End Try

even if it's not clear that the string contains a valid guid?

tia
Martin
 
You have three options:

1. Use Guid(string) constructor
2. Use RegEx
3. Use custom string parser (sequential and not RegEx based)

You are not happy with (1) and (2).
(3) is the only option left... but that may end up being a copy of (1)

However, if the data that you have is in some specific format, say, '{' is
always present or '-' is always absent, your (3) may be faster than (1)
(1) looks for all possibilities, presence of '{', presence of '-' etc making
it slow.

No other option comes to my mind than the above.

--
Cheers,
Gaurav Vaish
http://www.mastergaurav.org
http://www.edujini.in
-------------------


Martin Moser said:
Hi,

I've already tried to use RegularExpressions. RegEx is faster if the
string
doesn't contain a valid guid, but it's slower if the string contains a
valid
guid.


sloan said:
Exception handling is expensive.... and this if this is not
"exceptional",
as in....only happens in a blue moon, Id avoid the try/catch

From
http://geekswithblogs.net/jawad/archive/2005/05/20/GuidVerifier.aspx

private static Regex isGuid = new
Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F
]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);

internal static bool IsGuid(string
candidate, out Guid output)

{

bool isValid = false;

output=Guid.Empty;

if(candidate!=null)

{



if
(isGuid.IsMatch(candidate))

{


output=new Guid(candidate);


isValid = true;

}

}

return isValid;

}



rewrite that as your procedure. it avoids the try/catch, and gets the
purpose accomplished.



Martin Moser said:
Does somebody know if there's a faster way to convert a string to a
guid, than:

Try
gID = New Guid(myString)
Catch ex As Exception
gID = Guid.Empty
End Try

even if it's not clear that the string contains a valid guid?

tia
Martin
 
3. Use custom string parser (sequential and not RegEx based)

For this option, you could always use a freely available tool (eg.
Salamander, dotnet Reflector), to see exactly what "public Guid(string str)"
does. This would ensure that your custom code worked exactly like the
framework's code, except you'd change your version so that it would not
raise exceptions and return false instead.

Stephen
 
Back
Top