Android Tutorial Variables and Subs visibility

Discussion in 'Tutorials & Examples' started by Erel, Jun 12, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Basic4android v2.00 adds two new access modifiers: Public and Private.
    This tutorial will cover the rules which determine when a variable or a sub can be accessed from outside.

    Note that the default accessibility is Public.

    Variables
    - Activities, services and code modules: process global variables are public by default.
    Activity global variables are always private.
    You can hide process global variables by using the Private modifier:
    Code:
    Sub Process_Globals
     
    Dim ThisIsAPublicVariable As String 'public
     Public ThisIsAPublicVariableToo As String 'exactly the same as Dim.
     Private ThisIsAPrivateVariable As String 'private
    End Sub
    - Classes: sames as above for variables declared in Class_Globals.

    Subs
    - Subs declared in Activity and Service modules are private.
    - Subs declared in Code modules and Class modules are public by default.
    You can hide such subs with the Private modifier:
    Code:
    Private Sub ThisIsAPrivateSub(x As Int)

    End Sub
    The Public modifier can also be used (though it doesn't have any effect):
    Code:
    'Public subs
    Sub S1

    End Sub

    Public Sub S2
     
    End Sub
    CallSub and CallSubDelayed can be used to call subs in other services, activities and classes. These methods can be used to access both private and public subs.
     
    Devan likes this.
  2. salmander

    salmander Active Member Licensed User

    This is sweet..... Thanks Erel.
     
  3. rbsoft

    rbsoft Active Member Licensed User

    That is great - very flexible.

    Rolf
     
  4. Peter Simpson

    Peter Simpson Expert Licensed User

    Yea ha, nice one Erel :thumbup:

    Sent from my A100 using Tapatalk 2
     
  5. Djembefola

    Djembefola Active Member Licensed User

    Yes! Yes! Yes!

    :sign0008:

    This was the top item in my wishlist!
    Thank you so much.

    Next item:

    Public Property Set
    Public Property Get
     
  6. mjtaryan

    mjtaryan Active Member Licensed User

    Hi Erel,

    I have a generally related question. How many times does a Process Global variable have to be declared?

    I ask because I declared a variable in Sub Process_Global in the Activity Main and use it in both Main and in a second activity, call it "Second." The variable is boolean and I check its value in the Activity_Create in Activity Second.

    During compiling I get the message that it was used before it was declared. So, I'm confused.
     
  7. moster67

    moster67 Expert Licensed User

    Did you check it using the module name before (main.myboolean)?

    example:

    Code:
    if main.myboolean = true then
     
    ''''
    end if
     
  8. mjtaryan

    mjtaryan Active Member Licensed User

    AH! :sign0161:

    No, I hadn't -- didn't think I needed to, I'm so used to declaring globals only at the beginning of the package's start up module and being able to use them like any other variable. THANKS!
     
  9. Ennesima77

    Ennesima77 Member Licensed User

    I to everyone,
    How can I do if I need to call a sub with parameters?
    Code:
    Sub SetSpinnerID(ID as int, Refill as boolean)
    I tried to use CallSub(Main, "SetSpinnerID(1, False)") but it doesn't work.
    How can I solve it?
     
    Last edited: Aug 30, 2012
  10. klaus

    klaus Expert Licensed User

    Code:
    FillSpinner(TrueFalse)
    If the routine is in the Main module.

    Best regards.
     
  11. Ennesima77

    Ennesima77 Member Licensed User

    I correct the code.

    Code:
    Sub SetSpinnerID(ID as int, Refill as boolean)
    the sub is in the main activity, but I need to call it from a secondary activity.

    I try to explain what I use it for:

    In the main activity there are 2 spinner, first is the Tournament list and second is the Players List. Near the spinner there are two (button one for each spinner) and button call Tournament Activity a Players Activity where user can add/modify/delete etc..
    At the end, when Tournament or Players Activity were closed, I need to refill spinner and set the tournament or players ID in the spinner.

    Thanks for your kindly help!
     
    Last edited: Aug 30, 2012
  12. klaus

    klaus Expert Licensed User

    You should have your data in Process_Global variables.
    In the Edit activities you change the data and then when coming back the Spinners are updated.

    Best regards.
     
  13. Ennesima77

    Ennesima77 Member Licensed User

    Isn't possible use a sub or function like a Class Module?
    Like DBUtils, I will call Main.SetSpinnerID(1,false).
    I need to declare a lot a variable in this way and in a big project may be use a lot of memory resource.:sign0085:
     
  14. klaus

    klaus Expert Licensed User

    When you call another activity from the Main activity you are not sure that that yo will have all the data when you return.
    You should read about the activity life cycle, either in this tutorial , in the Wiki or in the Beginner's Guide.

    Best regards.
     
  15. MrKim

    MrKim Active Member Licensed User

    OH MAN, I have been fighting that ALL day. I wish there was somewhere that told us the rules. I am SOOOO used to VBA. A global is a global, there is no ned to reference the declaring module unless it is a form. But I am getting used to it. Love the program.
     
    Jo Muller likes this.
  16. mjtaryan

    mjtaryan Active Member Licensed User

    I have three LIstView2 objects the items for each which I want to format using the SPListView library. Therefore, I'd like to put the formatting sub in a code module and call it from the appropriate activities. In an effort to accomplish this I tired declaring each of the ListView2 objects in Process_Globals. However, I get this error message:

    ----------
    Parsing code. 0.05
    Compiling code. Error
    Error compiling program.
    Error description: Cannot access activity object from sub Process_Globals.
    Occurred on line: 12
    Dim lstTOC As ListView2 'The actual Table of Contents
    Word: listview2
    ----------


    Does this mean a view cannot be declared in Process_Globals or is there another reason for the error? Is there a way to correct this? Thanks.
     
  17. klaus

    klaus Expert Licensed User

    Yes, you cannot dim a view in Process_Globals !
    But you can transfer it in the calling routine as a parameter.

    Best regards.
     
  18. mjtaryan

    mjtaryan Active Member Licensed User

    How should I declare It in the parameter list of the called sub? That is, should I declare It as an Object, a View, a ListView2 or what? Thanks.
     
    Last edited: May 24, 2013
  19. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice