By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

How to copy one object to another?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.