我正尝试通过字符串变量 (cal_Operation
) 发送操作符号,但出现错误。
注意事项:
- label1 将保存结果
- text1 包含将对其进行计算的两个数字之一
- text2 保存两个数字中的第二个数字 对它们的计算
- cal_Operation string 包含运算符号(- or + or/or - )
代码是:
Dim cal_Operation As String
Public newops = New Dictionary(Of String, Func(Of Double, Double, Double))() From {
{"+", Function(x, y) x + y},
{"-", Function(x, y) x - y},
{"*", Function(x, y) x * y},
{"/", Function(x, y) x / y}
我有单选按钮将操作分配给字符串变量
在按钮中我插入这段代码
RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
cal_Operation = "+"
Dim op1 = newops(cal_Operation)
Label1.Text = CStr(op1(CDbl(TextBox1.Text), CDbl(TextBox2.Text)))
End Sub
但是我得到了这个错误
An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll Additional information: No default member found for type 'Func(Of Double,Double,Double)'.
是什么原因及解决方法?
最佳答案
您需要更改 newops
字典的声明并明确其类型
Public newops as Dictionary(Of String, Func(Of Double, Double, Double)) =
New Dictionary(Of String, Func(Of Double, Double, Double))() From {
{"+", Function(x, y) x + y},
{"-", Function(x, y) x - y},
{"*", Function(x, y) x * y},
{"/", Function(x, y) x / y}}
关于vb.net - 将数学运算符分配给没有 IF 的变量 - System.MissingMemberException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35240381/