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

how to: instantiate object if only its name (e.g. "foo") is known

TR
At runtime, I'd like to instantiate an object (e.g. myInvoice) when all I
know is the name of the class that has to instantiated (e.g. "Invoice").

Dim DesiredClass as String = "Invoice"
'// now create an instance of the desired class
???

Can this be done?

I hope the question is clear.
Thanks
TR
Nov 21 '05 #1
3 1080
"TR" <tr@anonymous.biz> schrieb:
At runtime, I'd like to instantiate an object (e.g. myInvoice) when all I
know is the name of the class that has to instantiated (e.g. "Invoice").


You can use 'Activator.CreateInstance' to do that. Samples can be found
here (ignore the German text and take a look at the listings):

<URL:http://dotnet.mvps.org/dotnet/code/codingtechnique/#ClassByName>

--
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 #2
TR
Vielen Dank!
TR

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
"TR" <tr@anonymous.biz> schrieb:
At runtime, I'd like to instantiate an object (e.g. myInvoice) when all I know is the name of the class that has to instantiated (e.g. "Invoice").


You can use 'Activator.CreateInstance' to do that. Samples can be found
here (ignore the German text and take a look at the listings):

<URL:http://dotnet.mvps.org/dotnet/code/codingtechnique/#ClassByName>

--
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 #3
"TR" <tr@anonymous.biz> schrieb:
Vielen Dank!


:-)

--
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

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

Similar topics

1
by: whornak | last post by:
I am getting the value of an object to be instantiated from the database so all I have is the name of it stored in a string variable. How do I instaintiate the object using the variable?
11
by: Oleg Alistratov | last post by:
Is in javascript a method to get object's identifier as string? E.g., may I write function GetName thus, that var v; var s = GetName(v); has led to s == "v"
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
3
by: Isz | last post by:
I can do this easily enough in actionscript, but I would like to do it also in C#. I have 10 Image controls on the .aspx page. I want to create a loop that makes the first 'n' number of Images'...
3
by: George Varelas | last post by:
Hi. How can I change an object's name after I have created. For example I create an array as following "int mArray = new int" How can I change the name "mArray" and derive it from a combination...
9
by: Michael.Suarez | last post by:
void RaiseMyObjectsClickEvent(string btn_name) { //Raise btn_name.Click } Could someone please help me fill in the rest? Thanks,
7
by: multicherry | last post by:
Hi, Having searched for a way to fetch a window object by name, all I came across were answers along the line of... "All you have to do is say windowObj = window.open("blah", "name");" which...
2
by: Jon Paal [MSMD] | last post by:
trying to test these old guys on win xp pro sp2 http://support.microsoft.com/kb/249290 Sample ASP script can't instantiate object
3
by: =?Utf-8?B?Umljaw==?= | last post by:
How do I find the Key Name if a Value Name (String Name) is known? For example: I have a Value Name (String Name) "MyValueName" somewhere under the Key Name "SOFTWARE\\ATI". ATI has nested keys ...
12
by: Shannon Mayne | last post by:
I would like to create objects with algorithmically determined names based on other object names and use object names for general algorithm input. How would one extract the name of an object...
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
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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...
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...

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.