Solved

Create objects from a given string

Posted on 2006-07-05
8
210 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now