473,406 Members | 2,371 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,406 software developers and data experts.

Change Object Type at Runtime

I'm trying to do something, not sure if it's possible or even if it
makes sense, but I figured I'd throw it out there.

I have a class OrderSummary that contains information about an order. I
then have two classes that inherit OrderSummary; K_OrderSummary and
M_OrderSummary which are specific types of orders. I will not know what
type of order it is until runtime so I want to create an object of type
OrderSummary. I only want to have to choose the type once for each
order (at runtime).

for example:

OrderSummary o;

//this will be done once, when the object is created, then the rest of
the code can just use type OrderSummary
if(type.Equals("k"){
o = new K_OrderSummary();
}
else{
o = new M_OrderSummary();
}

private void DoSomethingToOrder(OrderSummary order){
order.Something(); //this would call M_OrderSummary.Something() or
K_OrderSummary.Something() depending on what type of order it is.
}

Essentially, I want to use inheritance, but be able to declare the
object as the parent type, but use the child functions.

It would be similar to doing this:

Object o = new Form();
and then expect to be able to use o as if it were a Form object. The
declaration works, because Form inherits Object, but I of course do not
have access the the Form functions.

Make any sense?

Thanks,
Yohaas

Nov 17 '05 #1
6 8262
Well, after that long winded questions - another two minutes of
serching yielded the answer: virtual.

It's easy, just make the function virtual in the base class, then
override it in the child class.
Yohaas

Nov 17 '05 #2
Makes perfect sense. What you want is something called "polymorphism,"
one of the basic concepts behing object oriented programming. If you
Google on "polymorphism" you'll find lots of information about it, in
general.

OrderSummary o;
if(type.Equals("k"){
o = new K_OrderSummary();
}
else{
o = new M_OrderSummary();
}

What you're really doing in the above code is implementing something
called a "Factory Method". I suggest going the whole distance and
making a method in OrderSummary that looks like this:

public static OrderSummary Factory(string summaryType)
{
if (summaryType == "k")
{
return new K_OrderSummary();
}
else
{
return new M_OrderSummary();
}
}

(and then improve it even further by making constants in OrderSummary
for "k" and "m").

Your Something() method you should implementing using an "override",
like this:

public abstract class OrderSummary
{
public abstract void Something();
}

public class K_OrderSummary : OrderSummary
{
public override void Something()
{
... do something for K order summary..
}
}

public class M_OrderSummary : OrderSummary
{
public override void Something()
{
... do something for M order summary..
}
}

then, when you have code that looks like this:

OrderSummary os = OrderSummary.Factory("k");
os.Something();

it will invoke the K_OrderSummary.Something() method polymorphically.

Nov 17 '05 #3
Bruce,
Thanks for the help - looks like a good solution too. I think I am
going to stick with the virtual methods though, since making abstract
methods in OrderSummary would prevent me from having non-abstract
methods. There are many common functions that I want to keep in
OrderSummary, and only override them when necessary.

Thanks again.
Yohaas

Nov 17 '05 #4
Whether OrderSummary should be abstract or not depends upon your
design. Does it make any sense to create an OrderSummary that is
neither a K_OrderSummary nor an M_OrderSummary? That is, can you say

OrderSummary os = new OrderSummary();

all by itself and it makes sense? If the answer is yes, then it should
_not_ be abstract. If the answer is "No, every order summary must be
either a K_OrderSummary or an M_OrderSummary" then it _should_ be
abstract.

Making a class abstract does _not_ stop you from adding non-abstract
methods to it. Declaring a class abstract means only two things:

1. The class has at least one method or property that is not defined in
the class (only declared), and must be defined by any child class that
you want to be able to instantiate (in your case, all child classes).
The class may have lots of non-abstract methods, but it has to have at
least one abstract method or class.

2. You cannot create an instance of the class itself, only instances of
any child classes that implement the stuff that you said was
"abstract".

Now, one reason why you might want Something() to not be abstract in
OrderSummary is if you want to provide a default implementation of it:
if it makes sense that there is a "generic" Something() that gets done
if a child class doesn't bother defining it. In that case you want to
declare it simply "virtual" and then override it in those child classes
that need behvaiour different from the default.

Nov 17 '05 #5
yohaas wrote:
Bruce,
Thanks for the help - looks like a good solution too. I think I am
going to stick with the virtual methods though, since making abstract
methods in OrderSummary would prevent me from having non-abstract
methods. [Of course you can have non-abstract methods with abstract methods in
the abstract class, actually, it's a better approach if you don't want
the OrderSummery created by declaring it abstract. So people using your
class can only declare K_OrderSummery or M_OrderSummery, not OrderSummery).

John
There are many common functions that I want to keep in OrderSummary, and only override them when necessary.

Thanks again.
Yohaas

Nov 17 '05 #6
Bruce and John,

Thanks to both of you - I had some misconceptions about abstract
classes. I will probably go back and make OrderSummary abstract, as
order will always be K or M.

Thanks again for the help.
Yohaas

Nov 17 '05 #7

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

Similar topics

1
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...
0
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...
5
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any assistance gratefully...
0
by: Matt S | last post by:
Hello, I'm trying to build a C# client to consume an AXIS Web Service (running SOAP over HTTP). The Web Service encodes full server-side exception traces in the Soap Fault > Detail element...
3
by: Andy | last post by:
If I use Visual Studio.Net 2003 to generate "web references" for clients using either sproxy.exe (for C++) client or wsdl.exe (for C#) clients, the web service endpoint gets "baked" into the...
0
by: hfleong | last post by:
I am trying to change the Active Directory password using C#. However, it seems to me it does not really works. I wonder what is wrong with my code. Hope somene can help me with this and thanks in...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
6
by: William | last post by:
for example, I have a global object: extern Object myobj; Can gcc get this object by using string "myobj" at runtime?? I know C++ rtti doesnt support this, but I dont know if gcc can , ...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.