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

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
0
sbhodge
Asked:
sbhodge
  • 3
  • 2
1 Solution
 
RonaldBiemansCommented:
Yes, you can do that using reflection, give me a second and I'll dig up some example
0
 
Ignacio Soler GarciaSolution Architech & Technical LeadCommented:
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
 
sbhodgeAuthor Commented:
Hi SoMoS

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

Simon
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sbhodgeAuthor Commented:
Ronald, thanks for your interest and time too.

Simon
0
 
Ignacio Soler GarciaSolution Architech & Technical LeadCommented:
Not really, just a common OOP developer with 12 hours a day of wrk and with good skills with google and vstudio help ;)
0
 
sbhodgeAuthor Commented:
Ahh, you're too modest!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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