• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

Inheritance/Declaration Question

I have a lookup routine that I created and compiled into a DLL.  I am using this in another program as such:

        Dim frm As frmSupplierLookup
        frm = New frmSupplierLookup("", Database, Company)
        frm.Show()

This works fine.

What I need to be able to do is have the "frmSupplierLookup" part be a variable.  For instance, sometimes it may need to be "frmCustomerLookup", which is another form in my DLL.  

This will be stored in a database and be user-defined and could change in the future.  So using a Select/Case statement or similar routine will not work.  It must truly be generic.

How can I do this?

Thanks,
Chris
0
chlade
Asked:
chlade
  • 4
  • 3
1 Solution
 
bisonfur37Commented:
I would  store the class name in a config file (e.g. App.Config) and use the Activator.CreateInstance method to  generate the object at runtime.

Also, check  out the  Factory / Abstract Factory Design Patterns  (http://www.dofactory.com/Patterns/PatternAbstract.aspx).
0
 
chladeAuthor Commented:
That wasn't enough to outright help me.  I searched on Activator.CreateInstance and found some good examples.  However, I am having trouble with the GetType on the class.

When I put the following in the Watch window, it returns Nothing.

Type.GetType("CommonLookupRoutines.frmSupplierLookup")

What might I be doing wrong?
0
 
bisonfur37Commented:
the problem is with the string you are  passing to Type.GetType.

Assuming you are building w/  Studio .Net, and have neither edited Assembly.info nor changed the project's root namespace,  you should be able to run the following.

Namespace MyTestNamespace

    Public Class MyTestClass
        Public Function MyTestFunction() As String
            Return "Hello World"
        End Function
    End Class

End Namespace


Namespace MainNamespace

    Public Class MainClass

        Shared Sub Main()
            Const TYPE_STRING As String = "WindowsApplication1.MyTestNameSpace.MyTestClass, WindowsApplication1, Version=1.0, Culture=neutral, PublicKeyToken=null"
            Dim myType As System.Type = Type.GetType(TYPE_STRING, True, True)
            MsgBox(DirectCast(Activator.CreateInstance(myType), MyTestNamespace.MyTestClass).MyTestFunction())
        End Sub

    End Class

End Namespace


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
bisonfur37Commented:
forgot to mention... in a assembly (project) called  "WidnowsApplication1"
0
 
chladeAuthor Commented:
Thanks.  I'm really close now.  The only remaining problem seems to be how to pass in the parameters.  I did some googling and thought I had it but it's giving an error.

Here is my "hard-coded" code that works fine:

        Dim frm As frmSupplierLookup
        frm = New frmSupplierLookup("", Database, Company)
        frm.Show()

Here is my new code:

        Dim myType As System.Type = Type.GetType("CommonLookupRoutines.frmSupplierLookup, " & _
            "CommonLookupRoutines, Version=1.0, Culture=neutral, PublicKeyToken=null", True, True)
        Dim frm As Form = DirectCast(Activator.CreateInstance(myType, Reflection.BindingFlags.CreateInstance, _
            Nothing, New [Object]() {"", Database, Company}), Form)
        frm.Show()

When I run this, I get the error message "Constructor on type 'CommonLookupRoutines.frmSupplierLookup' not found".

The constructor is there (as witnessed by the fact that the original code above works).  I'm guessing that I'm not properly passing the parameters but I guess I'm not certain.
0
 
chladeAuthor Commented:
Nevermind, I figured it out by using a different overload.  The following code works:

        Dim myType As System.Type = Type.GetType("CommonLookupRoutines.frmSupplierLookup, " & _
            "CommonLookupRoutines, Version=1.0, Culture=neutral, PublicKeyToken=null", True, True)
        Dim frm As Form = DirectCast(Activator.CreateInstance(myType, New [Object]() {"", Database, Company}), Form)
        frm.Show()

Thanks for your help.
0
 
bisonfur37Commented:
I've found this technique to be very useful in creating flexible applications  -- best of luck using it!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now