471,315 Members | 1,448 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

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

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
Feb 1 '06 #1
8 1792
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/073...6?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.

Feb 2 '06 #2
> 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.

Feb 2 '06 #3
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
""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:js**************@TK2MSFTNGXA02.phx.gbl...
| 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/073...6?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.
|
Feb 2 '06 #4
> 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.

Feb 2 '06 #5
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
Feb 2 '06 #6
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.

Feb 3 '06 #7
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
Feb 3 '06 #8
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/subscripti...gednewsgroups/

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

Feb 7 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Anand Pillai | last post: by
5 posts views Thread by Gary Wessle | last post: by
25 posts views Thread by pamelafluente | last post: by
3 posts views Thread by wildThought | last post: by
1 post views Thread by =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.