[Mono-list] Mono/.Net: object comparison

Martin Hinks mhinks at gmail.com
Thu Jun 30 15:03:19 EDT 2005


Yeah, kinda remembered that after I went and opened my mouth!

Thanks for the re-clarification Nick.

On 6/30/05, Nick Berardi <nberardi at zigamorph.com> wrote:
> Well your logic all depends on if you have not changed the behavior of how
> the class compares it self.  You can do this by overloading the '==' and
> '!=' sign or even better you can overload the GetHash() method and the
> Equals() method.  If you want to be able to compare these two this is the
> best option for you.
> 
> Nick
> 
> -----Original Message-----
> From: mono-list-bounces at lists.ximian.com
> [mailto:mono-list-bounces at lists.ximian.com] On Behalf Of Martin Hinks
> Sent: Thursday, June 30, 2005 5:13 AM
> To: Julien Sobrier
> Cc: mono-list at lists.ximian.com
> Subject: Re: [Mono-list] Mono/.Net: object comparison
> 
> Sounds like a classic referencing problem, I would think logically
> that myArray[0] different from foo is the correct behaviour...
> 
> MyClass test = new MyClass ("1","2","3");
> MyClass test2 = new MyClass ("1","2","3");
> 
> test != test2
> 
> They have different references although the values are the same...
> 
> I wouldn't therefore be able to do test.String1 = "new" and expect the
> value of test2.String1 to change. They are different objects...
> 
> So, the answer is that Mono seems to be comparing on memory reference
> whilst .NET does the comparison on value. Guess a bugzilla report is
> needed, I can file it if you want or you can as you have the working
> test case...
> 
> 
> 
> On 6/24/05, Julien Sobrier <julien at sobrier.net> wrote:
> > Hello,
> > I've compiled a program with Mono 1.1.6 under Linux, and Windows .Net,
> > with NAnt 0.85RC3 and NUnit 2.2.0
> >
> > I see a different behavior when I compare object with NUnit:
> >
> > I have a custom class MyClass with 3 public variables: String1 (string),
> > String2 (string) and Version1 (Version).
> >
> > In one class, I do like:
> > ArrayList myArray = new ArrayList();
> > myArray.Add(new MyClass(string1, string2, version1);
> > myArray.Add(new MyClass(string1a, string2a, version1a);
> > myArray.Add(new MyClass(string1b, string2b, version1b);
> >
> > Then I create MyClass foo = new MyClass(string1, string2, version1)
> >
> > Then, with Nunit, I do some comparisons (note sure about the index for
> > myArray):
> > * Mono 1.1.6/Linux
> > myArray[0] different from foo
> > (MyClass) MyArray[0] different from foo
> > ((MyClass) MyArray[0]).String1 =  foo.String1
> > ((MyClass) MyArray[0]).String2 =  foo.String2
> > ((MyClass) MyArray[0]).Version1 =  foo.Version1
> >
> > * .Net/Windows
> > myArray[0] = foo
> > (MyClass) MyArray[0] = foo
> > ((MyClass) MyArray[0]).String1 =  foo.String1
> > ((MyClass) MyArray[0]).String2 =  foo.String2
> > ((MyClass) MyArray[0]).Version1 =  foo.Version1
> >
> >
> > Who is right? I hope .Net is right :-) I wonder what cause this
> > different behavior
> >
> >
> > Details of the code:
> > [Test]
> > [Category("attributes")]
> > public void AssemblyDependTest()
> > {
> >     Assert.AreEqual(this.PluginA.Dependencies.Count, 6,
> > "AssemblyDepend: number of dependencies");
> >
> >     AssemblyDepend test = new
> > Platine.CoreLibs.Attributes.AssemblyDepend("PluginB");
> >
> >     Assert.AreEqual(test.PluginName, "PluginB", "AssemblyDepend:
> > PluginA depends on PluginB"); // OK
> >     Assert.AreEqual(test.MinVersion.ToString(), "0.0.0.0",
> > "AssemblyDepend: PluginA depends on PluginB"); // OK
> >     Assert.AreEqual(test.EventName, "*", "AssemblyDepend: PluginA
> > depends on PluginB"); // OK
> >
> >
> Assert.IsTrue(this.PluginA.Dependencies.Contains(test),
> > "AssemblyDepend: PluginA depends on PluginB"); // WRONG, should be OK
> >
> > Assert.AreEqual(((AssemblyDepend)
> > this.PluginA.Dependencies[5]).PluginName, test.PluginName,
> > "AssemblyDepend: PluginA depends on PluginB"); // OK
> > Assert.AreEqual(((AssemblyDepend)
> > this.PluginA.Dependencies[5]).EventName, test.EventName,
> > "AssemblyDepend: PluginA depends on PluginB"); // OK
> > Assert.AreEqual(((AssemblyDepend)
> > this.PluginA.Dependencies[5]).MinVersion, test.MinVersion,
> > "AssemblyDepend: PluginA depends on PluginB"); // OK
> > }
> >
> > Thank you
> > Julien
> > _______________________________________________
> > Mono-list maillist  -  Mono-list at lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-list
> >
> 
> 
> --
> Martin Hinks
> http://www.m-s-d.net
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
> 
> 


-- 
Martin Hinks
http://www.m-s-d.net


More information about the Mono-list mailing list