Link to home
Start Free TrialLog in
Avatar of sbhodge
sbhodge

asked on

instantiate object using 'new' keyword

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
Avatar of RonaldBiemans
RonaldBiemans

Yes, you can do that using reflection, give me a second and I'll dig up some example
ASKER CERTIFIED SOLUTION
Avatar of Ignacio Soler Garcia
Ignacio Soler Garcia
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sbhodge

ASKER

Hi SoMoS

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

Simon
Avatar of sbhodge

ASKER

Ronald, thanks for your interest and time too.

Simon
Not really, just a common OOP developer with 12 hours a day of wrk and with good skills with google and vstudio help ;)
Avatar of sbhodge

ASKER

Ahh, you're too modest!