Re: Casting a parent class to a child class
- From: "Truong Hong Thi" <thi1981@xxxxxxxxx>
- Date: 9 Oct 2006 03:30:08 -0700
B could be cast to A. A could not be cast to B.
Imagine if you could cast A to B, what if you call test.Label2, which
is not a member of class A.
S. Lorétan wrote:
Hi guys,
Sorry for this stupid question, but I don't know why it isn't working.
Here is my (example) code:
namespace Test {
class A {
public string Label1;
}
class B : A {
public string Label2;
}
class Program {
static void Main(string[] args) {
A test = new A();
B test2 = (B)test;
}
}
}
It work fine with the compiler, but when executing, it ends with an
InvalidCastException error. Why?
Thank you.
.
- References:
- Casting a parent class to a child class
- From: S. Lorétan
- Casting a parent class to a child class
- Prev by Date: Re: Casting a parent class to a child class
- Next by Date: copy SortedList
- Previous by thread: Re: Casting a parent class to a child class
- Next by thread: Re: Casting a parent class to a child class
- Index(es):
Relevant Pages
|