using hashtables

M

Mike P

I have 2 hashtables each of which I am using to store a set of IDs and
Descriptions. Hashtable 1 will have the full set of data (e.g. IDs
1,2,3,4,5) whereas Hashtable 2 will only have a subset of that data
(e.g. IDs 2,4).

What I want to do is to iterate through Hashtable 1 (using foreach?)
checking if each ID/Description is in Hashtable 2, and if not, then I
want to add the ID/Description into Hashtable 3. So by the end of this
I will have :

Hashtable 1 : complete set of data
Hashtable 2 : subset of data
Hashtable 3 : data in Hashtable 1 but not in Hashtable 2

Can anybody tell me how to iterate through my hashtables, and how to add
the data (IDs and Descriptions) to the third hashtable?
 
C

Chris Shepherd

Mike said:
I have 2 hashtables each of which I am using to store a set of IDs and
Descriptions. Hashtable 1 will have the full set of data (e.g. IDs
1,2,3,4,5) whereas Hashtable 2 will only have a subset of that data
(e.g. IDs 2,4).

What I want to do is to iterate through Hashtable 1 (using foreach?)
checking if each ID/Description is in Hashtable 2, and if not, then I
want to add the ID/Description into Hashtable 3. So by the end of this
I will have :

Hashtable 1 : complete set of data
Hashtable 2 : subset of data
Hashtable 3 : data in Hashtable 1 but not in Hashtable 2

Can anybody tell me how to iterate through my hashtables, and how to add
the data (IDs and Descriptions) to the third hashtable?

It's pretty straightforward (pseudocode):

foreach (Item in Hashtable 1)
{
if (Hasthtable 2.Contains(Item))
continue;
else
Hashtable 3.Add(Item);
}

What specifically are you having difficulty with?


Chris.
 
P

Peter Duniho

[...]
Can anybody tell me how to iterate through my hashtables, and how to add
the data (IDs and Descriptions) to the third hashtable?

While the point of having your data duplicated in two different places is
lost on me, the answer to your specific question is:

You can use foreach() to enumerate KeyValue<> pairs in the original hash
table, checking for the presence of the Key of the KeyValue<> instance in
the second hash table, and use the Add() method in the usual way to add
the KeyValue<> pair to the third table if it's not present in the second.

Pete
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Mike,

It has been a while since I last saw one post from you, I hope you had a
nice holidays.

You would have to iterate in the first hash and check the existence of the
key in hash2, if not present insert into hash 3

It should be plain simple to implement.
 
S

surcon

give us more detail if you want an optimized solution.

how the second hash is generated.

why you have a second hashtable with a subset of data from fist hashtable,
you can simple mark items from second hashtable in the first hashtable
 
M

Mike P

This is the bit I am having problems with :

Here is the syntax for populating my first two Hashtable :

hst1.Add(objDataReader["ApplicationID"],
objDataReader["ApplicationName"]);

Then I need to use a foreach loop to check for matches between the 2
hashtables and if there isn't a match, then add the data to a third
hashtable. But I am not sure about the syntax required :

foreach (int AppID in hst1)
{
if (hst2.Contains(AppID))
{
continue;
}
else
{
hst3.Add(hst1(AppID, AppName));
}
}
 
D

DeveloperX

This is the bit I am having problems with :

Here is the syntax for populating my first two Hashtable :

hst1.Add(objDataReader["ApplicationID"],
objDataReader["ApplicationName"]);

Then I need to use a foreach loop to check for matches between the 2
hashtables and if there isn't a match, then add the data to a third
hashtable.  But I am not sure about the syntax required :

foreach (int AppID in hst1)
                {
                    if (hst2.Contains(AppID))
                    {
                        continue;
                    }
                    else
                    {
                        hst3.Add(hst1(AppID, AppName));
                    }
                }

*** Sent via Developersdexhttp://www.developersdex.com***

System.Collections.Hashtable h1 = new Hashtable();
System.Collections.Hashtable h2 = new Hashtable();
System.Collections.Hashtable h3 = new Hashtable();

h1.Add("Key1","value1");
h1.Add("Key2","value2");
h1.Add("Key3","value3");
h2.Add("Key2","value2");

foreach(string key in h1.Keys)
{
if (!h2.Contains(key))
{
h3.Add(key, (string)h1[key]);
}
}

note, I'm just using strings for everything to keep it simple.
 
M

Mike P

Thanks...there's one more question I have on hashtables...how do I then
add the contents of a hashtable to a listbox for example?
 
M

Mike P

Found this out myself :

foreach (int key in hstOtherApps.Keys)
{
lstUnselected.Items.Add(new
ListItem((string)hstOtherApps[key]));
}

But now I need to iterate through a single select listbox that is
populated by a hashtable, and to find which (if any) item is selected,
and when I find one that is selected I need to remove it from the
current hashtable and add it to another hashtable.
 
D

DeveloperX

Found this out myself :

foreach (int key in hstOtherApps.Keys)
            {
                lstUnselected.Items.Add(new
ListItem((string)hstOtherApps[key]));
            }

But now I need to iterate through a single select listbox that is
populated by a hashtable, and to find which (if any) item is selected,
and when I find one that is selected I need to remove it from the
current hashtable and add it to another hashtable.

*** Sent via Developersdexhttp://www.developersdex.com***

Ok, you'll need to restructure a little.

First add a class as follows:

public class AnItem
{
private string _key;
private string _text;

public AnItem(string pKey, string pText)
{
_key = pKey;
_text = pText;
}
public override string ToString()
{
return _text;
}
public string Key
{
get
{
return _key;
}
}
}

You can adapt it to contain what ever you want, the important bit is
the ToString override.

Add these objects to your hashtables instead of just strings (or what
ever you're using).

Then you just add the object to the listbox

int i = listBox1.Items.Add(new AnItem("This is the key", "This is
displayed"));


And to retrieve it you simply do:

AnItem item = (AnItem)listBox1.Items[listBox1.SelectedIndex];

Note the cast from object to AnItem.

Then use the key to select/delete/whatever the item in the relevant
hashtable.
 
D

DeveloperX

Found this out myself :
foreach (int key in hstOtherApps.Keys)
            {
                lstUnselected.Items.Add(new
ListItem((string)hstOtherApps[key]));
            }
But now I need to iterate through a single select listbox that is
populated by a hashtable, and to find which (if any) item is selected,
and when I find one that is selected I need to remove it from the
current hashtable and add it to another hashtable.
*** Sent via Developersdexhttp://www.developersdex.com***

Ok, you'll need to restructure a little.

First add a class as follows:

public class AnItem
{


-snip-

Oh, remember to check SelectedIndex isn't -1 first (-1 being nothing
selected).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top