473,395 Members | 1,629 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,395 software developers and data experts.

Hashtable.Clone() is shadow copy, how to depth copy hashtable?


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();
}
public A(A obj)
{
_data = A._data.Clone(); //but this is shadow copy
}
public void func()
{
private ClassA a = new ClassA();
private ClassB b = new ClassB();
_data.Add("a",a);
_data.Add("b",b);
}
}

public main()
{
private A a;
private A b;
a.func();
private A c = new A(a); //I want to set c._data["a"] is a new
ClassA
b = a;
}
best wish,

Harvie

2005-10-27
Nov 17 '05 #1
6 9188

"harvie wang" <qu*****@gmail.com> wrote in message
news:5e**************************@news.microsoft.c om...

Hi,
I want to implement a copy construct class, but the class have as
Hashtable,how to do?


You aren't going to be able to do it in general. The objects contained in
the hashtable are going to have to be able to clone themselves.

Nov 17 '05 #2
Hello Daniel O'Connell [C# MVP],
thanks!

Is the class A can do the copy work?
private A a = new A();
a.func();
private A b = new B();
b = a;//
now b._data have a ClassA object? have a handle of object?

ClassA a = (ClassA)b._data["a"];
ClassA b = (ClassA)a._data["a"];
AddressOf(a) isn't equal to AddressOf(b)?

"harvie wang" <qu*****@gmail.com> wrote in message
news:5e**************************@news.microsoft.c om...
Hi,
I want to implement a copy construct class, but the class have as
Hashtable,how to do?

You aren't going to be able to do it in general. The objects contained
in the hashtable are going to have to be able to clone themselves.

Nov 17 '05 #3

"harvie wang" <qu*****@gmail.com> wrote in message
news:5e**************************@news.microsoft.c om...
Hello Daniel O'Connell [C# MVP],
thanks!

Is the class A can do the copy work?
private A a = new A();
a.func();
private A b = new B();
b = a;//
now b._data have a ClassA object? have a handle of object?
b would be a reference that a had. You will not get C++ style assignmnt
copying.

ClassA a = (ClassA)b._data["a"];
ClassA b = (ClassA)a._data["a"];
AddressOf(a) isn't equal to AddressOf(b)?


I'm not sure what you mean here.
Nov 17 '05 #4
Hi, Daniel O'Connell

Thanks very much!
ClassA a = (ClassA)b._data["a"];
ClassA b = (ClassA)a._data["a"];
AddressOf(a) isn't equal to AddressOf(b)?
I mean suppose that the memory address of object a is 0x1234, what's
the memory address of object b ? Is 0x1234?

Harvie

2005-10-28
"harvie wang" <qu*****@gmail.com> wrote in message
news:5e**************************@news.microsoft.c om...
Hello Daniel O'Connell [C# MVP],
thanks!
Is the class A can do the copy work?
private A a = new A();
a.func();
private A b = new B();
b = a;//
now b._data have a ClassA object? have a handle of object?

b would be a reference that a had. You will not get C++ style
assignmnt copying.
ClassA a = (ClassA)b._data["a"];
ClassA b = (ClassA)a._data["a"];
AddressOf(a) isn't equal to AddressOf(b)?

I'm not sure what you mean here.

Nov 17 '05 #5
Hello Daniel O'Connell [C# MVP],
thanks!
copy construct can get new object, such as :
public class Test
{
private int a;
private string s;
private Hashtable h;
public Test()
{
a = 10;
s = "hello";
h = new Hashtable;
}
public Test(Test t)
{
a = t.a;
s = t.s;
h = t.h;//copy?
h = (Hashtable)t.h.Clone() // shadow copy
}
public void func()
{
ClassA testa = new ClassA();
ClassA testb = new ClassA();
h.Add("a",testa);
h.Add("b",testb);
a = 15;
}
}

void main()
{
private Test a = new Test();
a.func();
private Test b = new Test(a); //here b.h have testa and testb
and b.a = 15
//object b is a new object, I want b.h is a new Hashtable
private Test c = new Test(); // here c.h has no element and
c.a = 10
c = a; // c is a refrence ,not I want
}

"harvie wang" <qu*****@gmail.com> wrote in message
news:5e**************************@news.microsoft.c om...
Hello Daniel O'Connell [C# MVP],
thanks!
Is the class A can do the copy work?
private A a = new A();
a.func();
private A b = new B();
b = a;//
now b._data have a ClassA object? have a handle of object?

b would be a reference that a had. You will not get C++ style
assignmnt copying.
ClassA a = (ClassA)b._data["a"];
ClassA b = (ClassA)a._data["a"];
AddressOf(a) isn't equal to AddressOf(b)?

I'm not sure what you mean here.

Nov 17 '05 #6

"harvie wang" <qu*****@gmail.com> wrote in message
news:5e**************************@news.microsoft.c om...
Hi, Daniel O'Connell

Thanks very much!
>> ClassA a = (ClassA)b._data["a"];
>> ClassA b = (ClassA)a._data["a"];
>> AddressOf(a) isn't equal to AddressOf(b)?

I mean suppose that the memory address of object a is 0x1234, what's
the memory address of object b ? Is 0x1234?


Well, that code is kinda ambigious(infact it doesn't work), but in that case
I would guess yes, the address would be hte same. it'd be far clearer if a
and b weren't assigned from themselves.
Nov 17 '05 #7

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...
3
by: A. Burch | last post by:
I'm wanting to use a Hashtable. I have 2 (keys, objectvalues) pairs of data to store. I'm using a static declartion for the Hashtable the instance of the class that is the object. If I try and...
3
by: Fred | last post by:
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...
1
by: oDDskOOL | last post by:
I realized today that the Hashtable.Clone only produces a shallow copy... that makes me go mad that M$ doesn't even provide a deep copy ctor for the Hashtable class ! mighty tech ducks might...
10
by: Ken Foster | last post by:
I have a hashtable keyed by some name, holding an instance of an object. Most of the time I use the hashtable in the traditional sense, given a name, I lookup the object, then I run a method on...
10
by: Q | last post by:
Hi, I'm using a hashtable to store away a bunch of numbers from my array: The Hashtable should look like: Key Value 1 01234 2 01235
2
by: Ray Cassick \(Home\) | last post by:
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. ...
2
by: Curtis | last post by:
Does anyone have any examples of serializing a hashtable? I tried to make a copy of the hash table using the .clone method but per MS documention it creates a shallow copy of the object. I am...
1
by: Ivan | last post by:
is it possible to make a universal hashtable that would be able to serialize itself to xml format? universal one, meaning I don't have to know about the type of keys and values in advance. Ivan
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...

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.