• twitter

  • Advertisements

Coolest function of the day, call an objects method using a variable, Dynamically invoke properties by string name using VB.NET

So I had always wanted to be able to do something like this

dim myDog as new dogobject

dim methodVariable as string = “bark”


i.e. call a method by using a variable

Well, after some serious googling, I finally ran across this function CallByName

this allows me to do this


or if you wanted to iterate through ever method in the object you could do

Dim pdc As System.ComponentModel.PropertyDescriptorCollection
pdc = System.ComponentModel.TypeDescriptor.GetProperties(myDog.GetType)

For Each pd As System.ComponentModel.PropertyDescriptor In pdc




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: