How to copy one object to another?

  • Thread starter Thread starter DBC User
  • Start date Start date
D

DBC User

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,
 
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?
 
"DBC User" <[email protected]> a écrit dans le message de (e-mail address removed)...

|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
 
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
 
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
 
Joanna said:
"DBC User" <[email protected]> a écrit dans le message de (e-mail address removed)...

|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

Thanks both. Got it working by writing the copy method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top