7

I've got a project in c# which is making use of another project written in vb.net. I am currently able to modify both.

I've got a method in the VB project like:

    Public Sub MethodName(ByVal param1 As String, ByRef param2 As String)
        param2 = param1 + 1
    End Sub

I am not able to call this method using the out keyword from C#:

    public void CallOtherMethod()
    {
        string param1 ="test";
        string param2;

        provider.AddTransaction(param1, out param2);
    }

Shouldn't the ByRef keyword in VB.Net have the capabilities of both "ref" and "out"?

Should I just go with ref?

Matthew Grima
  • 1,513
  • 5
  • 25
  • 40
  • 2
    My guess would be that `out` is the same as `ref` with an additional attribute, while `ByRef` is just `ref`. – Joey Aug 08 '11 at 08:51
  • A ref parameter needs initialising, but I don't need or have any use for that. I'm not really asking this because I'm stuck, I can easily go with using ref. – Matthew Grima Aug 08 '11 at 08:56
  • 3
    possible duplicate of [Is there a VB.NET equivalent of C# out parameters](http://stackoverflow.com/questions/4358742/is-there-a-vb-net-equivalent-of-c-out-parameters) – Tim Schmelter Aug 08 '11 at 08:57

4 Answers4

14

To the runtime ref and out are fairly interchangable, as they are both just passing a reference. However, out is prefixed with an additional attribute in the IL:

public void y(ref int a)
public void z(out int a)

turns into

.method public hidebysig instance void  y(int32& a)
.method public hidebysig instance void  z([out] int32& a)

which enables the C# compiler to distinguish the two and add the special semantics that out has, namely that an out parameter does not need to have an assigned value before entering the method and must be assigned a value before exiting the method.

In contrast, ByRef in VB only provides ref, but not the additional semantics of out. There it no equivalent of outin VB.

Joey
  • 344,408
  • 85
  • 689
  • 683
4

As mentioned in my question and answer you can specify an <Out()> attibute on the parameter definition and, although VB ignores it, C# treats the argument correctly.

Community
  • 1
  • 1
Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
3

There is no equivalent of out in VB (to my knowledge.) Just ByRef which is an equivalent of ref so that would be the correct choice.

Since you will be passing by reference the called function will recieve your value and you will need to intialise you parameter so that it has an address in memory.

EDIT:

Since you can edit both, why not change the Sub to a Function and get rid of the problem altogether and, IMO, make your code nicer too.

Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • I've got more than one parameter that needs returning in my actual project. I asked this question just for the sake of learning rather than a solution for my project. I'll just go with ref. Cheers. – Matthew Grima Aug 08 '11 at 09:02
  • 4
    You could return a class or structure. Just an idea. – Jodrell Aug 08 '11 at 09:05
2

Go with ref - it's the C# equivalent for VB's ByRef.

BUT Read this detailed explanation: When to use ref vs out.

Community
  • 1
  • 1
Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93