dynamic typing

  • Thread starter Thread starter mahmut arslan
  • Start date Start date
M

mahmut arslan

is it possible in VB.NET to declare a variable whose type
will be clear in runtime e.g.

dim ctrl as LinkButton
dim tp as Type = Type.GetType(Control)
dim ctrl_dynamic as tp

or something like this.
(I know, the code above is not working)
 
Just Dim the variable as Object. All classes inherit from Object, so a
variable that is a reference to Object can be assigned to any class
instance.

Dim ctrl As Object

ctrl = AnyObjectYouWant

If you want to be more specific, you use another subclass that all your
objects have in common. That gives you an added measure of type safety. So,
for example, if this variable only ever uses objects that are derived from
the Control class, you can Dim the variable as Control.

Dim ctrl As Control
ctrl = AnyControlObjectYouWant

In this case, you can't assign a non-control object to the variable, so you
get better protection and intellisense.

-Rob [MVP]
 
Back
Top