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 开发者_如何学Gocan 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
Can't you just call upd(param)?
I mean, rather than creating many functions, create one and use variables. ;]
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?
Its possible but its not cleaner, or faster.
Dim t As Type = base.GetType()
t.GetMethod("upd" + theInt)
精彩评论