Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assign a class at run-time 1

Status
Not open for further replies.

bitmite

Programmer
Dec 2, 2003
122
CA
I know there is probably and easy way to do this but I can not think of it.

I would like the user to select a report then I need to assign the class acosiated with the reports

so ie

I have reports:
reprints2005
reprints2006

then I have the code

if reports = "reprints2005" then
Dim cr As New reprints2005
elseif reports = "reprints2006 then
Dim cr As New reprints2006
enf if

I want the be able to use the report name they selected to acctually assign cr as appose to a big if statement

Brought to you By Nedaineum
The Portal to Geek-tum
 
Im guessing that your example is a little contrived, as reports2005, and reports2006 should be identical (going by the naming convention)

I think you'll be best off with a big if statement (or a case statement for now though..)

K
 
How about:
Code:
Dim myPrints As Object

Select Case reports
   Case "reprints2005"
      myPrints = New reprints2005
   Case "reprints2006"
      myPrints = New reprints2006
   .
   .
End Select

Regards.
 
I would like to have it say

myPrints = New me.dropdown.selectedvalue

this way if I add reports I do not have to adjust this code each time.

does this make sense?

Brought to you By Nedaineum
The Portal to Geek-tum
 
The following should work. It uses Reflection. Remember to change the Root Name Space to whatever your namespace is:

Code:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("Report2005")
        ListBox1.Items.Add("Report2006")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim report As Object
        report = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("RootNameSpace." + ListBox1.Text, True)
        TextBox1.Text = report.ToString()
    End Sub
End Class

Public Class Report2005 : Inherits Object
    Public Overrides Function ToString() As String
        Return "2005 Report"
    End Function
End Class

Public Class Report2006 : Inherits Object
    Public Overrides Function ToString() As String
        Return "2006 Report"
    End Function
End Class

You could make this even better by using a custom interface rather than Object as a base class.
 
that is perfect thank very much

Brought to you By Nedaineum
The Portal to Geek-tum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top