473,693 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2299
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******@newsg roups.nospam> wrote in message
news:C8******** *************** ***********@mic rosoft.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.MyItemFi eld="Hello world!";
ExtendedItem myExtendedItem= new ExtendedItem(my Item);
// Writes Hello World!
Console.WriteLi ne(myExtendedIt em.MyItemField) ;
}
}

public class Item
{
public string MyItemField;
}
public class ExtendedItem : Item
{
public string MyExtendedItemF ield;
public ExtendedItem(It em item)
{
this.MyItemFiel d=item.MyItemFi eld;
}
}

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5615
by: NadaHombre | last post by:
Hello, If I want to make a class which is a stack of objects of some specific type (not just "Objects"), what is the best way to do it? I wonder if there is some generally accepted "best" way to do this, because it must come up a lot. I personally create a new class which mimics all the methods of java.util.Stack , and keeps a java.util.Stack as a private instance variable. The new class changes the return types of pop and peek and...
2
1668
by: Henri Sivonen | last post by:
In order to keep subsequent code clear, I added a new method like this HTMLDivElement.prototype.foo = function(arg) { // foo for HTML div trees } Element.prototype.foo = function(arg) { // foo for XML tree retrieved using XMLHttpRequest }
7
1635
by: A Traveler | last post by:
Hello all, i was just curious if anyone whos been playing with VS2005 could tell me... In javascript (and java??) you can alter the prototypes for an object in your project. I dont remember the syntax exactly, but basically you do something like: function String.prototype.mySplit(myArgs){...do something...}
1
1555
by: rdaunoravicius | last post by:
Hi, Let's say you have a bunch of instatiated objects of the same class on your hands and you want to had some functionality to them. I'm facing this situation while working with PyGTK and libglade to create a GUI. Libglade creates a whole object tree representing the GUI out of an XML file, and a bunch of GtkComboBox objects are instantiated. I don't like the way GtkComboBox objects works, so I'd like them to have some extra methods....
3
1887
by: Jeff | last post by:
Hey ASP.NET 2.0 I'm trying to extend the MembershipUser class, and have encounter a problem: << See in the middle of this post for info about why I do this >> << See below of this post for the source code of Contact class >> public class Contact : MembershipUser
7
3384
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced python developers. II want to add some more KEYWORDS and DATATYPES into the python script apart from the existing ones. It would be really great if anybody could guide me as which files and
0
2109
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call Register_Handler(...). I thought there was some magic here that was assigning the pointer *Handler to my python...
0
761
by: J. Moreno | last post by:
Hello, I'm trying to add an extension to Object using VB. It shows up in the intellisense for other kinds of reference objects, but not if the variable is simply declared as Object. It works without problems when I use C#, but not from within VB...any ideas as to what is going on? Imports System.Runtime.CompilerServices
3
1300
by: J. Moreno | last post by:
Since nobody seems to know the answer in the vb group, guess I'll ask here... Does anyone know why I would be able to create an extension method for Object in C# but not in VB? I understand why using ByRef for the first parameter would work in VB but not C#, but this I just don't get at all... J. Moreno <planb@newsreaders.comwrote:
0
8552
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8816
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6479
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5825
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4332
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4567
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2992
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.