Solved

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

Posted on 2011-02-25
6
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34984591
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34984603
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
ID: 34989711
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jackiemeck
ID: 35826224
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 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35826833
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
ID: 35833514
That's perfect!  It's exactly what I needed.  Thanks so much.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS 2016 Rendering HTML tables 3 82
VB.Net - BackgroundWorker running multiple 5 69
Trying to Create FTP Client in VB.net - getting errors 3 32
vb.net dbnull syntax 1 47
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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