Solved

vb.net how to instantiate a class based on a variable name at runtime

Posted on 2011-02-25
6
432 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:jackiemeck
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
0
 
LVL 9

Expert Comment

by:rawinnlnx9
Comment Utility
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
0
 
LVL 41

Expert Comment

by:graye
Comment Utility
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

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jackiemeck
Comment Utility
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("NotSureWhatGoesHere", FormName(x))
next x

For x = 1 to 15
    if UserSelected(x) = true then
        myPromptScreen(x).InitializeData()
        myPromptScreen(x).CalculateData()
    end if
next x

Thanks in advance...
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
Try something like this:

    Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
    ...
    for x = 1 to 15
        myPromptScreen(x) = asm.CreateInstance(asm.GetName.Name & "." & FormName(x))
    next x
0
 

Author Closing Comment

by:jackiemeck
Comment Utility
That's perfect!  It's exactly what I needed.  Thanks so much.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Zoom web page in asp.net 2 34
DataGridView 2 28
Deserialize JSON array VB.Net problem 3 35
Expando 4 31
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now