473,796 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9199

"harvie wang" <qu*****@gmail. com> wrote in message
news:5e******** *************** ***@news.micros oft.com...

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.micros oft.com...
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.micros oft.com...
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.micros oft.com...
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.micros oft.com...
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.micros oft.com...
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(infac t 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
17168
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 (fsHashEnumerator.MoveNext()){ if (myHashEnumerator.Key == fsHashEnumerator.Key) { myHashEnumerator.Value = fsHashEnumerator.Value; }
3
1288
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 retreive the first object after storing after the 2 (key, objectvalues) are stored I always get the last object value stored. Why doesn't qrec = (Q)HT give a copy of the last or in this code the second of the 2 objects? How should the code be...
3
9695
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 atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
1
2338
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 reply "oh but what if the types in hastable don't have copy ctors defined? that could lead to dangerous situations". well, but was it a significant effort to provide a copy ctor / deep clone method to this class for objects that provide the...
10
15158
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 that object. Occaisionally however I need to run a method on every object in the hashtable. I've done both For..Each and iEnumeratior loops. The messy part is that under certain conditions, while iterating, a condition exists where I need to remove...
10
2105
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
3551
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. 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.
2
1220
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 trying to make a full or deep copy of the master hashtable any examples would be great. Thanks, Curtis
1
5412
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
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
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
7547
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4116
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
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
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.