none
[VB]Referencing a ByRef Extension from a C# Project RRS feed

  • Question

  • I have a VB.NET Extension Method (in a VB.NET project) declared as follows:
    <Extension()> Public Sub Load(Of T)(ByRef value As T)
    I reference this project from a C# project. However, this extension method is not listed. But if I remove the ByRef, it is listed. I realize that it is not always good to mix languages, but I didn't want to convert 100s of classes and work if I could avoid it (and they are both .NET and are in separate projects, so shouldn't it not matter?). Why is the ByRef causing a problem? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, October 18, 2019 4:16 PM

All replies

  • Hi,

    Since it's a C# problem, so it will be more suitable to ask about this in C# forum. I'll help you to move this thread to C# forum and you can get more help there.

    Best regards,

    Roy


    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 MSDNFSF@microsoft.com.

    Monday, October 21, 2019 5:42 AM
  • Hi Nathan Sokalski, 

    Thank you for posting here.

    According to your description, you may need to add ‘ref’ when you call T in C#.

    Here’s a reference you can refer to.

    C# Equivalent to VB 'ByRef' Parameters

    Hope it can help you.

    Besides, if I have any misunderstanding, please provide more information about your function.

    Best Regards,

    Xingyu Zhao


    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 MSDNFSF@microsoft.com.

    Monday, October 21, 2019 6:54 AM
    Moderator
  • In C#, extensions can take a ref parameter for structures and simple types like int, double, etc., therefore if you change the VB definition to

       <Extension()>

       Public Sub Load(Of T As Structure)(ByRef value As T)

          . . .

       End Sub

     

    then C# should accept it.

    If you cannot make such change, then you can call it using a different (long) syntax. For example:

       string s = "some text";

       SomeNamespace.SomeModule.Load( ref s );

     



    • Edited by Viorel_MVP Monday, October 21, 2019 8:13 AM
    Monday, October 21, 2019 8:12 AM
  • C# does not support extension methods on ref/out parameters outside value types at this time. You can see the discussion around the issues with it and the proposed solution here.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, October 21, 2019 2:12 PM
    Moderator
  • That would not work for 2 reasons:

    1. It would constrain the Type parameter to a Structure, which I do not want.

    2. If I add the ByRef, it is not accepted or listed in Intellisense for C#


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, October 21, 2019 3:29 PM