[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

instantiate object using 'new' keyword

Posted on 2004-11-02
6
Medium Priority
?
189 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
Comment
Question by:sbhodge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

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

Accepted Solution

by:
Ignacio Soler Garcia earned 1000 total points
ID: 12474759
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
ID: 12474974
Hi SoMoS

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

Simon
0
Independent Software Vendors: 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!

 

Author Comment

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

Simon
0
 
LVL 5

Expert Comment

by:Ignacio Soler Garcia
ID: 12475022
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
ID: 12475069
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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