instantiate object using 'new' keyword

Posted on 2004-11-02
Last Modified: 2010-04-23

I'm having a mental block - I may have once known the answer to this but for the life of me can't recall it!

I want to create a new object at run time.  What I don't want to do is this:

strAString = "datObject1" ' Defined elsewhere
Select Case strAString

   Case "datObject1"
      Dim anObject As datObjectTemplate = new datObject1

   Case "datObject2"
      Dim anObject As datObjectTemplate = new datObject2

End Select

As there are lots of objects and they all inherit from datObjectTemplate.  What I do want to do is something like this:

strAString = "datObject1" ' Defined elsewhere
Dim anObject As datObjectTemplate = new ???

So my question is, what can I replace ??? with to be able to determine the type of object that is created from the contents of the strAString variable and without having to test the contents of the strAString variable and write a seperate Dim line for each possibility?!!  I'm open to other suggestions - I just want to avoid the select case avenue!

Thanks a lot for looking
Question by:sbhodge
    LVL 25

    Expert Comment

    Yes, you can do that using reflection, give me a second and I'll dig up some example
    LVL 5

    Accepted Solution

    This is done with the Class Activator in this way:

    Dim s As String = "<FullQualifiedNameOfTheClass>"
    Dim a As Object

    a = System.Activator.CreateInstance(System.Type.GetType(s))

    and now the a have the instance that you want.

    Askme for more if you need.


    Author Comment

    Hi SoMoS

    Thanks a lot, thats solved the problem perfectly - you certainly are worthy of the title 'expert'.


    Author Comment

    Ronald, thanks for your interest and time too.

    LVL 5

    Expert Comment

    by:Ignacio Soler Garcia
    Not really, just a common OOP developer with 12 hours a day of wrk and with good skills with google and vstudio help ;)

    Author Comment

    Ahh, you're too modest!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    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…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    934 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

    21 Experts available now in Live!

    Get 1:1 Help Now