473,480 Members | 4,852 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How Can I create an object instance from a string

I would like to create an instance of System.Windows.Form.TextBox
but I just have the name of the objec in a string like that:

string strType = "System.Windows.Form.TextBox";

I tryed myOgject = CreateInstance(Type.GetType(strType))
but it does not work because Type.GetType(strType) rturn null.

Does anybody can help me???

--
C# est a Mozart ce que
VB est a Normand L''amour
Jul 26 '06 #1
3 3413
Try this :
using System.Reflection;
//...
//...
string strTypeName = "System.Windows.Form.TextBox";
Object Obj = Activator.CreateInstance(strTypeName)
Steph
"TheSebaster" <Th*********@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...
I would like to create an instance of System.Windows.Form.TextBox
but I just have the name of the objec in a string like that:

string strType = "System.Windows.Form.TextBox";

I tryed myOgject = CreateInstance(Type.GetType(strType))
but it does not work because Type.GetType(strType) rturn null.

Does anybody can help me???

--
C# est a Mozart ce que
VB est a Normand L''amour

Jul 26 '06 #2
Hi,

IIRC the string being passed need to have more details about the type being
instantiated, this was the reason I never used it. I have used
AppDomain.CreateInstance and it does work great
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"TheSebaster" <Th*********@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...
>I would like to create an instance of System.Windows.Form.TextBox
but I just have the name of the objec in a string like that:

string strType = "System.Windows.Form.TextBox";

I tryed myOgject = CreateInstance(Type.GetType(strType))
but it does not work because Type.GetType(strType) rturn null.

Does anybody can help me???

--
C# est a Mozart ce que
VB est a Normand L''amour

Jul 26 '06 #3
The problem could be in that yoy have no assembly with TextBox loaded

use smth like this

string strType = "System.Windows.Forms.TextBox";
Assembly asm = Assembly.LoadWithPartialName("System.Windows.Forms ");
Type tp = asm.GetType(strType);
object obj = Activator.CreateInstance(tp);

PS: LoadWithPartialName is depricated in .net 2.0

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


"TheSteph" wrote:
Try this :
using System.Reflection;
//...
//...
string strTypeName = "System.Windows.Form.TextBox";
Object Obj = Activator.CreateInstance(strTypeName)
Steph
"TheSebaster" <Th*********@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...
I would like to create an instance of System.Windows.Form.TextBox
but I just have the name of the objec in a string like that:

string strType = "System.Windows.Form.TextBox";

I tryed myOgject = CreateInstance(Type.GetType(strType))
but it does not work because Type.GetType(strType) rturn null.

Does anybody can help me???

--
C# est a Mozart ce que
VB est a Normand L''amour


Jul 26 '06 #4

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

Similar topics

9
35911
by: Tian | last post by:
How can I create an instance of an object from a string? For example, I have a class Dog: class Dog: def bark(self): print "Arf!!!" I have a string: classname = "Dog"
8
3160
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
0
3614
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
4
16998
by: Ray | last post by:
I want to dynamically load DLLs (created from VB) and instantiate a class with a particular name, like "ProcessClass". I am able to load the DLL and confirm there is a class by that name BUT I...
2
2736
by: John Hughes | last post by:
Hi, Is it possible to create an object from a stored (database,xml or variable) name? For instance I have 2 class names stored in my database. Depending on the application settings I want to...
2
6544
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
4
12410
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
1
1326
geo039
by: geo039 | last post by:
Okay I have an application, which is to demonstrate the use of a created class. I have a previous and next button which cycles through the array. However I need the user to be able to create a new...
7
384
by: MisterPete | last post by:
How can I inherit from file but stil create an instance that writes to stdout? ----------- I'm writing a file-like object that has verbosity options (among some other things). I know I could just...
0
6584
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
0
7040
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,...
0
6905
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
7041
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
7080
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...
0
6908
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...
0
4478
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...
0
2994
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...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
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...

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.