Specified cast is not valid - copy object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

i have a problem to cast an object - hope somebody can help me. thx

namespace CopyTest
{

public class A
{
public int a;
}

public class B:A
{
public int b;
}

class TestClass
{

[STAThread]
static void Main(string[] args)
{
A a = new A();
B b = new B();

a.a = 1;
b = a; // <- error

// An unhandled exception of type 'System.InvalidCastException' occurred in
CopyTest.exe
// Additional information: Specified cast is not valid.
}
}
}
 
Hi,

What if you do this:

b = a;
b.b = 4;

b contains a A instance and A does not contain a "b" member and you would
get an error.

Now , I would bet that the above line provoke a compilation error (unless a
cast be used), are you sure it;s the real code you are using?


Cheers,
 
You are trying to assign the variable "b"( which you've said will point at
an object of type "B") to an object of type "A". Since "A" does not derive
from "B", this isn't allowed.

What you probably want is:
b.b = a.a

You may also want to review Object Oriented fundamentals and concepts
This is for java, but the concepts hold:
http://www.iam.ubc.ca/guides/javatut/java/objects/
 
this is the rigth code, the goal is to copy the same members from Class A to
Class B.
The result should be that only the base-class copied to the the Object B

namespace CopyTest

{


public class A

{

public int a;

}

public class B:A

{

public int b;

}

class TestClass

{


[STAThread]

static void Main(string[] args)

{

A a = new A();

B b = new B();

/* is working

b.a = 1;

b.b = 2;

a = b;

*/

a.a = 1;

b = a; // <- error


// An unhandled exception of type 'System.InvalidCastException' occurred in
CopyTest.exe

// Additional information: Specified cast is not valid.

}

}

}
 
Mike said:
this is the rigth code, the goal is to copy the same members from Class A to
Class B.
The result should be that only the base-class copied to the the Object B

And the error is correct. The result that *you* want is that only the
base class members get copied. But the compiler has no idea what you
are trying to do, only that B could have a lot more in it than what it
inherits from A and therefore won't allow a straight assignment. B is A
but A is not B.

I might write a Copy method on B that takes A as input and copies data
appropriately.
 
thx for the input - so my only change is a Memberwise Copy ?

ok i try this way and get the error... 'CopyTest.B.implicit operator
CopyTest.B(CopyTest.A)': user-defined conversion to/from base class



public class B:A
{
public static implicit operator B(A xx)
{
return new B();
}
public int b;
}
 
Mike said:
thx for the input - so my only change is a Memberwise Copy ?

ok i try this way and get the error... 'CopyTest.B.implicit operator
CopyTest.B(CopyTest.A)': user-defined conversion to/from base class



public class B:A
{
public static implicit operator B(A xx)
{
return new B();
}
public int b;
}

Your operator, as coded here, would be to cast an object of type B to an
object of type B. That's not allowed because it's not necessary.
 
Back
Top