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
chladeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.