Solved

Create objects from a given string

Posted on 2006-07-05
8
214 Views
Last Modified: 2012-05-05
Hi,

Simple problem, simple solution?

I have multiple forms, each has a different name...
What I would like to achieve is somewhat this:

Public Sub callNewForm(byval formName as String) as Form
{
   dim frm as new "FORMNAME"
   return frm
}

So I want to create and return a form with the help of a string containing the correct name of the form...

Any suggestions are welcome,

Greets,

Po$$e
0
Comment
Question by:posseke
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 500 total points
ID: 17041053
Hi posseke,
----------

you could try something like

System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(formName)

make sure that your form type exists in the current assembly, if that is not the case then you have load the assembly that contains the form and create an instance of that form

----------
bruintje
share what you know, learn what you don't
0
 
LVL 2

Author Comment

by:posseke
ID: 17041127
-------------------------------------------------------------------
Bruintje bedankt voor de reply,

ik ga jou idee uitproberen , en laat je direct iets weten...
-------------------------------------------------------------------

Thx for the reply, I'll test it and keep you informed!!!

Greets,

Po$$e
0
 
LVL 2

Author Comment

by:posseke
ID: 17041242
Bruintje,

It doesn't seem to be working, the forms i want to create are all in the current assembly so that shouldn't be the problem....

I'm getting no object returned from the reflection call...

Is there a workaround?

Greets,

Po$$e
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!

 
LVL 2

Author Comment

by:posseke
ID: 17041397
Bruintje,

Finally I got it working!!! :):)
It's about the same code you gave me, except the createInstance is changed :

Try
            Dim currAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
            retForm = currAssembly.CreateInstance(currAssembly.GetName.Name & "." & formName)
Catch ex As Exception
            Return Nothing
End Try

So you have to give 'AssemblyName.FormName' as parameter for the createInstance method....

Thanks for your comment, I find Reflection a very useful tool, and it will surely help me out in upcoming projects...

Thanks a lot!!

Greets,

Po$$e
0
 
LVL 6

Expert Comment

by:Nandakumar Sakthivel
ID: 17041459
Hi,
    Try to pass the full name of the forms type for the function
         
    Public Function callNewForm(ByVal formName As String) As Form
        Dim frm As Form = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(formName)
        Return frm
    End Function

Call the above function like
       callNewForm("WindowsTestApplication.Form5")

format -  " Namespace.formname"

Hope this helps you

Thanks,
Nanda
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17041536
sorry for passing out for a moment, but you found the answer already

reflection is indeed very usefull when you need it ;-)

thanks for the grade
0
 
LVL 6

Expert Comment

by:Nandakumar Sakthivel
ID: 17042285
Hi,
      "currAssembly.GetName.Name" May not work in some cases . The name space may be diferent from the assembly name .so , don not use the  currAssembly.GetName.Name.
      Instead of that plz specify full name space

Thanks,
Nanda
0
 
LVL 2

Author Comment

by:posseke
ID: 17042855
ok thx nanda, I'll keep that in mind and change the code!!

Thx a lot,

Greets,

Po$$e
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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