SyntaxHighlighter

Thursday, 9 April 2015

Overload Resolution Failed Because No Accessible Is Most Specific For These Arguments

I have a method which has two overloads where I can pass one of two object types. In some cases, these objects may be Nothing or null. When this is the case, you will almost certainly get the Overload resolution failed because no accessible is most specific for these arguments compile error.

It's quite simple to sort as you you literally just cast the Nothing or null to the type you want it to be processed as. For example:

In C#
public void MyMethod(object1 o)
{
    //Some code...
}

public void MyMethod(object2 o)
{
    //Some code...
}

...

MyMethod((object1)null);

In VB.Net
Public Sub MyMethod(o As object1)
    'Some code...
End Sub

Public Sub MyMethod(o As object2)
    'Some code...
End Sub

...

MyMethod(Cast(Nothing, object1))