DaTurk,
You can cast from A to D, but it might throw an InvalidCastException.
You would have to check every time to see if it is able to cast from A to
D.
You can use the as operator to do so. It will attempt to perform a cast
to
a type, and then return null if the cast is not possible:
// Assume a has an instance of D in it, but the variable is of type a.
D d = a as D;
// If d is not null then the cast was successful.
if (d != null)
{
// Use d here.
}
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
DaTurk said:
Thanks for the quick reply,
That's not what I need to do. Here
interface A{
public void foo();
}
interface B : A{
public void fow();
}
interface C : A{
public void fei();
}
public class D : C{
}
So what I need it to have this work.
A a = new D();
a.fei();
AS of now, I want to be able to get to classes inplementing A's methods
from a cast to A.
I know this is probably a stupid question, I'm just rusty with
inheritence. How do I acces the D specific methods from a cast to A?
Thomas T. Veldhouse wrote:
Hi,
I have three interfaces lets call them parent, child-A, and child-B.
I
want to have one variable, that can be either child-A, or child-B at
run time. I just can't seem to figure out how to do it. They both
inherit from parent, and I tried holding a parent variable, and then
casting it down to either of the children, but I can't see either of
the childrens unique methods.
Thanks in advance for the help.
Can you explain what you are trying to do and post a piece of sample
code
that
isn't working for you?
It seems to me there are a number of ways to do what it seems like you
are
asking.
class A;
class B : A;
class C : A;
A obj = new C();
But that is so simple I can't believe that is what you want to do.