Search code examples
vb.netnarrowing

Overload resolution failed because no accessible 'Show' can be called without a narrowing conversion


I've been having issues regarding this narrowing conversion error

Overload resolution failed because no accessible 'Show' can be called without a narrowing conversion:

'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': Argument matching parameter 'owner' narrows from 'String' to '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': Argument matching parameter 'caption' narrows from 'Microsoft.VisualBasic.MsgBoxStyle' to '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': Argument matching parameter 'buttons' narrows from 'System.Windows.Forms.MessageBoxIcon' to 'System.Windows.Forms.MessageBoxButtons'.

'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': Argument matching parameter 'buttons' narrows from 'Microsoft.VisualBasic.MsgBoxStyle' to 'System.Windows.Forms.MessageBoxButtons'.

I did some research and the generic solution for "Overload resolution failed because no accessible '' can be called without a narrowing conversion: " errors is to Specify Option Strict Off according to Microsoft. I tried changing this manually in the Project Properties but it didn't seem to work.

This is the code where the error is occurring:

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

I've also checked several other forums where they talk about this error but specifically related to the 'New' function and they don't seem to help.

Any help on this would be great!


Solution

  • You've invoking Show({string}, {MsgBoxStyle}, {MessageBoxIcon}), so the last overload in the error message is the closest:

    'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': Argument matching parameter 'buttons' narrows from 'Microsoft.VisualBasic.MsgBoxStyle' to 'System.Windows.Forms.MessageBoxButtons'.

    That's Show({String}, {String}, {MessageBoxButtons}, {MessageBoxIcon}) - you're missing a caption argument, and instead of MsgBoxStyle you should use the MessageBoxButtons enum.

    Sounds like you have Option Strict On - that's good excellent - but it seems you also have Imports Microsoft.VisualBasic, which is essentially polluting your IntelliSense with VB6 back-compatibility stuff, which MsgBoxStyle is part of; that enum means to work with the legacy MsgBox function, which MessageBox is a more .NET-idiomatic replacement for.

    Switching off Option Strict would be the single worst thing to do - you're passing a bad parameter and the compiler is telling you "I can't convert the supplied type to the expected one"; last thing to do is to make it say "hey don't worry, just implicitly convert all the things and blow up at run-time instead".

    IntelliSense/autocomplete should be telling you what to do as you type the arguments into the function call; re-type the opening parenthese ( and watch IntelliSense highlight the parameters and their respective types as you use the arrow keys to move the caret across the arguments you're supplying.