C# & B4PPC: How correctly to pass arguments?

Discorez

Member
Licensed User
Longtime User
I have a my C#-library (for example "MyClass"), where, is available is such subs:

B4X:
public void Test(out string S)
        {
            S = "7";
        }

 public void Test2(ref string S)
        {
            S = "7";
        }

from B4PPC, I try to change the variable:

B4X:
K = "TEST"
MyClass.Test(K)
MyClass.Test2(K)
MsgBox(K)
Messagebox always shows "TEST", but not "7".
For example, variable K - is very big string and it need to change its without copying of its value.
How it can be implemented?
 

agraham

Expert
Licensed User
Longtime User
Basic4ppc doesn't support "out" and "ref" parameters. Just declare the parameters as plain "String" to pass them in. Strings are passed by reference so there is no copying involved. To return a String either return it from the method or pass a String(1) array and assign the new value to the array element 0.
 

mjcoon

Well-Known Member
Licensed User
... or pass a String(1) array and assign the new value to the array element 0.

Can you show us a snippet of code to do that? I understand that the Sub could refer to an array because all arrays must be global. But that does not count as "passing" because it could not appear as a parameter (given that I thought globals cannot also appear as parameters).

I have been using "ByRef paramName As String" in arguments and that works without needing to use any array.

I would like to pass an array of strings but I think will just have to have a side-effect global array which the Sub accesses.

Mike.
 

agraham

Expert
Licensed User
Longtime User
I thought globals cannot also appear as parameters
Only in a Sub declaration as a parameter name because a parameter is a local variable and so cannot have the same name as a global. Passing globals as arguments to a Sub or library method is no problem.

I have been using "ByRef paramName As String" in arguments and that works without needing to use any array.
I assume you mean in a Basic4ppc Sub declaration called within Basic4ppc. We are talking about library methods here. Basic4ppc doesn't know how to pass ByRef to a library method. I suspect that Discorez didn't try to compile his test as the compiler will throw an error while the IDE will behave as though the argument was passed by value.
 

Discorez

Member
Licensed User
Longtime User
I suspect that Discorez didn't try to compile his test as the compiler will throw an error while the IDE will behave as though the argument was passed by value.
Yes, I didn't compile in B4PPC my project, it only launched under IDE, for testing.
Therefore has made so that only in a class of library the parameters would be transferred by reference.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…