By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 927 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

Instantiate objects at runtime possible?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
* "=?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 discussion thread is closed

Replies have been disabled for this discussion.