|
-
Nov 22nd, 2002, 12:35 PM
#1
Thread Starter
Registered User
Create an instance of an object from a string
Hi there all!
I have a question:
Is it possible to create an instance of an object if you have the name of the object in a string value?
In VB6 you could use the CreateObject Method like this for example:
Set xlApp = CreateObject("Excel.Apllication")
or something similiar I think. I have seen that the CreateObject function is still there, but didnt get lucky trying it out. CreateObject("Form1") for example, did not do the job.
I have a way around this problem, but this would sure save me a couple of pages of code.
Thanks!
/Leyan
-
Nov 22nd, 2002, 03:45 PM
#2
PowerPoster
You should be able to using Reflection. Check out the System.Reflection namespace. I'm not on my home computer, so I can't dig up an example right now.
-
Nov 24th, 2002, 09:42 AM
#3
Thread Starter
Registered User
It sure worked with reflection.....
Code:
Dim ty As Type = Type.GetType("Test.Form2")
Dim o As Object = Activator.CreateInstance(ty)
Dim mi As MethodInfo = ty.GetMethod("Show")
Dim params(mi.GetParameters.Length - 1) As Object
mi.Invoke(o, params)
You're a hat full of flowers. 
Thanks alot
/Leyan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|