473,670 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Promoting an object

Hi, I browsed for a related question/answer without luck, so here it goes:

Is there a known pattern that would allow me to promote an object of class A
to subclass B which inherits from A?
In other words, how can I turn a point into a circle or a dog into a
dalmatian? I know I can do the other way around with casting.

Thanks in advance.
--
P.J. Chan
Nov 17 '05 #1
5 3007
PJChan <PJ****@discuss ions.microsoft. com> wrote:
Hi, I browsed for a related question/answer without luck, so here it goes:

Is there a known pattern that would allow me to promote an object of class A
to subclass B which inherits from A?
Well, the object itself can never change type.
In other words, how can I turn a point into a circle or a dog into a
dalmatian? I know I can do the other way around with casting.


The normal way would be to provide a constructor in the derived class
which takes a reference to an instance of the base class, and creates a
new object based on it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
PJ,

Provided that Dalmatian inherits from Dog, and the declarations:

Dog dog;
Dalmatian dm;

Stating

dog = dm;

is OK, but the other way around you must use:

dm = (Dalmatian) dog;

or

dm = dog as Dalmatian;

Regards - Octavio
"PJChan" <PJ****@discuss ions.microsoft. com> escribió en el mensaje
news:32******** *************** ***********@mic rosoft.com...
Hi, I browsed for a related question/answer without luck, so here it goes:

Is there a known pattern that would allow me to promote an object of class
A
to subclass B which inherits from A?
In other words, how can I turn a point into a circle or a dog into a
dalmatian? I know I can do the other way around with casting.

Thanks in advance.
--
P.J. Chan

Nov 17 '05 #3
Thanks Jon,
the part of "creates a new object based on it" do you mean that it keeps a
private instance of the object or that somehow the constructor "returns" a
new instance of the subclass B based on the passed object (which is of class
A)?

I resolved the requirement temporarly using the former method, but would
really prefer to use the later. How do I construct the new instance? Some
form of copy or clone? Would you mind providing a simple example?

thks
Pedro
--
P.J. Chan
"Jon Skeet [C# MVP]" wrote:
PJChan <PJ****@discuss ions.microsoft. com> wrote:
Hi, I browsed for a related question/answer without luck, so here it goes:

Is there a known pattern that would allow me to promote an object of class A
to subclass B which inherits from A?


Well, the object itself can never change type.
In other words, how can I turn a point into a circle or a dog into a
dalmatian? I know I can do the other way around with casting.


The normal way would be to provide a constructor in the derived class
which takes a reference to an instance of the base class, and creates a
new object based on it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4
Thanks Octavio
following your second example I always get a Casting exception or a null (if
I use the "... as ...")

My understanding is that under C# you cannot typecast from class to subclass
just like that as private members would not necessarily be initialised
properly.

I don't mind using a construct like: objB = new SubClassB(objA)
but I don't know how to go about creating the new instance based on the
existing objA, and I don't know if it is possible that objB and objA share
everything that exists in class A but objB has the extensions supplied by
subclass B.

Thanks
Pedro

--
P.J. Chan
"Octavio Hernandez" wrote:
PJ,

Provided that Dalmatian inherits from Dog, and the declarations:

Dog dog;
Dalmatian dm;

Stating

dog = dm;

is OK, but the other way around you must use:

dm = (Dalmatian) dog;

or

dm = dog as Dalmatian;

Regards - Octavio
"PJChan" <PJ****@discuss ions.microsoft. com> escribió en el mensaje
news:32******** *************** ***********@mic rosoft.com...
Hi, I browsed for a related question/answer without luck, so here it goes:

Is there a known pattern that would allow me to promote an object of class
A
to subclass B which inherits from A?
In other words, how can I turn a point into a circle or a dog into a
dalmatian? I know I can do the other way around with casting.

Thanks in advance.
--
P.J. Chan


Nov 17 '05 #5
PJChan <PJ****@discuss ions.microsoft. com> wrote:
the part of "creates a new object based on it" do you mean that it keeps a
private instance of the object or that somehow the constructor "returns" a
new instance of the subclass B based on the passed object (which is of class
A)?
That's up to the implementation. If everything's done via properties,
it could be either - although you'd want to think of the ramifications
of what it meant to change the value of a property of the new object,
for instance.
I resolved the requirement temporarly using the former method, but would
really prefer to use the later. How do I construct the new instance? Some
form of copy or clone? Would you mind providing a simple example?


It really depends on what access the derived class has to the base
class. If the derived class has access to a sort of "copy constructor"
of the base class which just copies the values of all the fields from
one object into the new one, that would make things easier. Otherwise,
you may need to set the values of properties.

In general, it's something I try to avoid needing - does your design
absolutely depend on it?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

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

Similar topics

15
5945
by: Jim Newton | last post by:
hi all, does anyone know what print does if there is no __str__ method? i'm trying ot override the __repr__. If anyone can give me some advice it would be great to have. I have defined a lisp-like linked list class as a subclass of list. The __iter__ seems to work as i'd like, by traversing the links, and the __repr__ seems to work properly for somethings but not others. The basic idea is that a list such as is converted to ]],...
1
3221
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't understand is:
7
1514
by: socraticquest | last post by:
Hello, I need to set-up & promote a forum whose users would be small to medium-sized companies needing information, and posting responses regarding emerging developments in any industry & social trends. Are 'Commission Junction' and 'Google AdSense' good resources for website revenue generation? Any recommendations for resources useful in promoting websites & blogs?
0
4627
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't understand is:
26
5670
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized several parts of the DOM, but this does not include the window object. Thank you
0
1925
by: prac | last post by:
Promoting your site - Google Co-op Search Engine Use Google Co-op Search Engine, to have your own search engine that reflects your knowledge and interests. I have one on my Directory of Google Pages : http://philippe.chappuis.googlepages.com/gpc-pages.htm#cooperatif
3
2764
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions doesn't actually give any insight.
105
6165
by: Keith Thompson | last post by:
pereges <Broli00@gmail.comwrites: These types already have perfectly good names already. Why give them new ones? If you must rename them for some reason, use typedefs, not macros. --
0
8388
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,...
0
8907
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8817
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8593
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,...
0
8663
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7423
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5687
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
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2804
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

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.