Passing types or class names

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to pass a form to a class method then have the class instantiate and instance of the form and open it.
Is there a way of creating a form from its class name (a string variable) or from getting and passing its Type, then using it to instantiate an object?
thx, troy
 
Troy said:
I'm trying to pass a form to a class method then have the class
instantiate and instance of the form and open it. Is there a way of
creating a form from its class name (a string variable) or from
getting and passing its Type, then using it to instantiate an object?
thx, troy

Activator.Createinstance

see also:
<F1>
VS.NET
.NET Framework
Programming with .NET Framework
Discovering type information at runtime

especially
Dynamically loading and using types
 
* "=?Utf-8?B?VHJveQ==?= said:
I'm trying to pass a form to a class method then have the class
instantiate and instance of the form and open it.

Is there a way of creating a form from its class name (a string
variable) or from getting and passing its Type, then using it to
instantiate an object?

For example (there are many other ways to use 'CreateInstance'):

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByName( _
"System.Windows.Forms", _
"System.Windows.Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add(c)
///
 
Back
Top