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

Class name as a string variable

ADi
Hello,

I'm wondering it is possible somehow in C# to make that operation
possible:

abstract class ParentClass { };
class ClassA : ParentClass { };
class ClassB : ParentClass { };
class ClassC : ParentClass { };

ParentClass object; // declare an abstract object

string[] Classes = new String[] { "ClassA", "ClassB", "ClassC" };

object = new Classes[0](); // defining "ClassA" object
^^^^^^^^^^

....in other words, how to make object which name is given as string
variable? It's possible AT ALL?

Regards,
ADi

Nov 17 '05 #1
3 1444
You have to use reflection. Check out System.Reflection namespace. And also,
just do a search on Google. You will find tons of sample code.

"ADi" <ad****@spam.kmicic.net> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hello,

I'm wondering it is possible somehow in C# to make that operation
possible:

abstract class ParentClass { };
class ClassA : ParentClass { };
class ClassB : ParentClass { };
class ClassC : ParentClass { };

ParentClass object; // declare an abstract object

string[] Classes = new String[] { "ClassA", "ClassB", "ClassC" };

object = new Classes[0](); // defining "ClassA" object
^^^^^^^^^^

...in other words, how to make object which name is given as string
variable? It's possible AT ALL?

Regards,
ADi

Nov 17 '05 #2
ADi <ad****@spam.kmicic.net> wrote:
I'm wondering it is possible somehow in C# to make that operation
possible:

abstract class ParentClass { };
class ClassA : ParentClass { };
class ClassB : ParentClass { };
class ClassC : ParentClass { };

ParentClass object; // declare an abstract object

string[] Classes = new String[] { "ClassA", "ClassB", "ClassC" };

object = new Classes[0](); // defining "ClassA" object
^^^^^^^^^^

...in other words, how to make object which name is given as string
variable? It's possible AT ALL?


I think you're looking for Activator.CreateInstance and possibly
Type.GetType as well.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
ADi
Thnx for the clue, System.Reflection is exactly what I need... :)

thanks again ;)

Regards,
ADi.

Nov 17 '05 #4

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

Similar topics

2
by: Ovid | last post by:
Hi, I'm trying to determine the cleanest way to override class data in a subclass. class Universe { public String name; private static double PI = 3.1415; Universe(String name) {
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
4
by: Andres | last post by:
Hi all, I have the problem to assign a variable of type object to a specific class at runtime. I want to use a string variable that specify the class a want to set this object. Is something...
5
by: Jeff Cobelli | last post by:
I am trying to include two classes as members of another class in a webservice. The definitions look basically like this: Public Class clsLender Public ID As String Public Name As String End...
5
by: Rob | last post by:
In many articles related to VB.net the word "class" is used... How many meanings are there to this word ? "possible to derived a class from another" "forms are full-fledged classes" "base...
9
by: craig.overton | last post by:
All, I am currently developing an FTP class in VB.NET. It's kid tested, mother approved when trying to access an FTP Server on a Windows box meaning I can connect, run commands, upload and...
0
by: Andrew Brook | last post by:
Hi everyone, I've been going through an article i found on MSDN (http://msdn.microsoft.com/msdnmag/issues/02/12/WebServicesDesign/) concerning designing the messages for a webservice in a schema...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
6
by: jmarcrum | last post by:
Hi everyone! I'm using a super class (DVD.java) that handles another class (EnhancedDVD.java). I want to pass the "details" of the DVD into the super class DVD.java. The super class contains the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.