Casting a parent class to a child class
- From: "S. Lorétan" <tynril@[nospam]gmail.com>
- Date: Mon, 9 Oct 2006 12:18:22 +0200
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.
.
- Follow-Ups:
- Re: Casting a parent class to a child class
- From: Chris Dunaway
- Re: Casting a parent class to a child class
- From: Truong Hong Thi
- Re: Casting a parent class to a child class
- From: steve.kaye
- Re: Casting a parent class to a child class
- Prev by Date: Re: Minimum age validation in C#
- Next by Date: Connect VB with C#, VB give instruction to a C# code ?
- Previous by thread: Minimum age validation in C#
- Next by thread: Re: Casting a parent class to a child class
- Index(es):