471,092 Members | 1,111 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 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 3318
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by francois | last post: by
33 posts views Thread by Ken | last post: by
16 posts views Thread by Sreekanth | last post: by
5 posts views Thread by BenW | last post: by
1 post views Thread by TheVillain9 | last post: by

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.