Dynamic creation of object during runtime

  • Thread starter murali pannala via .NET 247
  • Start date
M

murali pannala via .NET 247

(Type your message here)

--------------------------------
From: murali pannala


i would want to create an object dynamically. i.e i hold my class name in a variable only.and by passing this variable can i create the object of the type (value which the variable holds)

eg :

dim str as string
str="active"

i wanna create an object of type active during runtime as the val of str can keep changing
 
A

Armin Zingler

murali pannala via .NET 247 said:
i would want to create an object dynamically. i.e i hold my class
name in a variable only.and by passing this variable can i create the
object of the type (value which the variable holds)

eg :

dim str as string
str="active"

i wanna create an object of type active during runtime as the val of
str can keep changing


System.Activator.CreateInstance


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
O

One Handed Man \( OHM#\)

Not easily possible, Ive seen this question asked before and there was a
solution, however, i seem to remember it was a rather convoluted answer.

OHM


murali pannala via .NET 247 said:
(Type your message here)

--------------------------------
From: murali pannala


i would want to create an object dynamically. i.e i hold my class name in
a variable only.and by passing this variable can i create the object of the
type (value which the variable holds)
 
H

Herfried K. Wagner [MVP]

* murali pannala via .NET 247 said:
i would want to create an object dynamically. i.e i hold my class name
in a variable only.and by passing this variable can i create the object
of the type (value which the variable holds)

\\
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)
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top