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

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(object sender, System.EventArgs e)
{
container mycontainer = new container("foo");
ArrayList fooArrayList = mycontainer.GetListOfObjects();
}

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

public ArrayList GetListOfObjects()
{
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 1678
Earl Teigrob <ea******@hotmail.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.GetType and Activator.CreateInstance.

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

Try Activator.CreateInstance( );

Cheers,

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

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:O1**************@tk2msftngp13.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(object sender, System.EventArgs e)
{
container mycontainer = new container("foo");
ArrayList fooArrayList = mycontainer.GetListOfObjects();
}

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

public ArrayList GetListOfObjects()
{
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(object sender, System.EventArgs e)
{
Type t = Type.GetType("foo"); //returns a null value
object classInstance = Activator.CreateInstance(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******@hotmail.com> wrote in message
news:O1**************@tk2msftngp13.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(object sender, System.EventArgs e)
{
container mycontainer = new container("foo");
ArrayList fooArrayList = mycontainer.GetListOfObjects();
}

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

public ArrayList GetListOfObjects()
{
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("yourNamespace.foo")

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

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:O7*************@tk2msftngp13.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(object sender, System.EventArgs e)
{
Type t = Type.GetType("foo"); //returns a null value
object classInstance = Activator.CreateInstance(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******@hotmail.com> wrote in message
news:O1**************@tk2msftngp13.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(object sender, System.EventArgs e)
{
container mycontainer = new container("foo");
ArrayList fooArrayList = mycontainer.GetListOfObjects();
}

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

public ArrayList GetListOfObjects()
{
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
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...
4
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
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...
3
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...
4
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...
14
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
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...
5
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...
45
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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...
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.