I was recently bitten with a self-inflicted bug when using Map declared data.
I did this:
Expecting that Map1 would then contain “some content” and Map2 would contain “some other content”.
But it was pointed out to me that assigning a Map to a Map is a reference assignment and the above logic results in Map1, Map2, and A_Map all referencing the same “some other content”.
I checked the Beginner’s User Guide, the User Guide, and also the Rapid Android App Development book and did not see an explanation about this. Searching the forums to see if there was more information about “Map” I got swamped with hits for using “Google maps”.
I also see code examples where a Map is used without performing the .Initialize method on the Map, even though the Guides say that this is required. Looking again at these examples it seems that this may be OK if the uninitialized Map is first “assigned” to another Map, which is to say that it becomes a Map reference.
As someone with (too) many years of ‘c’ experience behind me I am quite comfortable with pointers and handling references to values. But perhaps the syntax and rules for using B4A Maps can be made a bit more explicit and obvious?
I did this:
A_Map = “some content”
Map1 = A_Map
A_Map = “some other content”
Map2 = A_Map
Map1 = A_Map
A_Map = “some other content”
Map2 = A_Map
Expecting that Map1 would then contain “some content” and Map2 would contain “some other content”.
But it was pointed out to me that assigning a Map to a Map is a reference assignment and the above logic results in Map1, Map2, and A_Map all referencing the same “some other content”.
I checked the Beginner’s User Guide, the User Guide, and also the Rapid Android App Development book and did not see an explanation about this. Searching the forums to see if there was more information about “Map” I got swamped with hits for using “Google maps”.
I also see code examples where a Map is used without performing the .Initialize method on the Map, even though the Guides say that this is required. Looking again at these examples it seems that this may be OK if the uninitialized Map is first “assigned” to another Map, which is to say that it becomes a Map reference.
As someone with (too) many years of ‘c’ experience behind me I am quite comfortable with pointers and handling references to values. But perhaps the syntax and rules for using B4A Maps can be made a bit more explicit and obvious?