vb.net - 将数学运算符分配给没有 IF 的变量 - System.MissingMemberException

标签 vb.net

我正尝试通过字符串变量 (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/

相关文章:

.net - 如何修改 .net 的 BackgroundWorker 类,以便它在方法完成时触发自定义事件

c# - 如何让 ASP.NET 页面知道文件何时可以从服务器下载

jquery - 无法访问更新面板生成的后面跨度中的按钮

c# - 使用 vb.net 声明一个 IsInDesignMode 属性以在 wpf 中使用

vb.net - vb.net 中的 LINQ 与数据集

vb.net - 防止 VB.NET 的多行/逐字字符串破坏您的整个代码文件?

c# - 从字符串中删除所有特殊字符

vb.net - 通过 COM 接口(interface)向 VBA 公开 .NET DataTable 属性

c# - 非静态私有(private)或 protected 事件存在哪些用例?

c# - System.IO.FileShare 有限制吗?