[Wish] Private/Public scope

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'd like to be able do define variables/functions as private, so they dont show up in the intellisense in other modules.

Workaround for now, start private stuff with Z_
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
It is planned for the next version.

Awesome. I just encountered a similar problem. I had declared a global variable and a variable within a sub with the same names. And the system treated them as the same variable. When there is a conflict, could you rename the one in the sub to subname_variablename? Or even just warn us of the conflict?

I know you cant change the behavior since its working within java
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
This is by design and doesn't merit a warning. You have got to be able to redeclare global variables in order to obtain new instances of them.

The system didnt give them a new instance though, it treated them as the same variable.

Hence why it merits a warning
 

agraham

Expert
Licensed User
Longtime User
It did make a new instance. A rather artifical example

B4X:
Sub Globals
   Dim Btn as Button
End Sub

Sub SubOne
    Btn.Initialize("BtnOne") ' the original instance of a Button
    ...
End Sub

Sub SubTwo
    Dim Btn As Button ' a new instance of a Button
    Btn.Initialize("BtnTwo")
    ...
End Sub
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Well it didnt for me in a code module.

I had a variable in Process_Globals, and dimmed a new instance in a sub, the sub modified the one in Process_Globals

I wouldnt have made this topic if it didnt do it that way.
 

moster67

Expert
Licensed User
Longtime User
Perhaps you should then upload a part of your project which clearly shows the problem so Erel can have a look at it......

Well it didnt for me in a code module.

I had a variable in Process_Globals, and dimmed a new instance in a sub, the sub modified the one in Process_Globals

I wouldnt have made this topic if it didnt do it that way.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Perhaps you should then upload a part of your project which clearly shows the problem so Erel can have a look at it......

I fixed it ages ago, I'm not going to break my code for this.

That's exactly what should happen.

You just told me that didnt happen.
Thats exactly how it shouldnt work, hence why I asked for a rename/warning.
 

kickaha

Well-Known Member
Licensed User
Longtime User
NeoTechni,

You obviously do not understand what is going on, so here is a simpified version.

When you re-Dim a variable, it creates a new instance of it in that it is allocated new memory space - but you will no longer be able to access the old instance with the variable name as that is now pointing to the new instance - but the old instance can still be used internally (as part of an array etc).

So agraham is right, there is a new instance, its just that you cannot directly see or use the old one!
 

thedesolatesoul

Expert
Licensed User
Longtime User
Yes, it took me a while to understand this too.
Dim = new instance
Its a good thing I did a tiny bit of java before b4a, so I eventually got this.

Problem with Public variables is, when you pass it around in functions (as references) totally messes things up. So I make sure I declare a new instance within a function and return that instance and assign it back to the original.
hello = function(hello)
That should also take care of garbage collection.
Otherwise Re-Dimming all around the park will give the garbage collectors a run for their money.

kickaha,
as you said the original instance is still somewhere in memory unaccessible. If it is a global variable, how can we reclaim the variable? Isnt this a memory leak?
 

agraham

Expert
Licensed User
Longtime User
as you said the original instance is still somewhere in memory unaccessible. If it is a global variable, how can we reclaim the variable? Isnt this a memory leak?
No it's' not a memory leak and it's not necessarily inaccessible. For a View you could maybe receive it as the Sender of an event or get it via Activity.GetView, or even from another variable if you had assigned it. Once no further references to an object instance exists it becomes a candidate for garbage collection.

You shouldn't worry about references and "messing things up". Just write neat code and let the garbage collector do it's thing. Using Bitmaps is probably the only time when you should even consider the effect of garbage collection and memory use and only because a Bitmap is a very small managed object (which the garbage collector knows about) but holds a very large native object (about which the garbage collector know nothing).
 

kickaha

Well-Known Member
Licensed User
Longtime User
kickaha,
as you said the original instance is still somewhere in memory unaccessible. If it is a global variable, how can we reclaim the variable? Isnt this a memory leak?
Thats not what I said, I said
you will no longer be able to access the old instance with the variable name
As agraham has explained (and I alluded to), it is not necessarily inaccessible.
 

agraham

Expert
Licensed User
Longtime User
its really hard to know if you have left a reference to an object you dont want to use anymore so I guess no point thinking about that.
Correct. Basic4android is designed so it is very hard to create memory leaks - which is quite easy to do in Android if you write in Java and are careless with your View objects. This is the reason why there are both Globals and Process_Globals.

is that cray in your avatar? :)
Yes! :)
http://www.b4x.com/forum/forum-discussion/5336-agraham-given-innovator-medal-2.html#post31732

http://www.b4x.com/forum/forum-discussion/5336-agraham-given-innovator-medal-3.html#post31736
 

thedesolatesoul

Expert
Licensed User
Longtime User
:sign0138: speechless ...
when my friends were growing up looking at superheroes in comic books, i was looking at supercomputers in computer books...hoping that someday when i grow up i will make a contribution to computer science when it was much more pure...not like today with so much commercial motivations...pangs of nostalgia
right now i do not know what to say to you agraham :sign0188:, i will talk to you later when i get back to my senses!
 
Top