0
我希望代码返回运算符(如(+)或( - ) ),然后使用它来执行计算。在vb.net中可能吗?vb.net函数返回运算符
伪
public function rOperator(params) as operator
if (..)
return +
else
return -
end if
end function
伪
Msgbox(1 rOperator 2)
我希望代码返回运算符(如(+)或( - ) ),然后使用它来执行计算。在vb.net中可能吗?vb.net函数返回运算符
伪
public function rOperator(params) as operator
if (..)
return +
else
return -
end if
end function
伪
Msgbox(1 rOperator 2)
前面已经讲过,这是不可能的。你可以做的是创建一个Func(Of T)
字典,然后通过传入您的运营商来调用这些字段:
Sub Main
Dim operations As New Dictionary(Of String, Func(Of Double, Double, Double))()
'Set up operations for addition, subtractions, multipication, and division
operations.Add("+", Function(l, r) l + r)
operations.Add("-", Function(l, r) l - r)
operations.Add("*", Function(l, r) l * r)
operations.Add("/", Function(l, r) l/r)
Dim result As Double = operations("+")(5, 5)
Dim result2 As Double = operations("*")(5, 5)
Console.WriteLine(result)
Console.WriteLine(result2)
End Sub
不,这是不可能的。最简单的方法是根据你的情况制作一个不同的计算函数。即'DoMath(1,2)' –