tags:

views:

123

answers:

3

When I use MyGuid.ToString().Equals(OtherGuid.ToString())

they are Equal, why are they not equal when I compare the pure Guid ?

Update:

Well the problem here could be that I use a 3rd party control.

The .Key below has a Guid and committeeId is a Guid too. They were never Equal only when I did

ToString() on both Guid`s they were equal thats odd.

 for (int i = 0; i < this.ultraCalendarInfo.Owners.Count; i++) 
                if (ultraCalendarInfo.Owners[i].Key.ToString().Equals(committeeId))
                    ultraCalendarInfo.Owners[i].Visible = isVisible; 
+4  A: 

I can't reproduce the problem:

using System;

class Program
{
    static void Main(string[] args)
    {
        Guid x = Guid.NewGuid();
        Guid y = new Guid(x.ToString());

        Console.WriteLine(x == y);
        Console.WriteLine(x.Equals(y));
        Console.WriteLine(x.ToString() == y.ToString());
    }
}

Produces:

True
True
True

Please give a similar short but complete program which demonstrates the problem.

EDIT: I think I see the problem now, and it's in your code:

if (ultraCalendarInfo.Owners[i].Key.ToString().Equals(committeeId))

You've stated:

The .Key below has a Guid and committeeId is a Guid too.

You're calling ToString() on the Guid but not on committeeId, so that condition will never be true. If you called ToString() on both or neither it should be fine.

I strongly suspect that this (or something very similar, if the above isn't your real code) is the problem. Calling ToString() more than once (i.e. guid.ToString().ToString() etc) will always return the same string, of course - so if you have an unbalanced number of ToString() calls (0 on one side and 1 on the other) then adding an extra call to both sides would "fix" the problem... but removing one of the ToString() calls is the real fix.

Jon Skeet
I have updated my init post with the code it happened
Panella
@Panella: Updated my answer to point out your bug.
Jon Skeet
@Johnerhmmm... sorry I did ToString() on committeeId at the invisible line of code at top... I have not posted :P
Panella
@Panella: Please update your post with a *complete* program which actually demonstrates the problem. I've given an example which shows it all working - you should try to provide a similar, stand-alone example which we can run to see what's going on. Just snippets - especially inaccurate ones - aren't going to help much.
Jon Skeet
+2  A: 

Consider the code below

object g1 = Guid.NewGuid();
object g2 = new Guid(((Guid)g1).ToByteArray());
Console.WriteLine("{0}\r\n{1}", g1, g2);
Console.WriteLine("   Equals: {0}", g1.Equals(g2));
Console.WriteLine("Object ==: {0}", g1 == g2);
Console.WriteLine(" Value ==: {0}", (Guid)g1 == (Guid)g2);

Storing a GUID in a variable of type "object" has the effect of "boxing" it into a reference type. When you use "==" to compare reference types, they may not be equal even if the values contained in them are equal. This is a difference from value types because if you were to declare g1 and g2 above as Guid, then you would get True for all the equality tests. But the above code returns False for the "==" test. Notice that if you "un-box" the values as shown in the third test, it will see that the values are equal. It will also see that they are equal if you use the "Equals" method, which can be (and is) overridden to perform more intelligent comparisons of objects based on their specific types.

BlueMonkMN
A: 

As you don't show the declaration of the two guids, the only thing I can think of is that they are different guid types (I'm using SqlGuid as an example here):

 Sub Main()

        Dim stringValue = Guid.NewGuid().ToString()

        Dim first = New Guid(stringValue)
        Dim second = New SqlTypes.SqlGuid(stringValue)

        Console.WriteLine(first = second)    'True'
        Console.WriteLine(first.Equals(second))    'False'
        Console.WriteLine(first.ToString() = second.ToString())    'True'
        Console.ReadKey()

    End Sub
Pondidum