Solved

instantiate object using 'new' keyword

Posted on 2004-11-02
187 Views
Last Modified: 2010-04-23
Hi,

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
Simon
0
Question by:sbhodge
    6 Comments
     
    LVL 25

    Expert Comment

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

    Accepted Solution

    by:
    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.

    SoMoS
    0
     

    Author Comment

    by:sbhodge
    Hi SoMoS

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

    Simon
    0
     

    Author Comment

    by:sbhodge
    Ronald, thanks for your interest and time too.

    Simon
    0
     
    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 ;)
    0
     

    Author Comment

    by:sbhodge
    Ahh, you're too modest!
    0

    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 vb.net 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 (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. 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