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

  • Thread starter Thread starter harvie wang
  • Start date Start date
H

harvie wang

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-2
 
harvie wang said:
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.
 
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 said:
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.
 
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
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.
 
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
}

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.
 
harvie wang said:
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.
 
Back
Top