arraylist object in hashtable

F

Fred

I'm trying to build a hashtable and a arraylist as object value

I'm not able to retrieve stored object from the hashtable.


Hashtable mp = new Hashtable(); // THE HASHTABLE
ArrayList atemp = new ArrayList(); // THE ARRAY

StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");

atemp.Clear(); // Create an array with just 1 element "5"
atemp.Add("5");

sw.WriteLine("COUNT:" + atemp.Count ); // give's 1 : OK

mp.Add("test",atemp); // ADD the array to the hashtable

if(mp.ContainsKey("test")) // try to retrieve the array in the hashtable,
FOUND
{
atemp.Clear();
atemp=(ArrayList)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("COUNT:"+ atemp.Count ); // give's 0 : ?????
}
 
L

Larry Lard

Fred said:
I'm trying to build a hashtable and a arraylist as object value

I'm not able to retrieve stored object from the hashtable.

Actually, you are :)
Hashtable mp = new Hashtable(); // THE HASHTABLE
ArrayList atemp = new ArrayList(); // THE ARRAY

StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");

atemp.Clear(); // Create an array with just 1 element "5"
atemp.Add("5");

sw.WriteLine("COUNT:" + atemp.Count ); // give's 1 : OK

mp.Add("test",atemp); // ADD the array to the hashtable

OK you have added the ArrayList to the Hashtable. But atemp is still a
reference to that same ArrayList!
if(mp.ContainsKey("test")) // try to retrieve the array in the hashtable,
FOUND
{
atemp.Clear();

You have just cleared out the ArrayList! atemp and mp["test"] are the
*same* ArrayList. If you change this line to

atemp=null;
atemp=(ArrayList)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("COUNT:"+ atemp.Count ); // give's 0 : ?????
}

then you will get the results you want (Of course you don't *need* to
set atempt to null before re-assigning it)
 
F

Fabien Bezagu

Fred,

I seems to be surprised your array is cleared but it's perfectly normal.

Since ArrayList is a reference type, when adding it to the hashtable, you
don't create another instance by cloning your original array. You have only
one array, that you cleared.

Fabien
 
G

Guest

Hi Fred,
because an ArrayList is a reference type not a value type, when you add it
to the hashtable, the hashtable does not store an exact copy of the
ArrayList, it just stores a reference to the original ArrayList, so when you
cleafr the contents out of the array list at the end of your code and then
get the count of the items in the arraylist stored in the hashtable (ie
really the same object) then your result will be zero because you removed all
the items from the list:

i.e. in your code:


Hashtable ht = new Hashtable();
ArrayList atemp = new ArrayList();
ArrayList atemp2;

//remove all the items from the ArrayList and add one item
atemp.Clear();
atemp.Add("5");

atemp.Count -> should equal one

//add a REFERENCE to the arraylist to the hashtable, NOT a copy
//of the arraylist - if you want a copy then you need to Clone the object
//to produce a copy of the object
mp.Add("test",atemp);

if(mp.ContainsKey("test")) // try to retrieve the array in the hashtable,
FOUND
{
//Here you are removing al lthe items form the arraylist, this is the same
//object referenced inside the hashtable
atemp.Clear();
//atemp.Count should equal zero at this point

//retrieve the object reference form the hashtable
atemp2=(ArrayList)mp["test"];

//atemp and atemp2 point to the same instance of an object
//so atemp2 will also have a count of zero.

}


Hope that helps
Mark R Dawson
 

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