364,033 Members | 4804 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Comparing two Dictionaries

mylixes
P: 29
I have this list of devices and I store it in the Dictionary. When the program loaded, the Items of the dictionary will be displayed in the listView. I created a thread that will refresh the list when a new device is connected to a usb port. Now, how could I compare the items currently stored in the dictionary to the new items saved in another Dictionary?

For example:

Dictionary1 = {device1, device2} -this dictionary holds the items displayed in the listView.

When the program is loaded, the thread will start and will poll for the new device.

Dictionary2 = {device1, device2, device3} -consider that device3 is the new connected device.

Now, what is the simpliest way to compare the two dictionaries without affecting the process being run by the device1 and device2 of Dictionary1?

I also want device3 to automatically displayed in the ListView when the thread detect it.
Feb 6 '12 #1
Share this Question
Share on Google+
3 Replies


GaryTexmo
Expert 100+
P: 1,225
For the purposes of comparison, imho just compare the keys. If you're only interested in comparing dictionary1 to dictionary2, you could do something like...

Expand|Select|Wrap|Line Numbers
  1. List<object> newItems = new List<object>();
  2. foreach (object key in dictionary2.keys)
  3. {
  4.   if (!dictionary1.ContainsKey(key)
  5.     newItems.Add(key);
  6. }
That should populate newItems with all the keys that dictionary2 has but dictionary1 doesn't have. You might have to do another pass if you also want the items that dictionary1 has but dictionary2 doesn't have.

Also this is comparing keys, as a Dictionary object is essentially a list of KeyValuePair objects. If you wanted to compare values you could change the code to do that.

As for comparing them without having an impact on the process, you'll probably want to do the compare in another thread, and make sure you use the lock statement to ensure you're not reading and writing to the dictionaries at the same time. Perhaps when you detect differences, you can trigger an event from your processing thread to your GUI thread to have your ListView update?

There are several ways you could go about this, that's just one suggestion. Hopefully it gives you some ideas :)
Feb 6 '12 #2

mylixes
P: 29
Thank you so much for the explanation. I already tried that. Im just kinda confused with what I have encountered.

Actually I want my program to do this:

I have a listView that will display a list of devices. each devices may have different interfaces.

For example:
Device1 - Device1.0
- Device1.0
- Device1.0

Device2 - Device2.1
- Device2.1
- Device2.1

The program can detect similar device name. they only differ in theie interface index.
So, given the example above, device1 and device2 are just similar device having the same device name but their interfaces have different index. The index may vary according to the number of similar device connected to the PC.

Now, I want to display the devices' name to the listView and I want it to be sorted.
I want to display only the device name exclusing their interfaces. So, for the example above, I want to display the like this:

Row 1: Device1
Row 2: Device2

I displayed two similar devices since the PC detected two similar devices.
I actually done this part.

But my problem now is that, my program run a thread that will automatically detect a new device connected to the PC.

So if the thread detected the new device, I store it to another Dictionary. By the way, the values displayed in the ListView are alose stored in the Dictionary.

say for an instance, I have Dict1 that holds values of the ListView and I have Dict2 which will hold the values of the listView and the new detected device.

I want to compare if the two dictionaries are equal. I tried comparing their keys and values I managed to do that. But I am quite confused if the scenario is like this:

dict1 = {[key = 0], [value = "device1"
[key = 1], [value = "device2"}

dict2 = {[key = 0], [value = "device3"
[key = 1], [value = "device1"
[key = 2], [value = "device2"}

Since, it is possible that the new device can be store at any dictionary index because of alphabetical order.

Any help will be highly appreciated.
Feb 7 '12 #3

GaryTexmo
Expert 100+
P: 1,225
I see your confusion... I ask you this, why are you using an integer as your key in your dictionary? You're effectively turning your dictionary into a List and losing all the advantages of having a key. In fact, why use a dictionary at all, why not just use a List?

In either case you mentioned you tried my example code as well as comparing by value. I don't know how you did this but were unable to merge the dictionaries. Looking at the example you provided, it's quite clear that dict2 does not contain the value "device3" and thus it would be detected as a missing item.

Using almost the exact code I posted originally, but using Values instead of Keys I'm able to detect that device3 is not present in dict1. I freely admit that I may not be understanding you correctly but your last bit there with the example data is pretty clear. Perhaps you should post the code you're trying to use...
Feb 7 '12 #4

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp