473,511 Members | 16,738 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 3000
PJChan <PJ****@discussions.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.com>
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****@discussions.microsoft.com> escribió en el mensaje
news:32**********************************@microsof t.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****@discussions.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.com>
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****@discussions.microsoft.com> escribió en el mensaje
news:32**********************************@microsof t.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****@discussions.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.com>
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
5924
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...
1
3203
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...
7
1508
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 &...
0
4617
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...
26
5635
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...
0
1918
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...
3
2754
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? ...
105
6081
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
7355
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
7423
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...
1
7081
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
7510
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
4737
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...
0
3225
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...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.