473,405 Members | 2,141 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,405 software developers and data experts.

Creating an object depending on a value of a parameter

Hi all,
I have a method which takes a string parameter. It takes a type name as a
string.
Inside the method, i want to create an instance of the type which is
mentioned in the string parameter.

Please help me providing with some example code.

Thanks,

Chamith
Nov 16 '05 #1
7 1095
Hi Chamith
If the set of the types you are reciving as input to this method is
finite , you can do it inside a switch statement
switch type{
case "int " : //create you int object
case "string"://create you string object
default : // not a recognized type , return an erro message

}
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #2
Chamith <ch********@gmail.com> wrote:
I have a method which takes a string parameter. It takes a type name as a
string.
Inside the method, i want to create an instance of the type which is
mentioned in the string parameter.

Please help me providing with some example code.


Look at the docs for Activator.CreateInstance. Sample code:

using System;

class Test
{
static void Main()
{
string typeName = "Foo";

Type t = Type.GetType (typeName);
object o = Activator.CreateInstance(t);
}
}

class Foo
{
public Foo()
{
Console.WriteLine ("Constructing instance of Foo");
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
If the type to be created is in the same assembly as the creating function then

object CreateIt(string typename)
{
Type t = Type.GetType(typename);
object o = Activator.CreateInstance(t);
return o;
}

if the type isn't in the same assembly (or in mscorlib) then you haven't got enough information to create the type

Regards

Richard Belwett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi all,
I have a method which takes a string parameter. It takes a type name as a
string.
Inside the method, i want to create an instance of the type which is
mentioned in the string parameter.

Please help me providing with some example code.

Thanks,

Chamith

Nov 16 '05 #4
Hi,

Thanks a lot for all your replies. Those are really usefull.
One more thing i got to ask.
Once the object is created, it's type is System.Object.
Since i need to get all the functionaly of the specific type i'm creating, i
need to cast it to that particular type explicitly.
but.. coz i dont know what the 'exact type' is until the runtime, is there a
way to do it?

want to do it like this.

Type t = Type.GetType(typename);
MySpecificType o = (MySpecificType)Activator.CreateInstance(t);

Thanks,

Chamith

"Chamith" <ch********@gmail.com> wrote in message
news:#o**************@TK2MSFTNGP10.phx.gbl...
Hi all,
I have a method which takes a string parameter. It takes a type name as a
string.
Inside the method, i want to create an instance of the type which is
mentioned in the string parameter.

Please help me providing with some example code.

Thanks,

Chamith

Nov 16 '05 #5
Use a common base class or interface and return that from the method

MyBase CreateIt(string typename)
{
object o = Activator.CreateInstance(Type.GetType(typename));
return (MyBase)o;
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

Thanks a lot for all your replies. Those are really usefull.
One more thing i got to ask.
Once the object is created, it's type is System.Object.
Since i need to get all the functionaly of the specific type i'm creating, i
need to cast it to that particular type explicitly.
but.. coz i dont know what the 'exact type' is until the runtime, is there a
way to do it?

want to do it like this.

Type t = Type.GetType(typename);
MySpecificType o = (MySpecificType)Activator.CreateInstance(t);

Thanks,

Chamith

Nov 16 '05 #6
Interesting article related to designing plug-in -based apps in .NET
http://www.divil.co.uk/net/articles/plugins/plugins.asp
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:Oo**************@TK2MSFTNGP15.phx.gbl...
Use a common base class or interface and return that from the method

MyBase CreateIt(string typename)
{
object o = Activator.CreateInstance(Type.GetType(typename));
return (MyBase)o;
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

Thanks a lot for all your replies. Those are really usefull.
One more thing i got to ask.
Once the object is created, it's type is System.Object.
Since i need to get all the functionaly of the specific type i'm creating,
i
need to cast it to that particular type explicitly.
but.. coz i dont know what the 'exact type' is until the runtime, is there
a
way to do it?

want to do it like this.

Type t = Type.GetType(typename);
MySpecificType o = (MySpecificType)Activator.CreateInstance(t);

Thanks,

Chamith

Nov 16 '05 #7
Hi,
As Dino said, use any "good-old" polymorphic technique like Interface
or BaseClass.

"Chamith" <ch********@gmail.com> wrote in message
news:uL**************@TK2MSFTNGP10.phx.gbl...
Hi,

Thanks a lot for all your replies. Those are really usefull.
One more thing i got to ask.
Once the object is created, it's type is System.Object.
Since i need to get all the functionaly of the specific type i'm creating, i need to cast it to that particular type explicitly.
but.. coz i dont know what the 'exact type' is until the runtime, is there a way to do it?

want to do it like this.

Type t = Type.GetType(typename);
MySpecificType o = (MySpecificType)Activator.CreateInstance(t);

Thanks,

Chamith

"Chamith" <ch********@gmail.com> wrote in message
news:#o**************@TK2MSFTNGP10.phx.gbl...
Hi all,
I have a method which takes a string parameter. It takes a type name as a string.
Inside the method, i want to create an instance of the type which is
mentioned in the string parameter.

Please help me providing with some example code.

Thanks,

Chamith


Nov 16 '05 #8

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

Similar topics

6
by: Chamith | last post by:
Hi all, I have a method which takes a string parameter. It takes a type name as a string. Inside the method, i want to create an instance of the type which is mentioned in the string parameter. ...
9
by: Murat Ozgur | last post by:
Hello, I want to know about "creating new objects without assignment to a reference" like this : ... new Employee("John","Woo"); .... Is this a good programming practice ? How does...
6
by: Chamith | last post by:
Hi all, I have a method which takes a string parameter. It takes a type name as a string. Inside the method, i want to create an instance of the type which is mentioned in the string parameter. ...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.