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

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 2308
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
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.