Android Question parameters in subs

Markos

Active Member
Licensed User
Longtime User
Hi All,

I have what may be a fundamental question to most avid b4a developers but I was wondering if the following code would update the byte array buff1 and if the code is correct. If not for a byte array would it update a basic type such as int or string etc?

sub check
dim buff1() as byte

checkthis (buff1)
msgbox(buff1(1),"Test")
end sub

sub checkthis(buff2() as byte)
buff2(0)=56
buff2(1)=99
end sub
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Did you try it?

In Java (and therefore B4J and B4A), objects are passed by reference and primitives are passed by value. Arrays are objects, so they are passed by reference, meaning that buff2 in your checkthis sub is a reference to the same array as buff1 in your sub check.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Hi All,

I have what may be a fundamental question to most avid b4a developers but I was wondering if the following code would update the byte array buff1 and if the code is correct. If not for a byte array would it update a basic type such as int or string etc?

sub check
dim buff1() as byte

checkthis (buff1)
msgbox(buff1(1),"Test")
end sub

sub checkthis(buff2() as byte)
buff2(0)=56
buff2(1)=99
end sub


The call should be
buff1 = checkthis(buff1)

The sub must also specify the return type eg:
Sub checkthis(buff2() as byte) as byte
buff2(0) = 56
buff2(1) = 99
return buff2
end sub
 
Upvote 0

Markos

Active Member
Licensed User
Longtime User
The call should be
buff1 = checkthis(buff1)

The sub must also specify the return type eg:
Sub checkthis(buff2() as byte) as byte
buff2(0) = 56
buff2(1) = 99
return buff2
end sub

Oh that looks great. I will try that to update the variable proper. I was gonna do that but thought it may have been redundant. Kool.

Cheers
 
Upvote 0
Top