473,511 Members | 16,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating an object using a type variable

Hi
I need to create an object of a class using a type variable.
for example, instead of writing:
X = new MyClass
I want to write something like:
Dim t as Type = GetType(MyClass)
X = CreateObjectFromType(t)

is this possible?
Nov 21 '05 #1
4 2312
Yes:

X = System.Activator.CreateInstance(t)

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
"Ramez" <ra***@newsgroup.nospam> escribió en el mensaje
news:uY**************@tk2msftngp13.phx.gbl...
Hi
I need to create an object of a class using a type variable.
for example, instead of writing:
X = new MyClass
I want to write something like:
Dim t as Type = GetType(MyClass)
X = CreateObjectFromType(t)

is this possible?

Nov 21 '05 #2
Yes:

X = System.Activator.CreateInstance(t)

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
"Ramez" <ra***@newsgroup.nospam> escribió en el mensaje
news:uY**************@tk2msftngp13.phx.gbl...
Hi
I need to create an object of a class using a type variable.
for example, instead of writing:
X = new MyClass
I want to write something like:
Dim t as Type = GetType(MyClass)
X = CreateObjectFromType(t)

is this possible?

Nov 21 '05 #3
"Ramez" <ra***@newsgroup.nospam> schrieb:
I need to create an object of a class using a type variable.
for example, instead of writing:
X = new MyClass
I want to write something like:
Dim t as Type = GetType(MyClass)
X = CreateObjectFromType(t)

is this possible?


Yes:

\\\
Dim b As Button = _
DirectCast(Activator.CreateInstance(GetType(Button )), Button)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4
"Ramez" <ra***@newsgroup.nospam> schrieb:
I need to create an object of a class using a type variable.
for example, instead of writing:
X = new MyClass
I want to write something like:
Dim t as Type = GetType(MyClass)
X = CreateObjectFromType(t)

is this possible?


Yes:

\\\
Dim b As Button = _
DirectCast(Activator.CreateInstance(GetType(Button )), Button)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

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

Similar topics

1
1369
by: Doug | last post by:
I am attempting to create a unit test that can switch between calling a method from the web service I created or calling the component that the web service actually calls. So in my unit test...
8
5955
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
1
227
by: Irfan | last post by:
Hi all I am new to DotNet and C# Is there any way to get a type of object from a string. Lets suppose i have a string variable containing a type "MyNameSpace.MyComponent" where MyComponent.dll...
1
1606
by: Danielb | last post by:
I need to create a read-only copy of Object X at run time, I know how to find the type of Object X (using GetType) but how do I go about creating a readonly copy of X? What I want to do is this:...
4
2510
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
26
5635
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
16
2333
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
6
1769
by: wcc | last post by:
Hello, How do I create a class using a variable as the class name? For example, in the code below, I'd like replace the line class TestClass(object): with something like class...
15
2071
by: mark.norgate | last post by:
Hello I want to create a reference to an object, so that changes to the referenced object are reflected in the other object. Like this: object o = 123; object p = o; o = 456;
19
1911
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
0
7242
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
7418
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
5662
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
4737
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
3222
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...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
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.