473,809 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.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(aInsta nce1);

A aInstance2 = new A();

aInstance2.B = 5;

if (queueDictionar y[1].Contains(aInst ance2))
Console.WriteLi ne("Found instance of A with B set to 5");
else
Console.WriteLi ne("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 1881
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.m icrosoft.com> wrote in message
news:js******** ******@TK2MSFTN GXA02.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.Defaul t to determine equality, and that Comparer.Defaul t 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
5022
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 useful when you have many threads using the queue for data access and synchronization. Is there a performance hit when one uses an 'infinite' queue
13
2162
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 results as it is. I mean, for instance, something like this. namespace DAL {
5
3311
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
29766
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" name="ClickedElement" id="Messenger" /> </form> </body>
3
4185
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
7118
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 someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
5
1900
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(); TheObject.Dictionary<string, string= new Dictionary<string, string>(); .... } The first line (TheObject instance = new TheObject();) doesn't get
7
1312
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 instance?
2
2363
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
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.