PC Review


Reply
Thread Tools Rate Thread

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

 
 
Alexander Walker
Guest
Posts: n/a
 
      1st Feb 2006
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


 
Reply With Quote
 
 
 
 
Peter Huang [MSFT]
Guest
Posts: n/a
 
      2nd Feb 2006
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.

 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      2nd Feb 2006
> 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.

 
Reply With Quote
 
Alexander Walker
Guest
Posts: n/a
 
      2nd Feb 2006
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-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| 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.
|


 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      2nd Feb 2006
> 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.

 
Reply With Quote
 
Alexander Walker
Guest
Posts: n/a
 
      2nd Feb 2006
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


 
Reply With Quote
 
Peter Huang [MSFT]
Guest
Posts: n/a
 
      3rd Feb 2006
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.

 
Reply With Quote
 
Alexander Walker
Guest
Posts: n/a
 
      3rd Feb 2006
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


 
Reply With Quote
 
Yan-Hong Huang[MSFT]
Guest
Posts: n/a
 
      7th Feb 2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Job Queues. sapna mc Microsoft VB .NET 0 17th Jun 2008 08:10 AM
Perfmon - Server work queues object missing =?Utf-8?B?Q2FudWNrIGdlb2Zm?= Microsoft Windows 2000 0 27th Apr 2007 02:06 PM
Printer Queues R1Bandit Windows XP General 0 15th Sep 2004 05:02 AM
Two printer queues Daniel Microsoft Windows 2000 Printing 0 28th Apr 2004 02:06 PM
Message Queues Neil Stevens Microsoft VB .NET 8 12th Apr 2004 04:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:24 AM.