jackiemeck
asked on
vb.net how to instantiate a class based on a variable name at runtime
I'm sure this is easy, but my search is coming up short. I want to be able to instantiate an object (in this case a Prompt Screen), but the Class name is variable based on what the user is doing.
This is the statement:
myPromptScreen = New [VariableClassName]
Thanks in advance.
This is the statement:
myPromptScreen = New [VariableClassName]
Thanks in advance.
Look at the 5th option down. Regardless though one of these will work for you. It's the right way:
http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx
http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx
Here is a small chunk of code that I've used to allow a program to dynamically load a "plug-in" at run time. It uses the techniques as described above
Imports System.IO
Imports System.Reflection
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim DLL_File As String
' fill the combobox with the list of available plug-in DLLs
For Each DLL_File In Directory.GetFiles(Application.StartupPath & "\" & My.Settings.PluginDirectory, "Plug*.dll")
ComboBox1.Items.Add(DLL_File)
Next
' make a default item
If ComboBox1.Items.Count > 0 Then
ComboBox1.Text = ComboBox1.Items(0).ToString
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Assmbly As Reflection.Assembly
Dim PlugInType As Type
Dim pi As PluginBase
' load the DLL file using the "signature" of the base class
Assmbly = Reflection.Assembly.LoadFrom(ComboBox1.Text)
PlugInType = Assmbly.GetType(Assmbly.GetName.Name & "." & Assmbly.GetName.Name, True, True)
pi = CType(Activator.CreateInstance(PlugInType), PluginBase)
' execute a method on the plug in
pi.DoSomething()
End Sub
End Class
ASKER
I have searched numerous resources and have found a lack of detailed examples along the lines of what I'm trying to do. I know I need "CreateInstance", but I don't know how to format it. I'll restate by issue with hopefully a little better detail:
I have 15 Forms that each include the same methods. I don't know which of the Forms the user will decide to activate until runtime. Basically, there is a loop of 1 through 15, and if the Form is selected, we need to run the identical method inside it.
' The assembly is called "MyProgram", but I'm not sure where that goes.
FormName(1) = "FormForReportA"
FormName(2) = "FormForReportB"
FormName(...) = (etc)
FormName(15) = "FormForReportZ"
for x = 1 to 15
myPromptScreen(x) = CreateInstance("NotSureWha tGoesHere" , FormName(x))
next x
For x = 1 to 15
if UserSelected(x) = true then
myPromptScreen(x).Initiali zeData()
myPromptScreen(x).Calculat eData()
end if
next x
Thanks in advance...
I have 15 Forms that each include the same methods. I don't know which of the Forms the user will decide to activate until runtime. Basically, there is a loop of 1 through 15, and if the Form is selected, we need to run the identical method inside it.
' The assembly is called "MyProgram", but I'm not sure where that goes.
FormName(1) = "FormForReportA"
FormName(2) = "FormForReportB"
FormName(...) = (etc)
FormName(15) = "FormForReportZ"
for x = 1 to 15
myPromptScreen(x) = CreateInstance("NotSureWha
next x
For x = 1 to 15
if UserSelected(x) = true then
myPromptScreen(x).Initiali
myPromptScreen(x).Calculat
end if
next x
Thanks in advance...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's perfect! It's exactly what I needed. Thanks so much.
http://msdn.microsoft.com/en-us/library/aa310400(VS.71).aspx