Solved

Create objects from a given string

Posted on 2006-07-05
8
211 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - Project Software to help the Developer 7 40
VB.NET (2008) - Refactoring Question 2 23
vb.net and creating a class 5 20
VB.NET Textbox input validation 4 37
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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 …
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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