Modules - more information

Discussion in 'Beta Versions' started by Erel, Aug 26, 2008.

  1. Erel

    Erel Administrator Staff Member Licensed User

    From now on all code is written inside code modules.
    Each module can include code, forms and objects.
    Adding new modules is done by choosing Modules (menu) - Add New Empty Module or Add Existing Module.
    When you add an existing module, it will be copied to the source code folder.
    Modules files are saved with .bas extension.
    The main project file (with .sbp extension) is loaded as the Main module.

    Access modifiers:
    Each sub and each global variable can be declared as 'Private' - only accessible from their own module or 'Public' - accessible from other modules as well.
    By default all subs and variables are private.
    Instead of the Dim keyword you can use Private or Public to declare a global variable (Dim is equivalent to Private).
    Code:
    Sub Globals
      
    Public num, state
      
    Public Type (x,y) Point
      
    Private counter
      
    Dim a,b
      c = 
    4 'Private variable
    End Sub
    You can also add Public or Private before a sub declaration:
    Code:
    Public Sub ThisIsAPublicSub (minValue, maxValue)
    ...
    End Sub
    Note that declaring a private sub is equivalent to a default sub.

    Accessing public subs, variables, objects and controls of another module is done by writing the module name followed by a period and the required field.
    Code:
    Module1.Form1.Show
    It is only necessary to write the module name when accessing fields in other modules. However it is legitimate to also access the current module fields in this way (the pop-up list will show both public and private fields in this case).

    When you create a new form you can choose to create this form in a new module instead of the current one.
    You can also move existing forms from one module to another from the designer: Tools - Move Form.

    Control events subs should be located in the same module of the parent form.

    I've attached two small examples.
     

    Attached Files:

  2. agraham

    agraham Expert Licensed User

    I have been playing and it seems that automatic compiler allocated event Subs "must be located" rather than "should be located" as presumably the compiler wires ModuleName.ControlName events to ModuleName.ControlName_EventName.

    However for event Subs added at runtime by AddEvent it doesn't seem to matter where the Sub is declared

    I assume that at runtime a Control can have it's parent changed with no ill effect as the compiler allocated event Subs will now be wired to the Control and will not be changed.
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    That is correct. You can use AddEvent to wire an event to a sub which is located in another module.

    Do you mean with something like FormLib.ChangeParent?
    It will probably work.
     
  4. agraham

    agraham Expert Licensed User

    It better had! :) FormEx relies on it. Actually it does work (wipes brow!).
     
  5. Erel

    Erel Administrator Staff Member Licensed User

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