473,379 Members | 1,511 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,379 software developers and data experts.

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

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 :-)
Sep 8 '06 #1
4 1106
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

Sep 8 '06 #2
* 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! :)
Sep 8 '06 #3
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 :-)
Sep 8 '06 #4
"Matt Salmon" <mattsalmon@REMOVE_THIS.hotmail.comwrote in message
news:56**********************************@microsof t.com...
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 :-)

Sep 8 '06 #5

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

Similar topics

5
by: Eskimo Joe | last post by:
I am trying to create a desktop icon using VB6. is this possible? -p
8
by: Raymond H. | last post by:
Hello, 1- How to see, in a Label, the URL of a link that the mouse pass over? (in the WebBrower control in a vb projet). 2- How to create a menu and a submenu via a button Command1? and...
3
by: anon | last post by:
I have been used to using DAO in the past, and then converted to ADO. Now I am having to use VB.Net(2000) and ADO.NET and am experiencing difficulties with the creation and population of an mdb....
3
by: martin | last post by:
Hi, this is going to sound amazingly stupid but I have to ask. whenever I create a new webform in visual studio, it always wants to save the form in the root directory. This makes it hard to...
4
by: Adam - Regus | last post by:
I'm trying to create a button that prints 2 copies of a form. Using the wizard, I created a button that prints one automatically, using the following code: End Sub Private Sub...
8
by: barb | last post by:
So that the world at large benefits from our efforts, here is one fully documented way to use Windows Irfanview freeware to create thumbnail web galleries (http://www.irfanview.com). STEP 1:...
4
by: Abdhul Saleem | last post by:
Hi, I am recieving error ActiveX component can't create object in the following line in the asp page. set ExcelApp = CreateObject("Excel.Application") Previously this code was working fine....
11
by: cmdolcet69 | last post by:
I have the code below that will evaluate a string of characters. If its less then 12 long it will trigger a message box if its equal it will save the information and close the form. I have this...
4
by: Robert | last post by:
Greetings New to Visual Studio 2008. Always us VB6.0 SP5. I am trying to copy files from A to B. I select a directory, and copy files. Works ok, but it does not create the directory first. ...
0
by: Dale | last post by:
Access 2000 I have a check scanner from Magtek, (MicrImage) This is like a "point of sale" check scanner/credit card reader. Magtek furnished 2 ocx's (MTMicrImage.ocx & SaxComm8.ocx). They also...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.