【问题标题】:Overload resolution failed because no accessible 'Show' can be called without a narrowing conversion重载解决失败,因为没有缩小转换就无法调用可访问的“显示”
【发布时间】:2018-05-04 16:58:24
【问题描述】:

我一直有关于缩小转换错误的问题

重载解析失败,因为没有缩小转换就无法调用可访问的“显示”:

'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': 参数匹配参数'owner' 从 'String' 缩小到 'System.Windows.Forms.IWin32Window'。

'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': 参数匹配参数“caption”从“Microsoft.VisualBasic.MsgBoxStyle”缩小到“String”。

'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': 参数匹配参数“按钮”从“System.Windows.Forms.MessageBoxIcon”缩小到“System.Windows.Forms.MessageBoxButtons”。

'Public Shared Function Show(text As String, caption As String, button As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': 参数匹配参数“按钮”从“Microsoft.VisualBasic.MsgBoxStyle”缩小到“System.Windows.Forms.MessageBoxButtons”。

我做了一些研究,“重载解析失败,因为没有缩小转换就无法调用可访问的''的通用解决方案:”错误是根据 Microsoft 指定 Option Strict Off。我尝试在项目属性中手动更改它,但它似乎不起作用。

这是发生错误的代码:

If MessageBox.Show("Please Enter a value for ESD (rad)", "ESD (rad) Value", MsgBoxStyle.OkCancel, MessageBoxIcon.Information) = DialogResult.OK Then
            txtCal_USE_Radio.Focus()

我还查看了其他几个论坛,他们在其中讨论了此错误,但与“新建”功能特别相关,但似乎没有帮助。

对此的任何帮助都会很棒!

【问题讨论】:

  • 错误告诉你什么是错误的 2ce。 Microsoft.VisualBasic.MsgBoxStyle.OkCancel 用于旧版 MsgBox(),与 Winforms 命名空间中的 MessageBoxButtons.OKCancel 不同。如果您愿意,Intellisense 将帮助您解决这些问题。请阅读How to Ask 并采取tour

标签: vb.net narrowing


【解决方案1】:

您调用了Show({string}, {MsgBoxStyle}, {MessageBoxIcon}),因此错误消息中的最后一个重载是最接近的:

'Public Shared Function Show(text As String, caption As String, button As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': 参数匹配参数“按钮”从“Microsoft.VisualBasic.MsgBoxStyle”缩小到“System.Windows.Forms.MessageBoxButtons”。

那是Show({String}, {String}, {MessageBoxButtons}, {MessageBoxIcon}) - 你缺少caption 参数,你应该使用MessageBoxButtons 枚举而不是MsgBoxStyle

听起来你有Option Strict On - 这很好 非常好 - 但你似乎也有Imports Microsoft.VisualBasic,这实质上会污染你的 IntelliSense 与 VB6 向后兼容性东西,MsgBoxStyle 是其中的一部分;该枚举意味着使用旧版 MsgBox 函数,MessageBox 是一个更符合 .NET 习惯的替代品。

关闭Option Strict 将是最糟糕的事情 - 你传递了一个错误的参数并且编译器告诉你“我无法将提供的类型转换为预期的类型”;最后要做的是让它说“嘿别担心,只是隐式转换所有东西并在运行时爆炸”。

IntelliSense/autocomplete 应该会在您将参数键入函数调用时告诉您要做什么;重新键入左括号 ( 并观察 IntelliSense 在您使用箭头键在您提供的参数之间移动插入符号时突出显示参数及其各自的类型。

【讨论】:

  • 感谢您的帮助!我从另一个研究过它的人那里得到了这段代码,结果一团糟!没有 cmets,他们没有给我任何方向,所以这对我有很大的帮助!
【解决方案2】:

您正在将您的 MessageBox 与 MsgBox 混合使用将 MsgBoxStyle.OkCancel 更改为 MessageBox 语法。

If MessageBox.Show("Please Enter a value for ESD (rad)", "ESD (rad) Value", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = DialogResult.OK Then

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多