Jump to content
Xtreme .Net Talk

Referencing a Form by name


Recommended Posts

Guest Andrejko
Posted

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.

Guest Andrejko
Posted

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.

  • *Gurus*
Posted

Oh ok.

 

To create an instance of any class in your app with a string, use this 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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Guest Andrejko
Posted

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)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...