Android Question Passing a reference to an object

davemorris

Active Member
Licensed User
Hi, Guys
I am having a problem - I am using B4X pages and have setup an object to store information used by may classes and pages in my application.

In the App this global object (with a public attribute) resides in the B4XMainPage, however when testing is can reside elsewhere.
So I have tried to pass a reference of this object in various calls to pages/classes, attempting to avoid using explicit code like
B4XPages.MainPage.ObjectName.ElementName = newDate (i.e. when testing it can reside in one of the test modules).

Unfortunately, using a reference does not update the global object (it appears to only update a local copy of the object) - the explicit code does however work.

I have used this method of passing a reference to an object in other languages and it works well, and by not using explicit code to access a fixed global object makes the classes more re-usable and easier to test.

I hope you can understand my question, if so what I am doing wrong?


Dave Morris
 

William Lancee

Well-Known Member
Licensed User
Longtime User
It sounds complicated. Can you give us a small example project?
I don't know what means "however when testing it can reside elsewhere."
An object is an object that resides in one place with potentially many references.
 
Upvote 0

davemorris

Active Member
Licensed User
Hi, Wil
Thanks for the response - I see you point I will try to produce some small example project. Obviously, you are correct only one object can exist - but in my case I have created multiple objects (all based on the same class) but containing different or preset data. So when I am testing a class/module/page which uses that class, I simply point the class to be tested at the appropriate object containing test data. (See you point - I will get started on the simple project - even trying to explaining it makes of appear more complex, but this technique does simplify code and makes testing much easier).

Thanks again for the help so far.

Dave
 
Last edited:
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Perhaps this is the base of the problem:
Obviously, you are correct only one object can exist - but in my case I have created multiple objects (all based on the same class) but containing different or preset data.
I see a great danger here that (perhaps depending on the sequence) when processing the information, updated information may be lost. Isn't it therefore better to define all objects in the B4XMainPage and refer to them from the other B4XPages? You can find here an example how to retrieve from different B4XPages the current global objects defined and updated in the B4XMainPage. Don't forget to add the routine to retrieve the object when a B4XPage come active and a routine for update the global object when leave the B4XPage.
 
Upvote 0

davemorris

Active Member
Licensed User
Hi, Guys

Thanks for all the help - I managed to find a coding fault relating to what I believe as the passing of a reference to an object and this reference was passed by value causing a problem which appeared to be lost data. Firstly, I must say that everybody's input was important and helped me to find this difficult (for me) to find fault.

Wil's and Lucams Suggestions -
Can you give us a small example project?

Well I tried this and could not duplicate the problem (so it must be my bad code!)

Agraham's
It should work fine if your 'object' is a non-primitive. Arrays, lists, maps, custom types should all work as expected

They did, as I found out when I tried to demonstrate with a simple example.

Finally, MicroDrie comments hit the nail on the head.
I see a great danger here that (perhaps depending on the sequence) when processing the information, updated information may be lost

What happened in my code When the test sub ran, it simply replaced the real data with test data and called classes to be tested.
Unfortunately, I am a great believer from my "C" language and Assembler days "don't move the data change the pointer". So that was how the test subs changed data. Unfortunately when a reference (pointer) to data is passed by value, then the caller is unaware of the change and its reference still points to the old data and the called sub points to the new data which is lost on the return. (I am fully aware of the C# "ref" keyword which is suppose to overcome this problem - but this is B4X)

I can only say that putting the main reference on the Left hand side of an expression can cause problems:

i.e. myMainReference = newReference the other way around appears to be ok i.e. newReference = myMainReference and is not effected if the reference is passed "by value" or "by reference".

I have never been a big advocate of global objects (suggested by MicroDrie) but if your code becomes complex this problem with references can be a lot harder to find than problems caused by using global objects.

Thanks again for the help
Dave Morris
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Unfortunately, I am a great believer from my "C" language and Assembler days "don't move the data change the pointer".
I completely agree with the statement that it is better to change a pointer than to move data. Referring to just a global variable is actually exactly what happens, unlike when you approach the class as an object. Then you indeed copy data and you run into the problem of how to record the last updated value and you have to move data again. Sometimes a solution is only as close as possible.
 
Upvote 0
Top