Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric method overloading compilation error in VB
    primarykey
    data
    text
    <p>I have a problem with the VB.NET compiler failing to compile a class (in a separate C# assembly) which contains two overloads of a method with generic arguments. The equivalent code in C# compiles against the same assembly with no errors.</p> <p>Here are the two method signatures:</p> <pre><code>protected void SetValue&lt;T&gt;(T newValue, ref T oldValue) protected void SetValue&lt;T&gt;(T? newValue, ref T? oldValue) where T : struct </code></pre> <p>Here is the code to three assemblies that demonstrate the problem. The first is the C# assembly with a Base class that implements the generic methods. The second is a C# class derived from Base and calls both overloads of SetValue correctly. The third is a VB class also derived from Base, but fails to compile with the following error message:</p> <p>Error 1 Overload resolution failed because no accessible 'SetValue' is most specific for these arguments: 'Protected Sub SetValue(Of T As Structure)(newValue As System.Nullable(Of Integer), ByRef oldValue As System.Nullable(Of Integer))': Not most specific. 'Protected Sub SetValue(Of T)(newValue As System.Nullable(Of Integer), ByRef oldValue As System.Nullable(Of Integer))': Not most specific. </p> <ol> <li><p>Base Class assembly</p> <p>example:</p> <pre><code>public class Base { protected void SetValue&lt;T&gt;(T newValue, ref T oldValue) { } protected void SetValue&lt;T&gt;(T? newValue, ref T? oldValue) where T : struct { } } </code></pre></li> <li><p>C# Derived Class</p> <p>example:</p> <pre><code>public class DerivedCSharp : Base { private int _intValue; private int? _intNullableValue; public void Test1(int value) { SetValue(value, ref _intValue); } public void Test2(int? value) { SetValue(value, ref _intNullableValue); } } </code></pre></li> <li><p>VB Derived Class</p> <p>example:</p> <pre><code>Public Class DerivedVB Inherits Base Private _intValue As Integer Private _intNullableValue As Nullable(Of Integer) Public Sub Test1(ByVal value As Integer) SetValue(value, _intValue) End Sub Public Sub Test2(ByVal value As Nullable(Of Integer)) SetValue(value, _intNullableValue) End Sub End Class </code></pre></li> </ol> <p>Am I doing something wrong in the VB code, or are C# &amp; VB different when it comes to generic overload resolution? If I make the method arguments in Base non-generic then everything compiles correctly, but then I have to implement SetValue for every type that I wish to support.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload