Android Question Private class variable "visibility".

LucaMs

Expert
Licensed User
Longtime User
1629730416831.png


This happens only inside the class itself (but anyway it shouldn't).
 
This happens only inside the class itself (but anyway it shouldn't)
Huh? The private designation is for OUTSIDE access to the global variables of a class, not internal access.
Update: I get that you are accessing the class within the class. Which, BTW, I never really thought of doing, but cool. Still stand by my point (but I could be wrong, see disclaimer below)
 
Upvote 0
Huh? The private designation is for OUTSIDE access to the global variables of a class, not internal access.
In that "example" (image) I wasn't try to access the variable (directly) but an object of that type.
In the real project I use a loop on a global map that contains "that" type of objects and execute it inside the class.
In short, I access many objects of that type from within the class.
 
Last edited:
Upvote 0
Even java does this (allow access to private within the same class). Attaching demo files.
 

Attachments

Upvote 0
Well, it shouldn't work that way.
If it didn't work that way you wouldn't be able to access it from anywhere. The idea of a private item is that you can use it within a class but it cannot be seen outside which reduces possible confusion as to what the public interfaces to the class are. Subs and variables in a class should all be private except those that you want the outside world to be able to twiddle with.
 
Upvote 0
Have you tried making Class_Globals private ?
eg,
Code: Copy
Private Sub Class_Globals
...
End Sub
 
Upvote 0
Maybe I did not say it clear enough; try again.

Suppose you have a clsPerson class, which has a public method:
B4X: Copy
Public Sub MsgFrom (SenderPerson As clsPerson, Msg As String)
' Here I shouldn't be able to access SenderPerson.[any private member]
End Sub
You mean that it shouldn't be accessible to another instance of the same class, but only to the instance that contains the property.

See if compiled it works. Java has the encapsulation system and so it shouldn't work if we understand it well

(I explain that you don't know English well)
 
Upvote 0
?‍♂️
 
Upvote 0
Have you tried making Class_Globals private ?
eg,
Code: Copy
Private Sub Class_Globals
...
End Sub
Tried. B4X doesn't get mad ?, it compiles and works.

Unfortunately the private variable is still visible (as are all its members, even routines).


P.S. B4X had no reason to get angry (and in fact it didn't) because the Class_Globals is rightfully private.
 
Last edited:
Upvote 0
Suppose you have a clsPerson class, which has a public method:
You have got to allow access to private variables from public Subs otherwise your class will be divided into public things and private things and the private things will be of no use at all as they cannot accept values from the public side. By only letting the outside world access public things you can control what they can and can't do to your class. Inside the class you can do whatever you want with both public and private things.

If you really mean the difference between class global and class instance variables then that is nothing to do with whether they are private or not - they can be either. Class globals are accessible from any instance of the class, class instances are unique to each instance of a class. Unfortunately B4X does not implement true class global variables.

Erel says (my boldening)
Class_Globals - This sub is similar to the activity Globals sub. These variables will be the class global variables (sometimes referred to instance variables or instance members).
Classes tutorial | B4X Programming Forum
 
Last edited:
Upvote 0
So, we have seen that this is the case in B4A, B4J (I assume also B4i), Java and VB Net.
also C++, TypeScript, C# etc. Sorry but we have 50 years of computer science and language design which has established these norms.
 
Upvote 0
Back
Top