Referencing a Form by name

  • Thread starter Thread starter Andrejko
  • Start date Start date
A

Andrejko

Guest
Does anyone know of a way to reference a class (say, a form class) by a String representation of its name?
IE,
System.Windows.Forms.getFormByName("Form1").someMember (pseudocode)
would be equivalent to just saying
Form1.someMember

Note: I am not trying to reference an instance of a class, but a Shared (static) member of that class.
 
You can't reference an instance of a form by it's name at the best of times, let alone if all you have is a string.

Consider a public hashtable, with which you could associate text with your form instances.
 
Shared members

Actually, I would like to acces Shared members of the form (I should have stated that in my original post)
I do not need an instance of the class to do this.
So, Form1.someMember is entirely possible, IFF someMember is declared as Shared.
Now it would be nice if I could do this with a string, without a big Select statement.
A hashtable would not work, since, as I am not creating instances of the classes, I would have nothing to place in it. Good idea though.
 
Oh ok.

To create an instance of any class in your app with a string, use this code:

Code:
Dim X As Form
X = CType(System.Reflection.Assembly.GetExecutingAssembly.CreateInstance("MyNameSpace.Form1"), Form)

X.ShowDialog()

I know this isn't exactly what you wanted but it's a start, and just off the top of my head.
 
thx

Thanks,
this actually is a usefull piece of information. Although this code still creates an instance, instead of just referencing the class. (Static class)
 
Back
Top