Click here to Skip to main content
15,888,019 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
how can call form name from string variable?
Posted
Updated 24-May-12 0:58am
v2

Your question isn't at all clear, but I'm assuming you want to create a form using a string. If that's true:

C#
var form = Activator.CreateInstance(Type.GetType("STRING_VARIABLE_HERE")).Unwrap() as Form;

if(form != null)
    form.Show();
 
Share this answer
 
v2
Comments
Ed Nutting 24-May-12 7:22am    
My 5+ - Though I don't like the use of var (why not just do proper casting and strong typing, that's half the point of languages like VB.Net and C#). Anyway, looks like it should work :)
jim lahey 24-May-12 7:34am    
The use of var in no way indicates a lack of strong typing. It's syntactic sugar for the compiler because you can infer the type from the cast that is performed. It's a standard part of C#.
Ed Nutting 24-May-12 12:47pm    
The sue of var isn't strongly typing - it can hold any type, which is by definition therefore not strongly typing your variable. Using var when it is not necessary (which is always in .Net languages) makes code unreadable and hard to understand, especially as many people don't use casting or the as keyword with it. It may be standard in C# but it is a horrible thing to use, otherwise why bother with using proper types!? You call it a "syntactic sugar", I call it an abomination...
jim lahey 4-Jun-12 3:45am    
I think you're confusing var and dynamic keywords. Of course var is strongly typed, assign something to a var and on the next line intellisense pops up with all the properties, methods and events for the type you just assigned, so I am definitely using proper types. as for readability my code reads just fine, but it's human nature to fear what you don't understand ;)
Ed Nutting 4-Jun-12 4:35am    
What you say demonstrates you do not understand what strongly typing really means or are you about to argue that JavaScript is strongly typed? I have done far too much programming in JavaScript - I know exactly what var is, what it does and that is precisely why it shouldn't be used. By the way, Intellisense says nothing about whether something is strongly typed or even good code practice. Perhaps you should try programming without it for a while and tell you what, only use vars. I bet your code will have a lot more bugs, be significantly harder to maintain and grossly unreadable.
Unless you have a good reason to activate types (forms in this case) based on a string, I would strongly recommend not to do this.

Using a string variable makes your code vulnerable to different kinds of errors, for example spelling mistakes in the string. These are not detected by the compiler since the form name is 'hidden' inside a string.

Personally I would prefer rethinking the need for this and possibly use generics instead. For example if you need to have a common method for all forms, you cound define a generic method that accepts any Form but only Form.

For information about generics, see Generic Types in Visual Basic[^]
 
Share this answer
 
Hi..

Please Try This.

I have create two form in my Application which is below.,
Form1 and Form2.
In this application WindowsFormsApplication2 is my namespace.

In Form1 contain one Button And this button's click event i open form2.
In Button click event looks like..

C#
var newForm = Activator.CreateInstance(Type.GetType("WindowsFormsApplication2.Form2")) as Form;

            if (newForm!= null)
                newForm.Show();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900