Check for the existence of a queues object inside a Dictionary of Queues

  • Thread starter Alexander Walker
  • Start date
A

Alexander Walker

Hello

I am using a Dictionary to store some queues which store instances of a class
that I have written

I would like to be able to determine whether a particular queue already contains
an instance with a particular value

Here is the code

using System;
using System.Collections.Generic;

namespace AlexanderWalker.Scratch
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
Dictionary<int, Queue<A>> queueDictionary = new Dictionary<int,
Queue<A>>();

Queue<A> aQueue = new Queue<A>();

queueDictionary.Add(1, aQueue);

A aInstance1 = new A();

aInstance1.B = 5;

queueDictionary[1].Enqueue(aInstance1);

A aInstance2 = new A();

aInstance2.B = 5;

if (queueDictionary[1].Contains(aInstance2))
Console.WriteLine("Found instance of A with B set to 5");
else
Console.WriteLine("Nothing was found");
}
class A
{
private int b;

public int B
{
get
{
return b;
}
set
{
b = value;
}
}
}
}
}

In the code above I would like to alter the class A or inherit from the queue so
that the text "Found instance of a with B set to 5" would be displayed on the
console. How would I do this?

Thanks

Alex
 
P

Peter Huang [MSFT]

Hi Alexander,

Thanks for your posting.
I just wonder why you need the function maybe we have another approach.
I understand that you want the A class used the B property to judge if two
classes are equal.
Here are some code for your reference.
class A
{
private int b;
public override bool Equals(object obj)
{
A o = obj as A;
if (o==null)
return false;
if (this.B == o.B)
return true;
else
return false;
//return base.Equals(obj);
}
public int B
{
get
{
return b;
}
set
{
b = value;
}
}
}

NOTE: commonly we did not recommened to override the equal method. Because
by default, we always call the base class Object.Equal Which will maintain
the consistent. The code above is for demo purpose, for detailed
information about how to implement an equal method please refer to the book
below.
See Common Object Operation in Book
Applied Microsoft .NET Framework Programming (Paperback)
by Jeffrey Richter
http://www.amazon.com/gp/product/0735614229/104-4488294-3613566?v=glance&n=2
83155

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.
 
B

Bruce Wood

NOTE: commonly we did not recommened to override the equal method. Because
by default, we always call the base class Object.Equal Which will maintain
the consistent.

Actually, so far as I know this is not true. It is common to override
the Equals() method for reference types, if you can establish clear
rules for when two of your objects are considered "equal."

What is _not_ recommended is overloading the == operator for reference
types. (So far) the .NET convention is that .Equals() compares for some
sort of identity of contents on key fields, while == does reference
comparison. In other words, Equals() tells you if two objects refer to
the same real-world entity; == tells you if two variables refer to the
same object instance.
 
A

Alexander Walker

Thanks for that Peter

After sending the message to the group I looked at MSDN and found the
documentation for the Queue<> where it said that the Contains method would check
to see if the type implemented IComparable<> or IComparable and use the
CompareTo method if it did, so I implemented IComparable<A> but the CompareTo
method didn't seem to get called when I invoked the Contains method on the
Queue<>, I then tried implementing IComparable and still no had no joy, the
documentation also mentioned overriding the Equals method which I did and it
worked, I also had to override the GetHashCode method

The reason why I was trying to see if the two instances had a property with the
same value is because I was using the property as a unique identifier for the
instance and I wanted to make sure that another instance with a property of the
same value was not added to the queue, is there a more appropriate way to do
this?

Alex


| Hi Alexander,
|
| Thanks for your posting.
| I just wonder why you need the function maybe we have another approach.
| I understand that you want the A class used the B property to judge if two
| classes are equal.
| Here are some code for your reference.
| class A
| {
| private int b;
| public override bool Equals(object obj)
| {
| A o = obj as A;
| if (o==null)
| return false;
| if (this.B == o.B)
| return true;
| else
| return false;
| //return base.Equals(obj);
| }
| public int B
| {
| get
| {
| return b;
| }
| set
| {
| b = value;
| }
| }
| }
|
| NOTE: commonly we did not recommened to override the equal method. Because
| by default, we always call the base class Object.Equal Which will maintain
| the consistent. The code above is for demo purpose, for detailed
| information about how to implement an equal method please refer to the book
| below.
| See Common Object Operation in Book
| Applied Microsoft .NET Framework Programming (Paperback)
| by Jeffrey Richter
| http://www.amazon.com/gp/product/0735614229/104-4488294-3613566?v=glance&n=2
| 83155
|
| 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.
|
 
B

Bruce Wood

The reason why I was trying to see if the two instances had a property with the
same value is because I was using the property as a unique identifier for the
instance and I wanted to make sure that another instance with a property of the
same value was not added to the queue, is there a more appropriate way to do
this?

That's exactly what Equals() is for. IComparable is for the situation
in which those
unique identifiers have an intrinsic ordering.
 
A

Alexander Walker

When I read the documentation I thought that it was saying that IComparable was
supposed to be used for what Equals() is used for. After re-reading the
documentation, I can confirm that it definitely says that the Contains method
will use Comparer.Default to determine equality, and that Comparer.Default will
check whether the type implements IComparable and use it if its available, might
this be a documentation error? Perhaps I misunderstand what the words mean.

Thanks very much for your advice. I have overridden the Equals() method and it
does exactly what I want it to do

Alex
 
P

Peter Huang [MSFT]

Hi Alex,

Thanks for your information.
I think you understand correctly and you can submit the feedback in the
link "Comments" at the end of the MSDN page.
"Send comments about this topic to Microsoft"

If you still have any concern, please feel free to 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.
 
A

Alexander Walker

Hi Peter

I have sent a comment about the documentation of the Contains method to
Microsoft. I described how I was confused by it and that I thought that the
IComparable implementation would be used to determine equality by the Contains
method when in reality the Equals method is used

Alex
 
Y

Yan-Hong Huang[MSFT]

Hi Alex,

Thanks very much for your update. We appreciate your feedback very much. It
helps us improve our product very much. :)

If there is any question, please feel free to post in the newsgroup. Thanks
again and have a good day.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://msdn.microsoft.com/subscriptions/managednewsgroups/

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

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

Top