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

Create object through classname

Hi,

I want to create an instance of a class by means of the classname. Can't figure out how.

in pseudocode this would look like:

Dim MyClassName as String = "MyClass"
Dim MyObject as New MyClasses(MyClassName)

Thanks for your help,

Egbert
Feb 20 '06 #1
8 4692
"Bert" <ed*******@aequor.nl> schrieb
Hi,

I want to create an instance of a class by means of the classname.
Can't figure out how.

in pseudocode this would look like:

Dim MyClassName as String = "MyClass"
Dim MyObject as New MyClasses(MyClassName)

I wonder why people need the class name at runtime, but if it's necessary,
have a look at System.Activator.CreateInstance
Armin

Feb 20 '06 #2
Egbert,

Do you have a goal with this thousand times by newbies asked questions who
want to convert VBA functions to VBNet?

Be aware that VBNet does beside using it for giving debugging informations
nothing with Object names, it is just for you to make it readable.

Cor
Feb 20 '06 #3
Hi Egbert,

As Armin has said, you can use the System.Activator.CreateInstance methods
to create an instance, but be aware that this pseudo-code:

Dim MyClassName as String = "MyClass"
Dim MyObject as New MyClasses(MyClassName)

is not very robust. If the name of the type "MyClass" changes and you don´t
update that string, your code will fail at run-time. A more common approach
is to create the instance from the type:
System.Activator.CreateInstance(type), and you can get the type through
several ways. For example, you can get the types of an assembly with
Assembly.GetTypes, or only the public types with Assembly.GetExportedTypes.
You can examine some properties (the bases or implemented interfaces) of the
type to guess which ones you want to create instances from. This is quite
common when creating plug-in frameworks.

Bottom line: if you know what you are doing, OK, but if not you can think
about the above.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Bert" <ed*******@aequor.nl> escribió en el mensaje
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi,

I want to create an instance of a class by means of the classname. Can't
figure out how.

in pseudocode this would look like:

Dim MyClassName as String = "MyClass"
Dim MyObject as New MyClasses(MyClassName)

Thanks for your help,

Egbert
Feb 20 '06 #4
Thanks Armin, Carlos and Cor,

I do have a goal with this question, I'm using a form that uses objects from different classes, depending on user's input. So in order to create the objects I need this code:

Function CreateObject("ClassName" as String) as Object
dim oType as Type = Type.GetType("ClassName")
Return Activator.CreateInstance(oType)
End Function

No worries about changing names, since everything is decribed and maintained in the databases.

Egbert

"Armin Zingler" <az*******@freenet.de> wrote in message news:e$**************@TK2MSFTNGP14.phx.gbl...
"Bert" <ed*******@aequor.nl> schrieb
Hi,

I want to create an instance of a class by means of the classname.
Can't figure out how.

in pseudocode this would look like:

Dim MyClassName as String = "MyClass"
Dim MyObject as New MyClasses(MyClassName)



I wonder why people need the class name at runtime, but if it's necessary,
have a look at System.Activator.CreateInstance


Armin

Feb 20 '06 #5
"Bert" <ed*******@aequor.nl> schrieb
Thanks Armin, Carlos and Cor,

I do have a goal with this question, I'm using a form that uses
objects from different classes, depending on user's input. So in
order to create the objects I need this code:

Function CreateObject("ClassName" as String) as Object
dim oType as Type = Type.GetType("ClassName")
Return Activator.CreateInstance(oType)
End Function

No worries about changing names, since everything is decribed and
maintained in the databases.

After creating the object, do you intend to use the members inherited
from System.Object only?
Armin

Feb 20 '06 #6
No, I use the members by means of Reflection.

Egbert

"Armin Zingler" <az*******@freenet.de> wrote in message news:ep***************@tk2msftngp13.phx.gbl...
"Bert" <ed*******@aequor.nl> schrieb
Thanks Armin, Carlos and Cor,

I do have a goal with this question, I'm using a form that uses
objects from different classes, depending on user's input. So in
order to create the objects I need this code:

Function CreateObject("ClassName" as String) as Object
dim oType as Type = Type.GetType("ClassName")
Return Activator.CreateInstance(oType)
End Function

No worries about changing names, since everything is decribed and
maintained in the databases.



After creating the object, do you intend to use the members inherited
from System.Object only?


Armin

Feb 20 '06 #7
"Bert" <ed*******@aequor.nl> schrieb

Function CreateObject("ClassName" as String) as Object
dim oType as Type = Type.GetType("ClassName")
Return Activator.CreateInstance(oType)
End Function

No worries about changing names, since everything is decribed
and maintained in the databases.

After creating the object, do you intend to use the members
inherited from System.Object only?


No, I use the members by means of Reflection.

I see. I asked only because otherwise I would have suggested a common
interface or base class.
Armin

Feb 20 '06 #8
"Bert" <ed*******@aequor.nl> schrieb:
I want to create an instance of a class by means of the classname. Can't
figure out how.

in pseudocode this would look like:

Dim MyClassName as String = "MyClass"
Dim MyObject as New MyClasses(MyClassName)


Check out the snippets at
<URL:http://dotnet.mvps.org/dotnet/code/techniques/#ClassByName>.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Feb 20 '06 #9

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

Similar topics

9
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"
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...
1
by: Jeremy S. | last post by:
I suspect I may be fundamentally misunderstanding how object variable references behave - and I'd sure appreciate some clarification - given my particular scenario. Summary: In an ASP.NET (1.1)...
0
by: Greg Conely via .NET 247 | last post by:
I am creating a application that will be using plugins. I am doing this so that when I want to let this application work with another type of dbase system, I only have to write\install one plugin,...
2
by: §iD` | last post by:
Hi! I would like to create a virtual folder (which I want to mount) and populate managing his content by a DLL or something like that in VB.NET (2.0). How can I acomplish this? Thanks to...
8
by: a | last post by:
I'm trying to save data from a custom object into the profile object, but it is not structured the way that I want. I'm trying to get the custom object to serialize as xml to a Profile object...
1
by: Sebarry | last post by:
Hi, I use the following code to create a couple of input fields at run-time. It works fine in Firefox but it doesn't do anything in Internet Explorer and no errors are given. The DOM created...
3
by: suganya | last post by:
Hi Some professionals already has developed the project using menu. In my company, they have given me task to clear the error in that. It is a script file named as "menubarAPI4.js" which is kept...
2
by: tridirk | last post by:
Hi; I am getting a Objceted Expected Error on my forum site. I can't find what is wrong? Line: Char: Error: Object expected Code:0 the site is My SMF Forum
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.