473,698 Members | 2,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1269
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.co m

"DaveD" <de****@noemail .noemailwrote in message
news:u2******** *************** *********@4ax.c om...
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.Creat eInstance() (or
more correctly you could include the assembly and use
Assembly.Create Instance 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.c om...
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
40629
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* just created a new pointer to the original value.... For example I wanted to initialize a list of empty lists.... a=, , , , ] I thought there has to be a *really* easy way of doing it - after a
1
1376
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 form I have a checkbox that I can use to "turn on or off" which one I would call and I have code something like this: if (chkUseWs.Checked == true) { WebReference.WebClass o = new WebReference.WebClass();
8
3993
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 ID=MyID
8
5964
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; this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_ApplicantName", dataset.Tables.Columns.DataType, 50,
10
2085
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: http://esnips.com/webfolder/b71bfe95-d363-4dd3-bfad-39999a9e36d0 What i have the biggest problems with now is between line 8 and 23. How i can move the character trough the game. Other parts of the game that have with the movement to do is between line 83-114 and...
6
1782
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 eval(className) (object):
2
4933
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 for it indicator = document.getElementById('indicator');
4
1590
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 able to create a new cell each time. In this cell, I want the variable name to change. Is this at ALL possible? I've never been able to figure this out in some of the other languages. Any help would GREATLY be appreciated.
15
2082
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
1947
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, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { switch (((Sale)e.Row.DataItem).SzPN) {
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7740
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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 we have to send another system

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.