开发者

Using Variable as symbols vb.net

I am writing a very simple mat开发者_JAVA百科h game. What I would like to be able to do is this:

Dim symbol as String

Private Sub Math()
    symbol = "+"
    1 symbol 1 = 2

    symbol = "-"
    1 symbol 1 = 0
end sub

I know this won't work, but it is the idea I want, thanks in advance.


As Marc Gravell already mentioned, you could use a lambda expression. This is how it works in VB:

Private Sub Calculate(f As Func(Of Double, Double, Double))
    Dim a As Double = 1.5, b As Double = 3.14
    Console.WriteLine(f(a,b));
End Sub

Then you would call Calculate like this:

Calculate(Function(x,y) x+y)
Calculate(Function(x,y) x-y)
Calculate(Function(x,y) x*y)


Go with if else or switch case, use actual symbols inside the condition, something like

if symbol == "+":
return a+b;

if symbol == "-":
return a-b;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜