473,715 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

arraylist object in hashtable

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.ht ml");

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

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

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

if(mp.ContainsK ey("test")) // try to retrieve the array in the hashtable,
FOUND
{
atemp.Clear();
atemp=(ArrayLis t)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("C OUNT:"+ atemp.Count ); // give's 0 : ?????
}

Nov 17 '05 #1
3 9694


Fred wrote:
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.ht ml");

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

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

mp.Add("test",a temp); // 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.ContainsK ey("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=(ArrayLis t)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("C OUNT:"+ 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)

--
Larry Lard
Replies to group please

Nov 17 '05 #2
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

"Fred" <fr**@belbone.b e> a écrit dans le message de news:
42************* **********@news .skynet.be...
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.ht ml");

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

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

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

if(mp.ContainsK ey("test")) // try to retrieve the array in the hashtable,
FOUND
{
atemp.Clear();
atemp=(ArrayLis t)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("C OUNT:"+ atemp.Count ); // give's 0 : ?????
}

Nov 17 '05 #3
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",a temp);

if(mp.ContainsK ey("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=(ArrayLi st)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

"Fred" wrote:
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.ht ml");

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

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

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

if(mp.ContainsK ey("test")) // try to retrieve the array in the hashtable,
FOUND
{
atemp.Clear();
atemp=(ArrayLis t)mp["test"]; // retrieve arraylist from key "test"
sw.WriteLine("C OUNT:"+ atemp.Count ); // give's 0 : ?????
}

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
21471
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one 'column' in the ArrayList, the other is automatically sorted and so I can use IndexOf without fear. This is because I will be going through 20,000 some odd records and don't want to use a Lookup type of table/query; I figure an ArrayList.IndexOf method...
2
2580
by: D. Shane Fowlkes | last post by:
I've been reading up on Arrays in ASP.NET. I'm going to create an two dimensional array of some type to contain 5 columns but a variable amount of rows. I read up on the ArrayList function and the HashTable as well. In the two books I have, there's no mention of the limitations of an ArrayList and it can only contain a single column array. But from the examples, I've seen, this seems to be true. The HashTable, as far as I can tell, can...
5
1558
by: Adda | last post by:
In a Parent mdi form I have a datagrid. I select a record from the grid and then invoke a childmdi form. I add the childmdi to an arraylist to keep track of it. If a user has selected multiple records from the grid and has multiple childmdi forms open and then re-selects a previously selected record, I want to activate that childmdi form. Suppose the arraylist contains indexes 0,1,2,3,4 (5 childmdi forms). A user re-selects a child...
3
2298
by: Sam | last post by:
Hi Everyone, I have a stucture below stored in an arraylist and I want to check user's input (point x,y) to make sure there is no duplicate point x,y entered (string label can be duplicated). Is there a way to compare the new input point x,y without having to a loop and compare the whole object in the array list. I can't use the indexof method because it would compare the whole object. Public Structure
20
5975
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim mylist as new FrameList mylist.Add(new FrameStructure) mylist(0).first = "blabla..." mylist(0).second...
18
4740
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
10
8508
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new Hashtable()); 2. create ArrayList aList = ArrayList.Synchronized(new ArrayList()); 3. create a string sList = ""; For 1 and 2, since the list is synced, many threads can directly
5
1471
by: blisspikle | last post by:
I figure that someone good at dotnet can look at this and give me a clue on how to easily organize this code? If there is a unique identifier like "Publisher" with a bunch of "Book" that are published under them (I used the arraylist class in the publisher class). How should the code be organized, and how can the books properties like "Name" be easily called in the main code, or searched for in the main code?
2
2464
by: Bruce | last post by:
Hi I am having a problem understanding the exact advantages of using an ArrayList over a Hashtable in any situation. In most areas of an application I am working on, lookup needs to be fast. If I use a hashtable Addition - O(1) - same as ArrayList Removal - O(1) - same as ArrayList Lookup- O(1) - ArrayList is O(n) Memory - Maybe slightly more than ArrayList since we have a hash to store.
0
9193
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9093
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
9039
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
7966
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4472
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
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3171
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
2532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.