PC Review


Reply
Thread Tools Rate Thread

How to create a sub-class based on some pre-determined flag.

 
 
Wolf
Guest
Posts: n/a
 
      8th Sep 2006
Say I have the following XML (as example)

<root>
<foo type="num">10</foo>
<foo type="date">2006-10-10</foo>
</root>

I now want to create at run-time a foo object but specifically a
specialisation of it based on the value of the "type" attribute (i.e.
some arbritary value).

What I do currently is,

foo f = null;
if (type == "num"){
f = new fooNum();
}
else if (type == "date"{
f = new fooDate();
}

but...... is there a cleaner way .. more OO?

Can I have some nice way of creating the right subClass based on the
value of the "type" attribute. The XML is just for example but really
what I'm looking for is the correct way of creating subClasses depending
on some condition so that you dont end up using big switch statements!

Ta in advance :-)
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      8th Sep 2006
Wolf wrote:

<snip>

> Can I have some nice way of creating the right subClass based on the
> value of the "type" attribute. The XML is just for example but really
> what I'm looking for is the correct way of creating subClasses depending
> on some condition so that you dont end up using big switch statements!


Look into Type.GetType and Activator.CreateInstance.

Jon

 
Reply With Quote
 
Wolf
Guest
Posts: n/a
 
      8th Sep 2006
* Jon Skeet [C# MVP] wrote:
> Wolf wrote:
>
> <snip>
>
>>Can I have some nice way of creating the right subClass based on the
>>value of the "type" attribute. The XML is just for example but really
>>what I'm looking for is the correct way of creating subClasses depending
>>on some condition so that you dont end up using big switch statements!

>
>
> Look into Type.GetType and Activator.CreateInstance.


Cheers!
 
Reply With Quote
 
=?Utf-8?B?TWF0dCBTYWxtb24=?=
Guest
Posts: n/a
 
      8th Sep 2006
Wolf,

You're probably not going to be able to avoid having a piece of custom
somewhere which says ' if the type is A then create fooNum, else create
fooDate'. However, if you're looking for the OO answer to this, I suggest you
look at the creational design patterns, such as the Factory Method, which
delegate responsibility for object creation.

Encapsulated within the factory object, you have a number of options for
creating an instance of your foo object, which Jon has already mentioned. You
could also look into reflection (perhaps storing the Type name to create in
the XML document, and generating the relevant type at runtime).

--
MCSD.NET, MBCS


"Wolf" wrote:

> Say I have the following XML (as example)
>
> <root>
> <foo type="num">10</foo>
> <foo type="date">2006-10-10</foo>
> </root>
>
> I now want to create at run-time a foo object but specifically a
> specialisation of it based on the value of the "type" attribute (i.e.
> some arbritary value).
>
> What I do currently is,
>
> foo f = null;
> if (type == "num"){
> f = new fooNum();
> }
> else if (type == "date"{
> f = new fooDate();
> }
>
> but...... is there a cleaner way .. more OO?
>
> Can I have some nice way of creating the right subClass based on the
> value of the "type" attribute. The XML is just for example but really
> what I'm looking for is the correct way of creating subClasses depending
> on some condition so that you dont end up using big switch statements!
>
> Ta in advance :-)
>

 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      8th Sep 2006
"Matt Salmon" <mattsalmon@REMOVE_THIS.hotmail.com> wrote in message
news:56087966-CA83-4621-B9D8-(E-Mail Removed)...
> Wolf,
>
> You're probably not going to be able to avoid having a piece of custom
> somewhere which says ' if the type is A then create fooNum, else create
> fooDate'. However, if you're looking for the OO answer to this, I suggest
> you
> look at the creational design patterns, such as the Factory Method, which
> delegate responsibility for object creation.


You can create a Dictionary of Factory delegates....

>
> Encapsulated within the factory object, you have a number of options for
> creating an instance of your foo object, which Jon has already mentioned.
> You
> could also look into reflection (perhaps storing the Type name to create
> in
> the XML document, and generating the relevant type at runtime).
>
> --
> MCSD.NET, MBCS
>
>
> "Wolf" wrote:
>
>> Say I have the following XML (as example)
>>
>> <root>
>> <foo type="num">10</foo>
>> <foo type="date">2006-10-10</foo>
>> </root>
>>
>> I now want to create at run-time a foo object but specifically a
>> specialisation of it based on the value of the "type" attribute (i.e.
>> some arbritary value).
>>
>> What I do currently is,
>>
>> foo f = null;
>> if (type == "num"){
>> f = new fooNum();
>> }
>> else if (type == "date"{
>> f = new fooDate();
>> }
>>
>> but...... is there a cleaner way .. more OO?
>>
>> Can I have some nice way of creating the right subClass based on the
>> value of the "type" attribute. The XML is just for example but really
>> what I'm looking for is the correct way of creating subClasses depending
>> on some condition so that you dont end up using big switch statements!
>>
>> Ta in advance :-)
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where should a boolean flag be located in the GUI class or controll class Tony Johansson Microsoft C# .NET 1 15th Feb 2011 02:03 PM
Set flag in inherited class when SerialPort.DataReceived fires? Len Weltman Microsoft C# .NET 0 29th Aug 2008 06:03 PM
Re: Creating duplicate rows of data based on a determined number of ti Jeff Boyce Microsoft Access External Data 5 26th Jan 2007 08:30 PM
Serializable Flag on a Class MrFile Microsoft ASP .NET 1 15th Jun 2006 10:33 PM
how to I create folders determined by message content as a rule? =?Utf-8?B?c3R1?= Microsoft Outlook VBA Programming 1 1st Oct 2004 07:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:28 PM.