473,473 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Instantiate objects at runtime possible?

Hi,

I have stored a menu in a database. The menu is dynamically updated and last level menu items contain the object type that should be instantiated. I am looking for a way to instantiate the object dynamically without an enormous select case statement. I can't find nothing in the docs... Any ideas?

To make things clear, what I want to do is something like this:

dim objType as new String("CustomerObject")

dim obj as new objType
All instantiated objects have new() constructors.

Mariano.
Nov 20 '05 #1
6 1564
something like

dim objType as Type = Type.GetType("CustomerObject")
dim objConstructor as ConstructorInfo =
objType.GetConstructor(Type.EmptyTypes)
dim obj as Object = objConstructor.Invoke(Nothing)

note: "CustomerObject" must be "full qualified"
so if that object is in nameSpace MyNamespace you should do:
dim objType as Type = Type.GetType("MyNamespace.CustomerObject")
hope this helps

dominique


"Mariano" <an*******@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
Hi,

I have stored a menu in a database. The menu is dynamically updated and last level menu items contain the object type that should be instantiated. I
am looking for a way to instantiate the object dynamically without an
enormous select case statement. I can't find nothing in the docs... Any
ideas?
To make things clear, what I want to do is something like this:

dim objType as new String("CustomerObject")

dim obj as new objType
All instantiated objects have new() constructors.

Mariano.

Nov 20 '05 #2
Take a look at the Activator.CreateInstance:
http://tinyurl.com/29rtl

You could use it like this:
Dim t As Type = Type.GetType("MyNameSpace.MyClass")
Dim o As Object = Activator.CreateInstance(t)

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"Mariano" <an*******@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
Hi,

I have stored a menu in a database. The menu is dynamically updated and last level menu items contain the object type that should be instantiated. I
am looking for a way to instantiate the object dynamically without an
enormous select case statement. I can't find nothing in the docs... Any
ideas?
To make things clear, what I want to do is something like this:

dim objType as new String("CustomerObject")

dim obj as new objType
All instantiated objects have new() constructors.

Mariano.

Nov 20 '05 #3
Cor
Hi Dominique,

I am curious because an answer from Wim partialy in Dutch, are you a native
Dutch speaker?

Cor
Nov 20 '05 #4
yep, from belgium...

wim is also from belgium (his email is same ISP as i have at home ;-))

D.

"Cor" <no*@non.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
Hi Dominique,

I am curious because an answer from Wim partialy in Dutch, are you a native Dutch speaker?

Cor

Nov 20 '05 #5
Cool, some Belgium folks (me too) !

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"Dominique Vandensteen" <domi.vds_insert@tralala_tenforce.com> wrote in
message news:Oq**************@tk2msftngp13.phx.gbl...
yep, from belgium...

wim is also from belgium (his email is same ISP as i have at home ;-))

D.

"Cor" <no*@non.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
Hi Dominique,

I am curious because an answer from Wim partialy in Dutch, are you a

native
Dutch speaker?

Cor


Nov 20 '05 #6
* "=?Utf-8?B?TWFyaWFubw==?=" <an*******@discussions.microsoft.com> scripsit:
I have stored a menu in a database. The menu is dynamically updated
and last level menu items contain the object type that should be
instantiated. I am looking for a way to instantiate the object
dynamically without an enormous select case statement. I can't find
nothing in the docs... Any ideas?


<http://groups.google.de/groups?selm=bqns3k%2424ghqf%242%40ID-208219.news.uni-berlin.de>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #7

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

Similar topics

2
by: jerrygarciuh | last post by:
Hello, Is it possible to instantiate a child class within the constructor of its parent? eg Class DBI extends DB { function DBI() { // explicit parent constructor call
5
by: Glenn Serpas | last post by:
I have Class A and Class B .. Class B has a private member that is a pointer to a Class A object. private: B *mypointer ; I instantiate the A object A* myobject new = A();
8
by: Steven T. Hatton | last post by:
I've had an idea kicking around in my head regarding how to create a library of classes (templates?) that provide the same kind of functionality as do Java classes which all derive from the UBC...
16
by: gabon | last post by:
Due a big project I would like to create different javascript classes and assign them to divs. But how? :) I know the usage of prototype but given that this could be possible: function...
9
by: the_grove_man | last post by:
I guess my question can go in two directions. I create applications that run multiple queries against a database. Generally speaking in the past I have used a Data Control (calling it dat1)...
48
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
14
by: Ray5531 | last post by:
I have a console application in my local computer which I like to use remoting in it,to instanciate an object (MyClass.dll) in a web application(its bin folder) in a completely seperated box(in the...
4
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the several instances (James, Gunner, etc.), without...
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
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,...
0
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,...
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.