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

How to copy one object to another?

I have 2 classes which are exactly same except the name. I do not have
access to either of the class to change anything in it.
In my code, I need to copy one of one class to another class. How can I
do this?
class A
{
....
}

class B
{
.....Exactly same as A, all properties and methods
}

class C
{
A a;
B b;
b = (B)a; // trying to do this but fails with invalid casting.
}

Any help?
Thanks,

Nov 16 '06 #1
5 8223
I am not sure if there is any nice way to doing what you propose, but
if all properties and methods are the same you could create a
conversion function. The conversion function would take an instance of
A, make an instance of B and copy all information to it, then return B
(or vice versa).

This approach could take some time to code, esp if the class is large,
but should do what you need. Sorry to ask the obvious, but is it really
necessary to copy the contents from one class to another completely
identical class? What is it you are trying to accomplish?

On Nov 16, 1:16 pm, "DBC User" <dbcu...@gmail.comwrote:
I have 2 classes which are exactly same except the name. I do not have
access to either of the class to change anything in it.
In my code, I need to copy one of one class to another class. How can I
do this?
class A
{
...

}class B
{
....Exactly same as A, all properties and methods

}class C
{
A a;
B b;
b = (B)a; // trying to do this but fails with invalid casting.

}Any help?
Thanks,
Nov 16 '06 #2
"DBC User" <db*****@gmail.coma écrit dans le message de news:
11**********************@i42g2000cwa.googlegroups. com...

|I have 2 classes which are exactly same except the name. I do not have
| access to either of the class to change anything in it.
| In my code, I need to copy one of one class to another class. How can I
| do this?
| class A
| {
| ...
| }
|
| class B
| {
| ....Exactly same as A, all properties and methods
| }
|
| class C
| {
| A a;
| B b;
| b = (B)a; // trying to do this but fails with invalid casting.
| }

Do you want B to behave exactly like A, or do you want to add more behaviour
to B as well as that from A ?

If you want exactly the same behaviour, then you do not need another class.

If you want B to have additional behaviour and to be assignable to a
variable of type A, then you need to use inheritance.

class A
{
...
}

class B : A
{
....Exactly same as A, all properties and methods
}

class C
{
A a;
B b;
b = (B)a; // this will still fail
}

You cannot assign an instance of one class unless they are related by
inheritance, and even then, you cannot cast an instance of A to a variable
of type B, as A does not necessarily support the extra behaviour declared in
B; even if you have no extra behaviour, the rule still applies because of
the potential to have extra behaviour.

However, provided B inherits from A, you can assign an instance of B to a
variable of type A.

class C
{
A a;
B b = new B();
a = b; // this will work

// or you can even do this :

a = new B();
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 16 '06 #3
Justin's caveats aside, because they are valid, you can create a
constructor or a cast:

class A
{
public A(B item)
{
this.var = item.var;
// etc.
}

public static implicit operator A(B item)
{
A result = new A();
result.var = item.var;
return result;
}
}

Of course, neither of those work if A and B are sealed. If they are,
you'll need to create a third class:

class C // C stands for "converter" :)
{
public static B ConvertAToB(A item)
{
B result = new B();
result.var = item.var;
return result;
}

public static A ConvertBToA(B item)
{
A result = new A();
result.var = item.var;
return result;
}
}

Those are probably the cleanest way to do this.
Stephan

justin creasy wrote:
I am not sure if there is any nice way to doing what you propose, but
if all properties and methods are the same you could create a
conversion function. The conversion function would take an instance of
A, make an instance of B and copy all information to it, then return B
(or vice versa).

This approach could take some time to code, esp if the class is large,
but should do what you need. Sorry to ask the obvious, but is it really
necessary to copy the contents from one class to another completely
identical class? What is it you are trying to accomplish?

On Nov 16, 1:16 pm, "DBC User" <dbcu...@gmail.comwrote:
I have 2 classes which are exactly same except the name. I do not have
access to either of the class to change anything in it.
In my code, I need to copy one of one class to another class. How can I
do this?
class A
{
...

}class B
{
....Exactly same as A, all properties and methods

}class C
{
A a;
B b;
b = (B)a; // trying to do this but fails with invalid casting.

}Any help?
Thanks,
Nov 16 '06 #4
My favorite way of handling what you're describing is to do what ssamuel
suggests: creating a cast operator - though I usually use an explicit cast
for all custom casts even if no data is lost, just so that you know what
you're doing.

The problem with that solution is, as you said, you don't have access to
change either class. That makes it a little difficult without simply
creating a conversion method in the form of

SomeSharedStaticClass.ConvertClassAtoClassB(ClassA a, ClassB b)

The only other thing I know of, if it works for your needs, is
MemberwiseClone. MemberwiseClone is a method of the Object class so all
classes have it.

The only problem with MemberwiseClone is that it does a shallow copy. If
your classes only have value type properties, or if you can live with a
shallow copy of your reference types, then the answer to your question might
be MemberwiseClone.

HTH

Dale

--
Dale Preston
MCAD C#
MCSE, MCDBA
"DBC User" wrote:
I have 2 classes which are exactly same except the name. I do not have
access to either of the class to change anything in it.
In my code, I need to copy one of one class to another class. How can I
do this?
class A
{
....
}

class B
{
.....Exactly same as A, all properties and methods
}

class C
{
A a;
B b;
b = (B)a; // trying to do this but fails with invalid casting.
}

Any help?
Thanks,

Nov 16 '06 #5

Joanna Carter [TeamB] wrote:
"DBC User" <db*****@gmail.coma écrit dans le message de news:
11**********************@i42g2000cwa.googlegroups. com...

|I have 2 classes which are exactly same except the name. I do not have
| access to either of the class to change anything in it.
| In my code, I need to copy one of one class to another class. How can I
| do this?
| class A
| {
| ...
| }
|
| class B
| {
| ....Exactly same as A, all properties and methods
| }
|
| class C
| {
| A a;
| B b;
| b = (B)a; // trying to do this but fails with invalid casting.
| }

Do you want B to behave exactly like A, or do you want to add more behaviour
to B as well as that from A ?

If you want exactly the same behaviour, then you do not need another class.

If you want B to have additional behaviour and to be assignable to a
variable of type A, then you need to use inheritance.

class A
{
...
}

class B : A
{
....Exactly same as A, all properties and methods
}

class C
{
A a;
B b;
b = (B)a; // this will still fail
}

You cannot assign an instance of one class unless they are related by
inheritance, and even then, you cannot cast an instance of A to a variable
of type B, as A does not necessarily support the extra behaviour declaredin
B; even if you have no extra behaviour, the rule still applies because of
the potential to have extra behaviour.

However, provided B inherits from A, you can assign an instance of B to a
variable of type A.

class C
{
A a;
B b = new B();
a = b; // this will work

// or you can even do this :

a = new B();
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Thanks both. Got it working by writing the copy method.

Nov 16 '06 #6

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
16
by: bluekite2000 | last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep copy of B. Is that bad design? Why and why not?
5
by: lion | last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a pointer will add to B, but if i want to create a copy of B instead of pointer, how to operate? Note:serialization...
3
by: Jul | last post by:
Hi, I need to create copy (clone) the OracleConnection object. The OracleConnection class does not contain "Clone" method for creating copy. It contains Protected MemberwiseClone but if use...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
4
by: OpticTygre | last post by:
If I pass an object to another form via the new form's tag property, I want to create an object exactly like it, with it's properties and all, but have it be a copy of the object passed through,...
8
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.