Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Basic .net - Call Procedure from a variable name

Tags:

.net

vb.net

Is there a way to call a procedure in Visual Basic (.net) with a variable name?

For example, the variable strColour can be one of 10 pre-defined values, green blue black white red pink orange yellow indigo purple. How to handle each one is in it's own Sub Routine, colgreen, colblue, colblack and so on.

I can use a bunch of if..then..else and select case, but what I'd like to have is something like VBA Excel's Run "col" & strColour

Is it possible?

like image 558
SanderDolphin Avatar asked Sep 27 '26 16:09

SanderDolphin


1 Answers

Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click
    Dim s As String = "one"
    CallByName(Me, s, CallType.Method) 'Subs must be public

    s = "two"
    CallByName(Me, s, CallType.Method) 'Subs must be public
End Sub

Public Sub one()
    Stop
End Sub

Public Sub two()
    Stop
End Sub
like image 141
dbasnett Avatar answered Sep 30 '26 05:09

dbasnett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!