none
Overloaded Functions and Extensions RRS feed

  • Question

  • I was curious about the nature of Extensions in VB and overloading.

    I have a function that handles the release of memory for me, since sometimes the the GC isn't quite fast enough to verify that an object has been cleared.  I have two versions of the function, one is normal, the other is an Extension.

    Module Funcs
     Public Sub Free(ByRef self as Object, ByVal Collect as Boolean)
     end Sub
    End Module
    
    Module Extensions
      <System.Runtime.CompilerServices.Extension()> _
      Public Sub Free(Of T)(ByRef self as T)
      End Sub
    End Module

    Now, I am aware that VB allows for Reference Passed extensions, where C# does not, and thus the second function is pretty much ignored for my C# projects.  However, in my VB projects, it is claiming that the first "non-extension" function doesn't exists.  The overload doesn't seem to reach beyond the extension, and I was curious as to why?

    I can access the Funcs.Free() if I prefix it with "Funcs." but when I leave it just Free(obj, true) it claims there are too many arguments for the extension.

    Thanks

    Jaeden "Sifo Dyas" al'Raec Ruiner

     


    "Never Trust a computer. Your brain is smarter than any micro-chip."
    PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.
    Wednesday, May 12, 2010 7:40 PM

Answers

  • Hi Jaeden,

    I checked this issue again, you can try to change the name of the extension method to myfree or something else. Since some types have already declare the method Free().


    Hope this helpful to you! If you have any further quetions, please feel free to let me know.
    Please mark the right answer at right time.
    Bset Regards,
    Tracy
    • Marked as answer by SamAgain Monday, May 24, 2010 2:34 AM
    Monday, May 17, 2010 7:05 AM
  • Hi,

       If I understand it right, you want to implement 2 methods in 2 modules to improve the GC of your object. One is in a normal Module method and the other is an Extension method.  I am not sure why it has to be implented like this. Could you let us know your specific scenario?

       I wrote the following code to test your design: 

    Module Module1
     Sub Main()
      Dim obj As New T
      Free(obj, True) <----------------------!!!
     End Sub
    End Module
    
    Module Funcs
     Public Sub Free(ByRef self As Object, ByVal Collect As Boolean)
     End Sub
    End Module
    
    Module Extensions
     <System.Runtime.CompilerServices.Extension()> _
     Public Sub Free(Of T)(ByRef self As T)
      Console.WriteLine("extension")
     End Sub
    End Module
    
    Class T
    
    End Class

        At the marked line above, the compiler complaits that the Free method is ambiguous between the Funcs module and Extensions module. So I change the code to below:

    Module Module1
    
     Sub Main()
      Dim obj As New T
      Free(obj, True)
    
     End Sub
    End Module
    
    Module Funcs
     Public Sub Free(ByRef self As Object, ByVal Collect As Boolean)
     End Sub
    End Module
    
    Module Extensions
     <System.Runtime.CompilerServices.Extension()> _
     Public Sub Free2(Of T)(ByRef self As T) <-------------------Changed Name!
      Console.WriteLine("extension")
     End Sub
    End Module
    
    Module Module3
     Public Sub Free() <-----------------------Newly added module!!
      Console.WriteLine("do nothing");
     End Sub
    End Module
    
    Class T
    
    End Class

        This time, the ambiguity is between the Funcs module and Module3 module.

        If we put all the 3 Free methods in the same module like below:

    Module Funcs
     Public Sub Free(ByRef self As Object, ByVal Collect As Boolean)
     End Sub
     Public Sub Free()
      Console.WriteLine("do nothing");
     End Sub
     <System.Runtime.CompilerServices.Extension()> _
     Public Sub Free(Of T)(ByRef self As T) <---------- the extension method
      Console.WriteLine("extension")
     End Sub
    End Module
    

        There'll be no ambiguity at all.   And only the extension method can be accessed via the instance method syntax.

        I am not sure about the root cause of this. But I suggest to put the methods in the same module or just rename them as a work around.


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Thursday, May 20, 2010 11:49 AM refine
    • Marked as answer by JaedenRuiner Sunday, May 23, 2010 5:33 AM
    Thursday, May 20, 2010 11:48 AM

All replies

  • Hi Jaeden,

    I checked the relevant MSDN articles, and find that you can define an extension method on most types that can be represented in a Visual Basic parameter list, including the following:

    1. Classes(reference types)
    2. Structures(value types)
    3. Interface
    4. Delegates
    5. ByRef and ByVal arguments
    6. Generic method parameters
    7. Arrays

    So, it seems you can not declare a extension for the same method. From your codes, it seems you are going to add a extending method free which has been existed. You can refer to the link below for more detials:

    1. Extension Methods

    Good luck and have a nice day!


    Hope this helpful to you! If you have any further quetions, please feel free to let me know.
    Please mark the right answer at right time.
    Bset Regards,
    Tracy
    Monday, May 17, 2010 7:03 AM
  • Hi Jaeden,

    I checked this issue again, you can try to change the name of the extension method to myfree or something else. Since some types have already declare the method Free().


    Hope this helpful to you! If you have any further quetions, please feel free to let me know.
    Please mark the right answer at right time.
    Bset Regards,
    Tracy
    • Marked as answer by SamAgain Monday, May 24, 2010 2:34 AM
    Monday, May 17, 2010 7:05 AM
  • Hi,

       If I understand it right, you want to implement 2 methods in 2 modules to improve the GC of your object. One is in a normal Module method and the other is an Extension method.  I am not sure why it has to be implented like this. Could you let us know your specific scenario?

       I wrote the following code to test your design: 

    Module Module1
     Sub Main()
      Dim obj As New T
      Free(obj, True) <----------------------!!!
     End Sub
    End Module
    
    Module Funcs
     Public Sub Free(ByRef self As Object, ByVal Collect As Boolean)
     End Sub
    End Module
    
    Module Extensions
     <System.Runtime.CompilerServices.Extension()> _
     Public Sub Free(Of T)(ByRef self As T)
      Console.WriteLine("extension")
     End Sub
    End Module
    
    Class T
    
    End Class

        At the marked line above, the compiler complaits that the Free method is ambiguous between the Funcs module and Extensions module. So I change the code to below:

    Module Module1
    
     Sub Main()
      Dim obj As New T
      Free(obj, True)
    
     End Sub
    End Module
    
    Module Funcs
     Public Sub Free(ByRef self As Object, ByVal Collect As Boolean)
     End Sub
    End Module
    
    Module Extensions
     <System.Runtime.CompilerServices.Extension()> _
     Public Sub Free2(Of T)(ByRef self As T) <-------------------Changed Name!
      Console.WriteLine("extension")
     End Sub
    End Module
    
    Module Module3
     Public Sub Free() <-----------------------Newly added module!!
      Console.WriteLine("do nothing");
     End Sub
    End Module
    
    Class T
    
    End Class

        This time, the ambiguity is between the Funcs module and Module3 module.

        If we put all the 3 Free methods in the same module like below:

    Module Funcs
     Public Sub Free(ByRef self As Object, ByVal Collect As Boolean)
     End Sub
     Public Sub Free()
      Console.WriteLine("do nothing");
     End Sub
     <System.Runtime.CompilerServices.Extension()> _
     Public Sub Free(Of T)(ByRef self As T) <---------- the extension method
      Console.WriteLine("extension")
     End Sub
    End Module
    

        There'll be no ambiguity at all.   And only the extension method can be accessed via the instance method syntax.

        I am not sure about the root cause of this. But I suggest to put the methods in the same module or just rename them as a work around.


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Thursday, May 20, 2010 11:49 AM refine
    • Marked as answer by JaedenRuiner Sunday, May 23, 2010 5:33 AM
    Thursday, May 20, 2010 11:48 AM
  • Hi,

       I tried to call the normal module method Funcs.Free forcibly, the compiler complaints about ambiguity between Func.Free and Extension.Free.

       As to why the extension methods beat the module method, with the same name and different signature, I think you could contact the VB.NET compiler team by posting to the http://connect.microsoft.com/ to get the most authoritative responses.


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by JaedenRuiner Sunday, May 23, 2010 5:33 AM
    • Unmarked as answer by JaedenRuiner Sunday, May 23, 2010 5:33 AM
    Thursday, May 20, 2010 12:00 PM
  • Hi,

      How is the problem going on? Could you share your progress?


    Please mark the right answer at right time.
    Thanks,
    Sam
    Friday, May 21, 2010 9:28 AM
  • Sorry,

    Been a bit since I've checed MSDN, got lost in getting some work done.  I ended up resolving by not even using the extension method, since I go back and forth between C# and VB, and as we all know, C# doesn't allow reference extensions. 

    (void Free<T>(this ref T self) <--error)

    I was just curious is anyone knew why the "overload" is discarded in deference to the extension.  probably something to do with the compiler, and I haven't had a chance (or time) to check out further in depth on the specifics of the issue. 

    Thanks for your investigations on the subject as well, and I guess it is just good to note:  Extensions and Functions cannot overload each other in VB. (Haven't tested in C# yet if the same would occur).

    The primary reason I had the separated module was to put the Free extension in the VBExtensions namespace so as to reduce confusion over reference passed extensions that aren't allowed in C#, so I would never use the VBExtension namespace in a c# app.

    Thanks

    J"SD"a'RR


    "Never Trust a computer. Your brain is smarter than any micro-chip."
    PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.
    Sunday, May 23, 2010 5:30 AM