HighTechTalks DotNet Forums  

collection doesn't find element

Dotnet General Discussions microsoft.public.dotnet.general


Discuss collection doesn't find element in the Dotnet General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
sorincom
 
Posts: n/a

Default collection doesn't find element - 08-08-2005 , 04:34 PM






Hello,

I have this class:

public class TestClass
{
private string testData = "";
public string TestData
{
get { return this.testData; }
set { this.testData = value; }
}

//public override bool Equals(object obj)
//{
// return this.testData.Equals(obj);
//}

//public override int GetHashCode()
//{
// return this.testData.GetHashCode();
//}
}

and I use it like this:

static void Main(string[] args)
{
TestClass test = new TestClass();
ArrayList ary = new ArrayList();

test.TestData = "test data";

ary.Add(test);

Trace.WriteLine(((TestClass)ary[0]).TestData);
Debug.Assert(ary.Contains(test), "Asserting the collection
contains <test>.");
}

With the *Equals* override commented everything works fine: the trace writes
the "test data" string and the assert is quiet.

Could somebody please tell me why if only the *Equals* override is
uncommented the trace still works fine but the assert fails ?

Note: commenting/uncommenting the GetHashCode() doesn't seem to make any
difference.

Thanks in advance...

Reply With Quote
  #2  
Old   
sorincom
 
Posts: n/a

Default RE: collection doesn't find element - 08-11-2005 , 09:31 AM






Well, peer review seems to be indeed a good thing: I didn't notice equals
compares a String with a TestClass !...

"sorincom" wrote:

Quote:
Hello,

I have this class:

public class TestClass
{
private string testData = "";
public string TestData
{
get { return this.testData; }
set { this.testData = value; }
}

//public override bool Equals(object obj)
//{
// return this.testData.Equals(obj);
//}

//public override int GetHashCode()
//{
// return this.testData.GetHashCode();
//}
}

and I use it like this:

static void Main(string[] args)
{
TestClass test = new TestClass();
ArrayList ary = new ArrayList();

test.TestData = "test data";

ary.Add(test);

Trace.WriteLine(((TestClass)ary[0]).TestData);
Debug.Assert(ary.Contains(test), "Asserting the collection
contains <test>.");
}

With the *Equals* override commented everything works fine: the trace writes
the "test data" string and the assert is quiet.

Could somebody please tell me why if only the *Equals* override is
uncommented the trace still works fine but the assert fails ?

Note: commenting/uncommenting the GetHashCode() doesn't seem to make any
difference.

Thanks in advance...

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.