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

Creating an object with a variable

OK, this may be simple, but...

I have a 5 classes, they all bring up a form to edit a table, and
they're all derivied from a virtual class. The user is allowed to pick
a table to edit from a drop-down list that was populated from a SQL
table, this table also contains the name of the class that is used to
edit it. Can I use 1 new statment to create the correct object or do I
have to use a switch?

Thanks,
Dave
Jul 24 '06 #1
3 1260
Dave,

You could use a switch, but in reality, you will want to look at the
static GetType method on the Type class. With this, assuming you have a
fully qualified type name, you can get the type that you have stored in the
database. Then, you can pass that Type instance to the static
CreateInstance method on the Activator class, and it will return an instance
of that type.

This assumes that they all have the same constructor of course
(parameterless, or same number/type of arguments).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DaveD" <de****@noemail.noemailwrote in message
news:u2********************************@4ax.com...
OK, this may be simple, but...

I have a 5 classes, they all bring up a form to edit a table, and
they're all derivied from a virtual class. The user is allowed to pick
a table to edit from a drop-down list that was populated from a SQL
table, this table also contains the name of the class that is used to
edit it. Can I use 1 new statment to create the correct object or do I
have to use a switch?

Thanks,
Dave

Jul 24 '06 #2
I would recommend using the factory pattern to encapsulat the logic.

You can either use a switch or you can use Activator.CreateInstance() (or
more correctly you could include the assembly and use
Assembly.CreateInstance as the assembly that the type is in may have not
been loaded yet :)

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"DaveD" <de****@noemail.noemailwrote in message
news:u2********************************@4ax.com...
OK, this may be simple, but...

I have a 5 classes, they all bring up a form to edit a table, and
they're all derivied from a virtual class. The user is allowed to pick
a table to edit from a drop-down list that was populated from a SQL
table, this table also contains the name of the class that is used to
edit it. Can I use 1 new statment to create the correct object or do I
have to use a switch?

Thanks,
Dave

Jul 24 '06 #3
Thanks, I knew that had to be a way of doing it.

Dave
On Mon, 24 Jul 2006 14:27:58 -0400, DaveD <de****@noemail.noemail>
wrote:
>OK, this may be simple, but...

I have a 5 classes, they all bring up a form to edit a table, and
they're all derivied from a virtual class. The user is allowed to pick
a table to edit from a drop-down list that was populated from a SQL
table, this table also contains the name of the class that is used to
edit it. Can I use 1 new statment to create the correct object or do I
have to use a switch?

Thanks,
Dave
Jul 25 '06 #4

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

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
1
by: Doug | last post by:
I am attempting to create a unit test that can switch between calling a method from the web service I created or calling the component that the web service actually calls. So in my unit test...
8
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
10
by: connyledin | last post by:
Im trying to create a version of the game Wumpus. Mine is called Belzebub. But im STUCK! And its due tuesday 2 maj. Im panicing! Can some one help me?? here is the file:...
6
by: wcc | last post by:
Hello, How do I create a class using a variable as the class name? For example, in the code below, I'd like replace the line class TestClass(object): with something like class...
2
by: Moses | last post by:
Hi All, Is is possible to catch the error of an undefined element while creating an object for it. Consider we are not having an element with id indicator but we are trying to make the object...
4
by: =?Utf-8?B?VG9kZCBKYXNwZXJz?= | last post by:
Hey guys, Is there ANY way to accomplish this: (see below)? Basically, I want to have a loop (a < 3 is just for testing purposes, it will be an underermined amount). In this loop, I want to be...
15
by: mark.norgate | last post by:
Hello I want to create a reference to an object, so that changes to the referenced object are reflected in the other object. Like this: object o = 123; object p = o; o = 456;
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.