471,326 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Extending objects at runtime

Hi,

I've been trying to figure this out for a while and can't find the answer.
For simplicity I will use a basic example rather than my real classes.

I have one base class, Item and various extended classes which inherit from
Item, ExtendedItem1, ExtendedItem2, etc. As expected, Item defines properties
and methods common to all items. The ExtendedItem classes define additional
properties and methods specific to different types of items.

At runtime, I create an instance of an Item. Depending on certain
conditions, I want to extend that Item into one of the ExtendedItem classes,
ie transform the Item object into an ExtendedItem1 or ExtendedItem2 object.

Is there an easy way to do this? Can I easily tell the ExtendedItem class to
use my already instanciated Item object as the base class? or do I have to
write a new constructor for each ExtendedItem class that accepts an Item
object and itializes all values in the base class to those in the Item object
parameter?

Any help is appreciated.

Thanks,

James
Nov 28 '05 #1
3 2199
As far as i know, you need to write a constructor for your Extended classes
that takes an instance of Item and sets the properties correctly as you
suggested. I don't think there's any other way to upcast. Though someone
else might know some workaround.

-Tim

"James" <re******@newsgroups.nospam> wrote in message
news:C8**********************************@microsof t.com...
Hi,

I've been trying to figure this out for a while and can't find the answer.
For simplicity I will use a basic example rather than my real classes.

I have one base class, Item and various extended classes which inherit
from
Item, ExtendedItem1, ExtendedItem2, etc. As expected, Item defines
properties
and methods common to all items. The ExtendedItem classes define
additional
properties and methods specific to different types of items.

At runtime, I create an instance of an Item. Depending on certain
conditions, I want to extend that Item into one of the ExtendedItem
classes,
ie transform the Item object into an ExtendedItem1 or ExtendedItem2
object.

Is there an easy way to do this? Can I easily tell the ExtendedItem class
to
use my already instanciated Item object as the base class? or do I have to
write a new constructor for each ExtendedItem class that accepts an Item
object and itializes all values in the base class to those in the Item
object
parameter?

Any help is appreciated.

Thanks,

James

Nov 28 '05 #2
> I have one base class, Item and various extended classes which inherit
from Item, ExtendedItem1, ExtendedItem2, etc. As expected, Item
defines properties and methods common to all items. The ExtendedItem
classes define additional properties and methods specific to different
types of items.

At runtime, I create an instance of an Item. Depending on certain
conditions, I want to extend that Item into one of the ExtendedItem
classes, ie transform the Item object into an ExtendedItem1 or
ExtendedItem2 object.


Use your Item class as a constructor argument to your ExtendedItem classes
as shown below:
public class MyClass
{
public static void Main()
{
Item myItem=new Item();
myItem.MyItemField="Hello world!";
ExtendedItem myExtendedItem=new ExtendedItem(myItem);
// Writes Hello World!
Console.WriteLine(myExtendedItem.MyItemField);
}
}

public class Item
{
public string MyItemField;
}
public class ExtendedItem : Item
{
public string MyExtendedItemField;
public ExtendedItem(Item item)
{
this.MyItemField=item.MyItemField;
}
}

Regards,
Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 28 '05 #3
Hi,

Check out my article, it might be helpful.

http://www.developersdex.com/gurus/articles/739.asp

Happy Coding,

Stefan
C# GURU
www.DotNETovation.com

"You always have to look beyond the horizon and can never be complacent
-- God forbid we become complacent."

Jozef Straus

*** Sent via Developersdex http://www.developersdex.com ***
Nov 30 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by NadaHombre | last post: by
2 posts views Thread by Henri Sivonen | last post: by
7 posts views Thread by A Traveler | last post: by
1 post views Thread by rdaunoravicius | last post: by
3 posts views Thread by Jeff | last post: by
7 posts views Thread by Maximus Decimus | last post: by
reply views Thread by J. Moreno | last post: by
3 posts views Thread by J. Moreno | last post: by

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.