|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Create object through classnameHi,
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 "Bert" <edoord***@aequor.nl> schrieb I wonder why people need the class name at runtime, but if it's necessary, > 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) have a look at System.Activator.CreateInstance Armin 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 Show quoteHide quote "Armin Zingler" <az.nospam@freenet.de> wrote in message news:e$QtEciNGHA.3284@TK2MSFTNGP14.phx.gbl... > "Bert" <edoord***@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 >
Show quote
Hide quote
"Bert" <edoord***@aequor.nl> schrieb After creating the object, do you intend to use the members inherited > 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. from System.Object only? Armin No, I use the members by means of Reflection.
Egbert Show quoteHide quote "Armin Zingler" <az.nospam@freenet.de> wrote in message news:epYI%23YjNGHA.428@tk2msftngp13.phx.gbl... > "Bert" <edoord***@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 >
Show quote
Hide quote
"Bert" <edoord***@aequor.nl> schrieb I see. I asked only because otherwise I would have suggested a common > > > > > > 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. interface or base class. Armin 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 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" <edoord***@aequor.nl> escribió en el mensaje I want to create an instance of a class by means of the classname. Can't news:%238CT6TiNGHA.4052@TK2MSFTNGP15.phx.gbl... Hi, 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 "Bert" <edoord***@aequor.nl> schrieb: Check out the snippets at >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) <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/>
Best data structure?
empty GUID from string in VB2005 Changing CheckedState or Checked-value without raising ItemChecked-event Opacity Unable to open file Text Files Installing published application fails How to change "database and User" into CR9 Webservice MyApplication has encountered a problem... when not Administrator! What is the difference between VB.NET and ASP.NET?? |
|||||||||||||||||||||||