Adrian
Adrian

Reputation: 691

How to call a function dynamically

I have in vb a selector

Dim ver =101
Select Case ver
        Case 101
            upd101()
        Case 102
            upd102()
        Case 103
            upd103()
    End Select

How can I make this select better by calling function in more dynamically in this form: with the prefix "upd" followed by a ver integer..?

thank you! Adrian

Upvotes: 3

Views: 6189

Answers (3)

Nix
Nix

Reputation: 58522

Its possible but its not cleaner, or faster.

Dim t As Type = base.GetType()

t.GetMethod("upd" + theInt)

Upvotes: 3

shahkalpesh
shahkalpesh

Reputation: 33476

Dim ver as Integer 
dim procToCall as String

ver = 101
procToCall = "upd" & ver

CallByName myclassInstace, procToCall, vbMethod

EDIT: Here myClassInstance is the instance of the class you have created.
i.e. lets assume you have a class named Person which has a method named ToString.
The code will look like

dim somePerson as new Person
CallByName somePerson, "ToString", vbMethod

However, avoid using this style of programming.
How many such methods exist for you to call the method in this form?

Upvotes: 4

Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 10467

Can't you just call upd(param)?

I mean, rather than creating many functions, create one and use variables. ;]

Upvotes: 5

Related Questions