Dynamically creating objects based on Strings

Bigbeanpole

Member
Joined
Jul 7, 2005
Messages
15
Programming Experience
1-3
Awhile ago, I was looking for how to create dynamic forms based on a string. In that search, I came across this:
VB.NET:
[size=2]
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Shared[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] CreateObjectInstance([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] objectName [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object
 
[/color][/size][size=2][color=#008000]' Creates and returns an instance of any object in the assembly by its type name.
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] obj [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] testing [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "Form1.ComboBox1"
 
[/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] objectName.LastIndexOf(".") = -1 [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#008000]'Appends the root namespace if not specified.
 
[/color][/size][size=2]objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2]obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)
 
MessageBox.Show("[assembly].getentryassembly " & [Assembly].GetEntryAssembly.ToString & ControlChars.NewLine & _
 
"[assembly].getentryassembly.GetName: " & [Assembly].GetEntryAssembly.GetName.ToString & ControlChars.NewLine & _
 
"[assembly].getentryassembly.GetName.Name: " & [Assembly].GetEntryAssembly.GetName.Name.ToString & ControlChars.NewLine & _
 
"[assembly].getentryassembly.GetName.name.cbolookupbyasset " & [Assembly].GetEntryAssembly.GetName.Name & "." & testing.ToString)
 
[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception
 
obj = [/size][size=2][color=#0000ff]Nothing
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] obj
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Function[/color][/size]
[size=2][color=#0000ff]
[/color][/size]

Now, according to the above, should I not also be able to create an object reference to a combo box, or any other object I so desire, by passing it a string of FormName.ControlName ? I've tried several different methods and can't quite find the right one. Does anybody have any suggestions?

I am wanting to have a dynamic reference to a combobox based on a string that will definately exist (I'm passing it specific values, so I know the control is there...)

In the above, I am able to create forms by calling it thus:
VB.NET:
[/color][/color][/size]
[size=2][color=#0000ff][size=2][color=#0000ff]DirectCast[/color][/size][size=2][color=#000000](CreateObjectInstance(formName), Form)[/color][/size]
[size=2][color=#000000]

with formName being a valid string value.

Can I not do the same thing changing Form to ControlOfChoice (ComboBox currently) ?
(alot of the above code is for testing purposes...all the messageboxes and what-not)
[/size]
 
Not with the function written the way that it is. That function will create instances of a form (given a string containin the base form's name) but only if it's contained withing the current assembly. To create the combobox, the CB lies in a different namespace & assembly, so you would have to make changes to detect the type being requested and change the assembly part to reflect where the requested control type actualy exits.

Tg
 
Is there somewhere that describes the Assembly and where various things live inside the Assembly? I tried to do some digging, but couldn't find a very difinitive explanation of it. It's a little beyond me at this phase of my programming experience. :S
 
Well, for a combo box, it's something like this: System.Windows.Forms.ComboBox.... If you look in the "hidden" code that the IDE builds for you where all the controls are created, it gives the namespaces you need there.

Tg
 
Back
Top