473,669 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(MyCla ssName)

Thanks for your help,

Egbert
Feb 20 '06 #1
8 4733
"Bert" <ed*******@aequ or.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(MyCla ssName)

I wonder why people need the class name at runtime, but if it's necessary,
have a look at System.Activato r.CreateInstanc e
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.Activato r.CreateInstanc e methods
to create an instance, but be aware that this pseudo-code:

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

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.Activato r.CreateInstanc e(type), and you can get the type through
several ways. For example, you can get the types of an assembly with
Assembly.GetTyp es, or only the public types with Assembly.GetExp ortedTypes.
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*******@aequ or.nl> escribió en el mensaje
news:%2******** ********@TK2MSF TNGP15.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(MyCla ssName)

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("C lassName" as String) as Object
dim oType as Type = Type.GetType("C lassName")
Return Activator.Creat eInstance(oType )
End Function

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

Egbert

"Armin Zingler" <az*******@free net.de> wrote in message news:e$******** ******@TK2MSFTN GP14.phx.gbl...
"Bert" <ed*******@aequ or.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(MyCla ssName)



I wonder why people need the class name at runtime, but if it's necessary,
have a look at System.Activato r.CreateInstanc e


Armin

Feb 20 '06 #5
"Bert" <ed*******@aequ or.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("C lassName" as String) as Object
dim oType as Type = Type.GetType("C lassName")
Return Activator.Creat eInstance(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*******@free net.de> wrote in message news:ep******** *******@tk2msft ngp13.phx.gbl.. .
"Bert" <ed*******@aequ or.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("C lassName" as String) as Object
dim oType as Type = Type.GetType("C lassName")
Return Activator.Creat eInstance(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*******@aequ or.nl> schrieb

Function CreateObject("C lassName" as String) as Object
dim oType as Type = Type.GetType("C lassName")
Return Activator.Creat eInstance(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*******@aequ or.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(MyCla ssName)


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
35996
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
25404
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
1
1658
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) Web application I store some information in an ArrayList in the Session. After it is initially placed in the Session, the applicaiton may need to update some of that information. The type of data stored in each element of the ArrayList is of a...
0
1596
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, not the entire app. I have created a base interface, it looks like: public interface myModel sub Initialize(byref DisplayPanel) sub Connect() sub Disconnect() sub Query() ...
2
2678
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 all!
8
2017
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 like so: <Teachers> <Teacher> <Classes> <Class>
1
3685
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 elements are appended to a div called appendTo. var className = ""; if( window.ActiveXObject ) className = "className"; else
3
8643
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 inside the folder "menu_script". The following is the code in this file. var menuOffsetTop=menuOffsetLeft=submenuOffsetTop=submenuOffsetLeft=submenuOffsetRight=0; var Doc=this.document;var standards=Doc.createElement&&Doc.createTextNode?1:0;...
2
31754
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
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8895
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8809
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7407
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2797
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 we have to send another system
2
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.