[VB]Generic Methods: Regular vs Array RRS feed

  • Question

  • I have two generic extension methods, one for a regular value and one for an array. Here are the declarations:
    <Extension()> Public Sub Save(Of T)(ByRef value As T)
    <Extension()> Public Sub Save(Of T)(ByRef value As T())

    As you can see, the only difference is that one parameter is a regular value, and the other is an array. However, I receive an error saying that neither one is most specific. I thought that whether the value was an array or not would determine which one to use, but I guess that is not true. Is there anything I can do other than create separate methods (give the methods different names)?

    Nathan Sokalski

    Sunday, July 28, 2019 3:24 PM

All replies

  • Hi,

    Since the problem is more related to VB, I'll move this thread to VB forum and you could get more support there.

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, July 29, 2019 7:31 AM
  • Interesting problem.  I'm guessing it is combining extension and generic.  If you code your two Save subs without <Extension()>, and if you code your calls as Save(whatever) instead of whatever.Save, then it will work as you want it to.  So you have an out.

    This result raises a problem in my mind.  How does the compiler know to call the 'As T()' sub rather than the 'As T' sub when an array is passed?  The 'As T' sub just needs an object of some type and an array is an object, so the same ambiguity seems to exist.  But the code compiles cleanly and when you run, the right Save() is called depending on the argument.  I don't get it, but it seems to work.

    Monday, July 29, 2019 10:11 AM
  • Hi Nathan,

    You can do this.>>

    Imports System.Runtime.CompilerServices
    Module MyExtensions
        <Extension()> Public Sub Save(Of T)(ByRef value As T)
        End Sub
        <Extension()> Public Sub Save(Of T)(ByRef value As Array)
        End Sub
    End Module

    Then call the extensions like this.


    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Call Test()
        End Sub
        Private Sub Test()
            Dim num As Integer = 9876504
            Dim myStringArray() As String = {"Hello", "World", "how", "are", "you", "all", "doing", "today", "?"}
            'Alternatively call the ARRAY method with this and then you are passing the TYPE
            ' to the ARRAY method.
            myStringArray.Save(Of String)
        End Sub
    End Class

    I hope this helps. :-)




    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Tuesday, July 30, 2019 2:00 AM