473,796 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to dyanamically instance a class from a string containing the class name?

In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?

Thanks for your Help

Earl

private void Button1_Click(o bject sender, System.EventArg s e)
{
container mycontainer = new container("foo" );
ArrayList fooArrayList = mycontainer.Get ListOfObjects() ;
}

public class container
{
public string ClassName;
public container(objec t ClassName)
{
this.ClassName= ClassName;
}

public ArrayList GetListOfObject s()
{
ArrayList myArrayList = new ArrayList();
for(int i=1;i<10;i++)
{
//Convert.ToClass (ClassName) myClass = new Convert.ToClass (ClassName)
//myArrayList.Add (myClass);
}
return myArrayList;
}
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}
}
Nov 15 '05 #1
4 1697
Earl Teigrob <ea******@hotma il.com> wrote:
In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?


Look at Type.GetType, Assembly.GetTyp e and Activator.Creat eInstance.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi,

Try Activator.Creat eInstance( );

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Earl Teigrob" <ea******@hotma il.com> wrote in message
news:O1******** ******@tk2msftn gp13.phx.gbl...
In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?

Thanks for your Help

Earl

private void Button1_Click(o bject sender, System.EventArg s e)
{
container mycontainer = new container("foo" );
ArrayList fooArrayList = mycontainer.Get ListOfObjects() ;
}

public class container
{
public string ClassName;
public container(objec t ClassName)
{
this.ClassName= ClassName;
}

public ArrayList GetListOfObject s()
{
ArrayList myArrayList = new ArrayList();
for(int i=1;i<10;i++)
{
//Convert.ToClass (ClassName) myClass = new Convert.ToClass (ClassName)
//myArrayList.Add (myClass);
}
return myArrayList;
}
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}
}

Nov 15 '05 #3
Thanks for the replays

I have got it this far from what I have read but gettype is returning a null
value in the following code

private void Button1_Click(o bject sender, System.EventArg s e)
{
Type t = Type.GetType("f oo"); //returns a null value
object classInstance = Activator.Creat eInstance(t); //blows up because t
is null
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}

"Earl Teigrob" <ea******@hotma il.com> wrote in message
news:O1******** ******@tk2msftn gp13.phx.gbl...
In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?

Thanks for your Help

Earl

private void Button1_Click(o bject sender, System.EventArg s e)
{
container mycontainer = new container("foo" );
ArrayList fooArrayList = mycontainer.Get ListOfObjects() ;
}

public class container
{
public string ClassName;
public container(objec t ClassName)
{
this.ClassName= ClassName;
}

public ArrayList GetListOfObject s()
{
ArrayList myArrayList = new ArrayList();
for(int i=1;i<10;i++)
{
//Convert.ToClass (ClassName) myClass = new Convert.ToClass (ClassName)
//myArrayList.Add (myClass);
}
return myArrayList;
}
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}
}

Nov 15 '05 #4
You need to use the full name of the type: GetType("yourNa mespace.foo")

--
Michael R
cr**@tampabay.r r.com

"Earl Teigrob" <ea******@hotma il.com> wrote in message
news:O7******** *****@tk2msftng p13.phx.gbl...
Thanks for the replays

I have got it this far from what I have read but gettype is returning a null value in the following code

private void Button1_Click(o bject sender, System.EventArg s e)
{
Type t = Type.GetType("f oo"); //returns a null value
object classInstance = Activator.Creat eInstance(t); //blows up because t is null
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}

"Earl Teigrob" <ea******@hotma il.com> wrote in message
news:O1******** ******@tk2msftn gp13.phx.gbl...
In the exampe below, the constructor of the container class is passed the string called "foo". Within the container class, I would like to create an instance(s) of foo and add them to the ArrayList that is returned. (Once I figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?

Thanks for your Help

Earl

private void Button1_Click(o bject sender, System.EventArg s e)
{
container mycontainer = new container("foo" );
ArrayList fooArrayList = mycontainer.Get ListOfObjects() ;
}

public class container
{
public string ClassName;
public container(objec t ClassName)
{
this.ClassName= ClassName;
}

public ArrayList GetListOfObject s()
{
ArrayList myArrayList = new ArrayList();
for(int i=1;i<10;i++)
{
//Convert.ToClass (ClassName) myClass = new Convert.ToClass (ClassName) //myArrayList.Add (myClass);
}
return myArrayList;
}
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}
}


Nov 15 '05 #5

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

Similar topics

6
7746
by: Andre Meyer | last post by:
Hi all I have been searching everywhere for this, but have not found a solution, yet. What I need is to create an object that is an instance of a class (NOT a class instance!) of which I only know the name as a string. This what I tried: class A:
4
3916
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
6
22536
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
3
4235
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page that needs the class module I get an error on web site: Object reference not set to an instance of an object Here is where the error is:
4
1465
by: Mark | last post by:
I'd like to take an instance of a class and store it in a database. I've marked my class as and am using the binary formatting code similar to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconserializationconcepts.asp, although I'd be happy to use something else. What method should be used to store this serialized class in a SQL Server database? Likewise, what method should be used to recreate this...
14
9922
by: Jordan Marr | last post by:
I have the following class: class ProvisionCollection { ... private int m_VarianceCount; public int VarianceCount { get { return m_VarianceCount; }
3
1637
by: pOwner | last post by:
I'm trying to create a pointer to a class in form1.h button1 event handler. It should be simple... but no. This example is as simple as I can make to illustrate the problem: Form1.h (produced with the designer containing a single button) *********************************************************************************** #pragma once namespace MyProject {
5
1456
by: RSH | last post by:
Hi, I have a situation where I have multiple objects created from concrete classes: // Concrete implementations of the Abstract class ApprovalChain MarketingApprovalChain MktgAppChain = new MarketingApprovalChain(); AccountingApprovalChain AcctAppChain = new AccountingApprovalChain(); I also have a Department enum
45
3022
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class clsData Private m_objSQLClient As clsSQLClient Private m_objUsers As clsUsers
0
9685
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
10467
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
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
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...
0
5454
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
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
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.