473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1868
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Anand Pillai | last post by:
The standard Python Queue module, allows to generate queues that have no size limit, by passing the size argument as <= 0. q = Queue(0) In a multithreaded application, these queues could be...
13
by: andrea | last post by:
Sorry for the stupid question, I know, but sometimes is necessary starts from the basic. I don't know how to pass the result of a method generated from a DAL class to a BL class returning the...
5
by: Gary Wessle | last post by:
hi or is there a better way to check the existence of "py" in a string "s"? string s = "djrfpyfd"; string t = "py"; string r = s.substr(s.find("py"),2); cout << (t==r) << endl;
25
by: pamelafluente | last post by:
Hi Guys, I have the following HTML code which is doing a GET to a page, say MyUrl.aspx : <body> <form name="form1" method="get" action="MyUrl.aspx" id="form1"> <input type="hidden"...
3
by: wildThought | last post by:
If I have an object that contains a generic dictionary inside of it, how do I get access to its properties such as count?
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
7
by: Andy B | last post by:
I have an instance of an object that needs to be accessed by all members of a page like page_load, button_click events and so on. Where in the codebehind would I put the creation of the object...
2
by: Andy B | last post by:
I have the following listView control on a page: <asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1"> <ItemTemplate> <dl> <dt>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.