Comparing GUIDs

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hi -

I'm experiencing a strange problem when comparing 2 guids. In my trial,
they're not equal. When I step through the (VB.NET) code, they are
evaluated as equal, and when I enter the comparison in the command window,
they're not equal. I'm pretty stumped on this one. Please help.

I've tried the following structures, all with the same result:

guid1.equals(guid2)
guid1.tostring.equals(guid2.tostring)
guid1.tostring = guid2.tostring

The 2 guids are:

guid1 = 2a14ce02-20c1-11c4-8000-8ea6b5cb2452
guid2 = 007900d7-009c-00e1-f100-7900fc002900

(obviously not the same)

Those are the values reported when I type ?guid1 and ?guid2 into the command
window. When I enter any of the above structures (e.g.,
?guid1.equals(guid2)) into the command window, False is correctly returned.

But in my code, I have the statement:

if guid1.equals(guid2) then X

and X is executed!

What could be wrong here??

Thanks for any help.

- Jeff
 
Jeff said:
Hi -

I'm experiencing a strange problem when comparing 2 guids. In my
trial, they're not equal. When I step through the (VB.NET) code,
they are evaluated as equal, and when I enter the comparison in the
command window, they're not equal. I'm pretty stumped on this one.
Please help.

I've tried the following structures, all with the same result:

guid1.equals(guid2)
guid1.tostring.equals(guid2.tostring)
guid1.tostring = guid2.tostring

The 2 guids are:

guid1 = 2a14ce02-20c1-11c4-8000-8ea6b5cb2452
guid2 = 007900d7-009c-00e1-f100-7900fc002900

(obviously not the same)

Those are the values reported when I type ?guid1 and ?guid2 into the
command window. When I enter any of the above structures (e.g.,
?guid1.equals(guid2)) into the command window, False is correctly
returned.

But in my code, I have the statement:

if guid1.equals(guid2) then X

and X is executed!

What could be wrong here??


Are you sure it is executed? I don't think so.

If you can't step into X, try this:

if guid1.equals(guid2) then msgbox "equal"

I guess you don't see a msgbox.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Thanks, Armin -

You're right! But there's still something "interesting" happening:

In my code, I have 2 lines:

if guid1 is system.dbnull.value then Y
if guid1.equals(guid2) then X

Both conditions evaluate as false. If I step through the code, Y is not
highlighted, but X is (even though it doesn't execute).

This is certainly no longer a showstopper for me (thanks again). But I'm
wondering why debugging behaves as above.

- Jeff
 
Jeff said:
You're right! But there's still something "interesting"
happening:

In my code, I have 2 lines:

if guid1 is system.dbnull.value then Y
if guid1.equals(guid2) then X

Both conditions evaluate as false. If I step through the code, Y is
not highlighted, but X is (even though it doesn't execute).


This code can not even be compiled. The Is operator must not be used with
value types, but GUID is a value type. How did you declare guid1 and guid2?
I declared them as GUID.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
IF guid1.GetHashCode=guid2.GetHashCode THEN ....
I've never had any problem with using this code.

That doesn't mean it's correct. A GUID is 128 bits of data, a hash
code only 32. So many many different GUIDs could return the same value
in theory.

Since there is an Equals method, I don't see why you would use
anything else.



Mattias
 
Well, Guid2 is declared as a guid.

Guid1 is a column in a data row. The data table was created from an xml
file, using dataset.readxml (with xmlReadMode.ReadSchema).

In the command window, ?typename(datarow("Guid1")) returns Guid.

So, to clarify, that first if statement really is:

if datarow("Guid1") is system.dbnull.value then Y

- Jeff
 
Jeff said:
Well, Guid2 is declared as a guid.

Guid1 is a column in a data row. The data table was created from an
xml file, using dataset.readxml (with xmlReadMode.ReadSchema).

In the command window, ?typename(datarow("Guid1")) returns Guid.

So, to clarify, that first if statement really is:

if datarow("Guid1") is system.dbnull.value then Y

That makes the difference...

To reproduce it, I did this:

Dim guid1 As Object, guid2 As Guid
guid1 = Guid.NewGuid
guid2 = Guid.NewGuid
If guid1 Is System.DBNull.Value Then MsgBox("")
If guid1.Equals(guid2) Then MsgBox("")

In both cases, Msgbox is not hiligthed and of course also not executed, so I
can not reproduce it.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Thanks for trying, Armin -

I've got it working, though I still have the unexplained highlighting
question. (I can live with it as it is.)

- Jeff
 
Jeff,
Are you on VB.NET 2002 or VB.NET 2003?

This sounds like the debugger problem in VS.NET 2002 that would highlight
lines of code, but not actually execute them.

Hope this helps
Jay
 
Hi Jeff,

I can not reproduce the problem on both VS.NET 2002 and VS.NET 2003.
Here is my test code.
Dim guid1 As Object, guid2 As Guid
guid1 = Guid.NewGuid
guid2 = Guid.NewGuid
If guid1 Is System.DBNull.Value Then Debug.WriteLine("")
If guid1.Equals(guid2) Then Debug.WriteLine("")

Can you reproduce the problem on your side with the code above?
If no, can you build a simple reproduce sample and post here?

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks for trying, Peter. I haven't had a chance to try your test code. As I reported earlier, I now have GUID comparison working

- Jeff
 
Back
Top