Out parameters, VB and C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michi Henning

    Out parameters, VB and C#

    Hi,

    I'm generating both VB and C# code from language-independent interface
    definitions, which is why I'm raising this issue. (The problem apppears
    to be somewhat esoteric, but it is real in my situation and is about
    more than just convenience.)

    C# has the notion of an out parameter (which is different
    from pass by value and pass by reference), but VB only understands
    pass by value and pass by reference. This means that parameters that
    notionally are out parameters end up as ByRef parameters in VB.
    I would like to find a way to have VB source code produce parameters
    that look like out parameters to C#.

    Here is a very simple example. Suppose I have the following in VB:

    ' VB:
    Public Sub setToFortyTwo(B yRef i As Integer)
    i = 42
    End Sub

    I bundle this method into an assembly that I call from C#.
    As it stands, I can call the method as:

    // C#:
    int i = 0; // Must initialize, because call is by reference
    setToFortyTwo(r ef i);

    What I would like to do instead is the following:

    // C#:
    int i; // No need to initialize for out parameters
    setToFortyTwo(o ut i);

    Is there some attribute I can use to instruct VB to mark the
    parameter as an out parameter in the generated Assembly?

    I had a look through the language reference, but I can't find
    anything applicable. But, possibly, there is some trickery with
    MarshalAs that could be used? Basically what I need is a way to
    tell the VB compiler that "this is *really* an out parameter and
    I want you to mark it as such in the assembly."

    I'd even consider post-processing the code that is generated by VB
    to change the parameter type, assuming that there is some documentation
    around that explains how the metadata is stored in an assembly or
    an object file. Can someone point me in the right direction for this,
    failing an easier solution?

    Thanks,

    Michi.
  • Jon Skeet [C# MVP]

    #2
    Re: Out parameters, VB and C#

    Michi Henning <michi@zeroc.co m> wrote:

    <snip>
    [color=blue]
    > Is there some attribute I can use to instruct VB to mark the
    > parameter as an out parameter in the generated Assembly?[/color]

    I *think* that you can use OutAttribute.

    Import System.Runtime. InteropServices

    and then use <Out> on the parameter declaration.

    It seems to work from a quick test, but you'll obviously want to look
    at it a bit more carefully.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Michi Henning

      #3
      Re: Out parameters, VB and C#

      Jon Skeet [C# MVP] wrote:
      [color=blue]
      > Michi Henning <michi@zeroc.co m> wrote:
      >
      > <snip>
      >[color=green]
      >>Is there some attribute I can use to instruct VB to mark the
      >>parameter as an out parameter in the generated Assembly?[/color]
      >
      >
      > I *think* that you can use OutAttribute.
      >
      > Import System.Runtime. InteropServices
      >
      > and then use <Out> on the parameter declaration.
      >
      > It seems to work from a quick test, but you'll obviously want to look
      > at it a bit more carefully.[/color]

      Thanks muchly for that! After much browsing through the doc, I just found
      this myself too. Works like a charm:

      ' VB:
      Public Sub setToFortyTwo(< System.Runtime. InteropServices .out()> ByRef i As Integer)
      i = 42
      End Sub

      This does the trick very nicely.

      Thanks again for your help, and I apologize for having shouted for help
      just that little bit too soon :-|

      Cheers,

      Michi.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Out parameters, VB and C#

        Michi Henning <michi@zeroc.co m> wrote:[color=blue]
        > Thanks muchly for that! After much browsing through the doc, I just found
        > this myself too. Works like a charm:
        >
        > ' VB:
        > Public Sub setToFortyTwo(< System.Runtime. InteropServices .out()> ByRef i As Integer)
        > i = 42
        > End Sub
        >
        > This does the trick very nicely.[/color]

        Excellent. I'd personally use an import and just have the parameter as
        (<Out> ByRef i as Integer)
        but that's a different story :)
        [color=blue]
        > Thanks again for your help, and I apologize for having shouted for help
        > just that little bit too soon :-|[/color]

        No problem at all - it's not entirely obvious from the docs. It should
        really be mentioned clearly in the ByVal vs ByRef documentation.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Mattias Sjögren

          #5
          Re: Out parameters, VB and C#

          Jon,
          [color=blue]
          >It should really be mentioned clearly in the ByVal vs ByRef documentation.[/color]

          Do you mean in the VB docs? I don't see why VB (or any other language
          for that matter) documentation should have to mention C#-isms such as
          out parameters. Instead, the C# docs for out should mention that it's
          really just a shorthand for [Out] ref.



          Mattias

          --
          Mattias Sjögren [MVP] mattias @ mvps.org
          http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
          Please reply only to the newsgroup.

          Comment

          • Michi Henning

            #6
            Re: Out parameters, VB and C#

            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1bb4ae 40a0bcf9c198b44 7@msnews.micros oft.com...[color=blue]
            > Michi Henning <michi@zeroc.co m> wrote:[color=green]
            > > Thanks muchly for that! After much browsing through the doc, I just found
            > > this myself too. Works like a charm:
            > >
            > > ' VB:
            > > Public Sub setToFortyTwo(< System.Runtime. InteropServices .out()> ByRef[/color][/color]
            i As Integer)[color=blue][color=green]
            > > i = 42
            > > End Sub
            > >
            > > This does the trick very nicely.[/color]
            >
            > Excellent. I'd personally use an import and just have the parameter as
            > (<Out> ByRef i as Integer)
            > but that's a different story :)[/color]

            Yes, normally, I would do just that. But, in this case, this is code generated
            by a compiler, and I have to be careful about name clashes between the
            generated code and user-defined symbols, so I can't import an entire
            namespace wholesale, for fear of symbol clashes.
            [color=blue][color=green]
            > > Thanks again for your help, and I apologize for having shouted for help
            > > just that little bit too soon :-|[/color]
            >
            > No problem at all - it's not entirely obvious from the docs. It should
            > really be mentioned clearly in the ByVal vs ByRef documentation.[/color]

            Yes, would be nice, wouldn't it?

            Cheers,

            Michi.

            --
            Michi Henning Ph: +61 4 1118-2700
            ZeroC, Inc. http://www.zeroc.com

            Comment

            • Michi Henning

              #7
              Re: Out parameters, VB and C#

              "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
              news:%23Hky1vOn EHA.2680@TK2MSF TNGP15.phx.gbl. ..[color=blue]
              > Jon,
              >[color=green]
              > >It should really be mentioned clearly in the ByVal vs ByRef documentation.[/color]
              >
              > Do you mean in the VB docs? I don't see why VB (or any other language
              > for that matter) documentation should have to mention C#-isms such as
              > out parameters. Instead, the C# docs for out should mention that it's
              > really just a shorthand for [Out] ref.[/color]

              No, this clearly belongs with VB. For example, VB may need to call into
              a DLL written in C# that uses out parameters. To some people, it may
              not be obvious how to pass the parameter. Or, alternatively, I may
              need to provide a callback function written in VB to a DLL written
              in C#. If that callback function uses out parameters, I need to know
              how to create an out parameter in VB.

              Cheers,

              Michi.

              --
              Michi Henning Ph: +61 4 1118-2700
              ZeroC, Inc. http://www.zeroc.com

              Comment

              • Mattias Sjögren

                #8
                Re: Out parameters, VB and C#

                [color=blue]
                >For example, VB may need to call into
                >a DLL written in C# that uses out parameters.[/color]

                They are seen as regular ByRef parameters from VB. The distinction
                between out and ref when calling such a method doesn't matter, since
                locals are always initialized in VB (so C# definite assignment rules
                don't apply).

                [color=blue]
                >Or, alternatively, I may
                >need to provide a callback function written in VB to a DLL written
                >in C#. If that callback function uses out parameters, I need to know
                >how to create an out parameter in VB.[/color]

                Not really. Again, you can just treat it as a regular ByRef.



                Mattias

                --
                Mattias Sjögren [MVP] mattias @ mvps.org
                http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                Please reply only to the newsgroup.

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Out parameters, VB and C#

                  Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:[color=blue][color=green]
                  > >It should really be mentioned clearly in the ByVal vs ByRef documentation.[/color]
                  >
                  > Do you mean in the VB docs? I don't see why VB (or any other language
                  > for that matter) documentation should have to mention C#-isms such as
                  > out parameters. Instead, the C# docs for out should mention that it's
                  > really just a shorthand for [Out] ref.[/color]

                  I think it should probably be mentioned in both C# and VB.NET
                  documentation, to be honest. (And it's not in the C# spec as far as I
                  can see, unfortunately.)

                  Given that it's something which can affect more languages than just C#
                  though, I don't see why it shouldn't be mentioned briefly in the VB.NET
                  docs though.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  Working...