?
Solved

Create objects from a given string

Posted on 2006-07-05
8
Medium Priority
?
217 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 2000 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
Technology Partners: 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

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!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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 …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

801 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