Dynamic object creation.

  • Thread starter Thread starter DaTurk
  • Start date Start date
D

DaTurk

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.
 
DaTurk said:
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.
 
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?
 
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?
 
Well, the cast will work fine, that's not my problem. But if I have an
instance of D, and cast it to A, I'll only have acces to A's methods,
and not to any D specific methods. This is what I'm trying to fix, I
want to access D specific methods after casting it to A.
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.
 
After you cast it to a D it will only have methods as a D .. in order to
access methods as if it were an A you wald have to upcast it to an A ...

A D could either be an A or a D, it doesn't know until runtime so by trying
to call a method from an A on a D may result in a failure (the D may not
actualyl be an A). There are ways to support this it is known as late
binding but C# does not do this by default ... You can use things like
dynamic methods to support this ..
http://www.codeproject.com/csharp/dynamicmethoddelegates.asp includes an
example.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung




DaTurk said:
Well, the cast will work fine, that's not my problem. But if I have an
instance of D, and cast it to A, I'll only have acces to A's methods,
and not to any D specific methods. This is what I'm trying to fix, I
want to access D specific methods after casting it to A.
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.
 
Thank you for the response. I understand all of this, I
D implements C which inherits from A, as B also inherits from A.

I'm thiking about a possible solution, but I don't know how feasible it
is. Would it be possible to have some thing like this.

System.Type dType = typeof(D);

//At this point we'll only have access to A's methods, but I want
access to D's
//This is all basically so I can switch from client -> server where I
use this class
//without having to create a whole other class. As you can imagine,
the two interfaces
//B, and C metioned above are the client and the server.
A a = new D();

((dType."someway to get a class representation")a."one of D's methods"

Basically my solution involves having some general variable, I thought
a System.Type would work, that can can be swapped at run time to either
the client or the server, and then have "a" be cast to that, which
would be either the client or the server.

What do you guys think.

Greg said:
After you cast it to a D it will only have methods as a D .. in order to
access methods as if it were an A you wald have to upcast it to an A ...

A D could either be an A or a D, it doesn't know until runtime so by trying
to call a method from an A on a D may result in a failure (the D may not
actualyl be an A). There are ways to support this it is known as late
binding but C# does not do this by default ... You can use things like
dynamic methods to support this ..
http://www.codeproject.com/csharp/dynamicmethoddelegates.asp includes an
example.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung




DaTurk said:
Well, the cast will work fine, that's not my problem. But if I have an
instance of D, and cast it to A, I'll only have acces to A's methods,
and not to any D specific methods. This is what I'm trying to fix, I
want to access D specific methods after casting it to A.
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)

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.
 

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