473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Dynamic object creation.

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.

Aug 4 '06 #1
6 4075
DaTurk <mm******@hotmail.comwrote:
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.
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Aug 4 '06 #2
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:
DaTurk <mm******@hotmail.comwrote:
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.
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Aug 4 '06 #3
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]
- mv*@spam.guard.caspershouse.com

"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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:
>DaTurk <mm******@hotmail.comwrote:
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.
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Aug 4 '06 #4
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.

Nicholas Paldino [.NET/C# MVP] wrote:
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]
- mv*@spam.guard.caspershouse.com

"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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:
DaTurk <mm******@hotmail.comwrote:
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.
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Aug 4 '06 #5
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/dy...ddelegates.asp includes an
example.

Cheers,

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


"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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.

Nicholas Paldino [.NET/C# MVP] wrote:
>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]
- mv*@spam.guard.caspershouse.com

"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googleg roups.com...
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:
DaTurk <mm******@hotmail.comwrote:
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.
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Aug 4 '06 #6
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 Young wrote:
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/dy...ddelegates.asp includes an
example.

Cheers,

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


"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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.

Nicholas Paldino [.NET/C# MVP] wrote:
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]
- mv*@spam.guard.caspershouse.com

"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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:
DaTurk <mm******@hotmail.comwrote:
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.
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Aug 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DotNetJunkies User | last post by:
Hie, I create a dynamique HtmlTable, in each cell of this HtmlTable put a new control ( dropdownlist,label,..) and then want to create handler so that if i change the select item in the dop downlist...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
5
by: Tompa | last post by:
Hi, I would like to create images on the fly as a response to an http request. I can do this with PIL like this (file create_gif.py): from PIL import Image, ImageDraw print 'Status: 200 OK'...
15
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
0
by: arthernan | last post by:
If I separate the code into two buttons on a web page it works. One button creates the webpart and the second creates the connection. But the minute I move the connection creation code to the web...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
2
by: Ron M. Newman | last post by:
Hi, Just need a little advice. Id like to build *dynamic* context menus for tree nodes. I'm pretty versed in building context menus and attaching them to tree nodes. My question is, what...
1
by: cdmsenthil | last post by:
I have an Infragistics UltrawebGrid . Each Row in the grid is attached to a context menu using Infragistics CSOM Upon click on the menu, I am creating an Iframe dynamically which points to...
0
by: tina2626 | last post by:
I m using this code in C#.net, for dynamic creation of GridView without using DB. <CODE> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.