C# and VB.Net out parameters
Asked Answered
H

4

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?

Harvell answered 8/8, 2011 at 8:50 Comment(3)
My guess would be that out is the same as ref with an additional attribute, while ByRef is just ref.Toothbrush
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.Harvell
possible duplicate of Is there a VB.NET equivalent of C# out parametersDiggings
T
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.

Toothbrush answered 8/8, 2011 at 9:2 Comment(0)
P
5

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.

Phenetidine answered 13/8, 2011 at 13:56 Comment(0)
D
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.

Dozy answered 8/8, 2011 at 8:55 Comment(2)
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.Harvell
You could return a class or structure. Just an idea.Dozy
H
2

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

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

Hootchykootchy answered 8/8, 2011 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.