Kuzon
Kuzon

Reputation: 822

Using Variable as symbols vb.net

I am writing a very simple math 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.

Upvotes: 0

Views: 587

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112682

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)

Upvotes: 1

Adithya Surampudi
Adithya Surampudi

Reputation: 4454

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;

Upvotes: 1

Related Questions