Java Question ByVal / Ref parameter in event

wl

Well-Known Member
Licensed User
Longtime User
Hello,

this might be a stupid question, but I have a library with a definition of some events being called by the Java code.

I would like to pass a variable of the event by ref so that, within the B4A code of the event, I can set this variable and the library code can react to this.

Thanks,

Wim

PS: Just noticed the subject of my post was wrong: it should have been "By Ref parameter in event" (can't edit the subject)
 
Last edited:

joseluis

Active Member
Licensed User
Longtime User
From here:
Basic4android follows Java parameter passing. Objects are passed by reference and "primitives" are passed by value.
Arrays are objects so are always passed by reference.
So if you want to pass Int, Float, or any primitive type by reference, they must be part of an object, either an array, a list, a custom type, etc.
 

wl

Well-Known Member
Licensed User
Longtime User
Thanks,

I once read this, but must have forgotten it meanwhile...:)
 

wl

Well-Known Member
Licensed User
Longtime User
OK, so I can pass an array of 1 boolean from my Java library code to be passed to B4A code, such as:

Boolean [] doContinue = { true };

raiseEventFromDifferentThread(null, this, myTask, eventName + "_download2completed", false, new Object[] {doContinue});

if (doContinue[0])
{
//do something: the user decided to continue
}

in the B4A event code I could modify:
doContinue(1) = false;

However: will this work ? Or would there be a synchronization problem (the library code and the UI code running on different threads) ? In other words: will the raiseEventFromDifferentThread block until the B4A event code has been fully executed ?

Thanks !
 

wl

Well-Known Member
Licensed User
Longtime User
Thanks Erel,

That was in fact what I was thinking. It means indeed a construction with an event with a value passed by reference (using an array with 1 element for example) - that could be set in the B4A eventcode - does not make any sense...

In a single threaded environment (or at least one where the method and the event are running in the same thread) this could be very usefull and makes code easier to read I tend to think.

Unfortunately I will need a multi-step process as you described.

/wl
 
Top