473,425 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Copy a hashtable

I have a hashtable in a class that contains key (strings) value (integers,
strings, longs, etc...) data pairs.

I want to move the data to another (different) class instance for
processing.

The key is that I want to make sure I don't rely on the first class to stay
around so I want to make sure that when I move (copy) the data from one
class to another I get a REAL copy, not just a reference to the node in the
first hashtable.

Basic looks of what I would like is below:

Public class Foo

private mDatastore as new hashtable

public sub AddData(. . .)
Nov 23 '05 #1
2 3533
Ray,

The most simple way to deepcopy an object is first to serialize it and than
to deserialize it. Here a sample for an arraylist. It has for that to
implement Iserializable

http://www.vb-tips.com/default.aspx?...c-61641f5c8d9d

The hashtable implements Iserializable as well.

I hope this helps,

Cor
Nov 23 '05 #2
Ray,

If you can guarentee that the items in the dictionary implement
ICloneable then you can do the following.

For Each item as DictionaryEntry in sourceDictionary
Dim cloneable as ICloneable = DirectCast(item.Value, ICloneable)
destinationDictionary.Add(item.Key, cloneable.Clone())
Next

Brian

Ray Cassick (Home) wrote:
I have a hashtable in a class that contains key (strings) value (integers,
strings, longs, etc...) data pairs.

I want to move the data to another (different) class instance for
processing.

The key is that I want to make sure I don't rely on the first class to stay
around so I want to make sure that when I move (copy) the data from one
class to another I get a REAL copy, not just a reference to the node in the
first hashtable.

Basic looks of what I would like is below:

Public class Foo

private mDatastore as new hashtable

public sub AddData(. . .)
.
.
.

public function GetData() as hashtable
return mDatastore

end function

End Class

FooInstance.AddData("Key1", value1)
FooInstance.AddData("key2", value2)

Public class Bar

private mLocalData as new hashtable

mLocalData = FooInstance.GetData()

End Class

... but I have a feeling that this is going to simply get my mLocalData set
up with references to the nodes in the mDatastore hashtable and not what I
want.

I even though about walking the hashtable returned by the GetData function
(using a for each and a dictionary entry object) then just adding them to
the mLocalData instance but was not sure that would do it either...

any ideas?


Nov 23 '05 #3

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

Similar topics

3
by: M | last post by:
I am trying to copy the values of one hashtable to another. I ran into a read only error when using the IDictionaryEnumerator. while (myHashEnumerator.MoveNext()){ while...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
16
by: Sreekanth | last post by:
Hello, Is there any better collection than HashTable in terms of performance, when the type of the key is integer? Regards, Sreekanth.
6
by: harvie wang | last post by:
Hi, I want to implement a copy construct class, but the class have as Hashtable,how to do? public class A { private Hashtable _data; public A() { _data = new Hashtable(); }
5
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's...
1
by: TheVillain9 | last post by:
I'm adding key-values to my hashtable (company_data), but everytime I add something new, it adds it to the element from which the hashtable was derived, Universe. Universe is just an arraylist of...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
1
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.