Queue.Contains(myobj) return always false..

  • Thread starter Thread starter semedao
  • Start date Start date
S

semedao

Hi
I have some Queue object that I enqueue other objects into it.
I want to check if someobject already exists before enqueue , using the
Contains method
but italways return false.
in the object that I enqueue I implement CompareTo method (Icompareable)
but the debugger doesn't go there...
thanks
 
Hi,

Queue.Contains internally uses Object.Equals() to determine whether or not
two object instances are equal. So I think you need to override the Equals
method in your object like the following example:

public class Foo
{
public int id;

public override bool Equals(object obj)
{
if (obj is Foo)
{
return (obj as Foo).id == id;
}
return false;
}
}

class Program
{
static void Main(string[] args)
{
Queue q = new Queue();
Foo f1 = new Foo();
f1.id = 1;
Foo f2 = new Foo();
f2.id = 1;
q.Enqueue(f1);
if (q.Contains(f2))
{
Console.WriteLine("contains");
}
}
}

I hope this helps. Please feel free to post here if anything is unclear.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
You can override the Equals and GetHashCode methods.

public class TestClass
{
public DateTime _time;
public string _name;

public TestClass( DateTime time, string name )
{
_time = time;
_name = name;
}

public override bool Equals( object obj )
{
TestClass tc = obj as TestClass;

return tc._name == this._name;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}

It'll work without overriding GetHashCode, but you'll get a warning.

HTH

Glenn
 
semedao,

Queue.Contains uses Equals method to compare items. You should override
this method if you need to provide custom comparition. If you decide to do
this read MSDN article for Object.Equals method. There are the requirements
for this method when overriden.

It is realy strange that it doesn't work though. Can you post a compilable
sample that demonstrates the problem?
 
Hi semedao,

What do you think of the replies above? Please feel free to post here if
anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks , I override it and it'swork , but some confused why to override
twice the same method , one by implementing IEquatable , and other for
regular...
 
Hi,

The generic interface IEquatable is an interface which will compare an
object against another object to see if they are equal. It has one method
for you to implement, the Equals( T obj ) method. This is pretty similar
to the System.Object.Equals method you maybe thinking, but the difference
is that the Object.Equals method that all classes have, is not type
specific, while the IEquatable interface is a generic type and will be
type-safe. When thinking about value types this comes into play even more
because with the Object.Equals, we have to box/unbox those values in order
to compare them, while our type-safe version of IEquatable will not need to
box the value type.

If you are using generic version of Queue<>, then either overriding
Object.Equals or implementing IEquatable<> will work. But the latter is
recommended since it has better performance. If you are using non generic
version of Queue, then only overriding Object.Equals will work.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Semedao,

Did my previous reply answer your question? Please reply here to let me
know whether you need further information. Thank you.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Yes , thank you
I need to implement the regular equals , because I can't use the generic
queue in the place I need it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top