Solved

Create objects from a given string

Posted on 2006-07-05
8
209 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
Comment Utility
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
Comment Utility
-------------------------------------------------------------------
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
Comment Utility
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
 
LVL 2

Author Comment

by:posseke
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:Nandakumar Sakthivel
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ok thx nanda, I'll keep that in mind and change the code!!

Thx a lot,

Greets,

Po$$e
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

7 Experts available now in Live!

Get 1:1 Help Now