[Gambas-user] accessing a variable from another class
Jussi Lahtinen
jussi.lahtinen at gmail.com
Thu Apr 20 20:36:10 CEST 2023
There seems to be a lot of confusion. Compilation does not change the scope
of variables (or objects) within the project.
But if you want to use the executable as a component/library, then you must
expose the public variables of the desired classes with "export".
Otherwise, it is not needed.
"Dim" declares a local variable, whose scope is within the function/sub.
"Private" declares a variable, whose scope is within the class.
"Public" declares a variable, whose scope is the whole project. And also
other projects when "export" is used.
"Static" just means the sub/function is shared with all instances of this
class, and that you do not need to instantiate this class to use the
function/sub. IE it is not needed for the scope at all.
So. You can create class2 which inherits class1 as its base class, and thus
"own" private and public variables of class1 as its own variables.
Thus you can example write within function/sub of class2:
thisIsPrivateVariableFromClass1 = 100
Outside of the class1 or class2 the private variables are not visible. IE
class1.thisIsPrivateVariableFromClass1 and
class2.thisIsPrivateVariableFromClass1 yields an error.
Jussi
On Thu, Apr 20, 2023 at 5:22 PM Mayost Sharon <sharon at 455.co.il> wrote:
> Hello
>
> In previous discussions on this topic
> I was told that what I was trying to do did not make sense
>
> After thought and examination
>
> In Gambas it is possible to access and change variables that are in
> another class
> By
> STATIC PUBLIC
>
> It also exists in other languages, for example:
>
> https://www.w3docs.com/snippets/java/accessing-a-variable-from-another-class.html
>
> So it makes sense to do such a thing
>
> The question is if I create several classes for example
> class1 class2 class3
> Is there a possibility that only class2 or class3 can read from class1
> That is, when I am in class2 it will be possible to read:
> class1.var1
>
> But when I compile all 3 classes
> It will not be possible from the outside to read the variable:
>
> dim cl1 as class1
> cl1.
> After the DOT it will not show me the variable var1
>
> Thank you very much everyone
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230420/939ed59d/attachment.htm>
More information about the User
mailing list