Alpha Video Training
Results 1 to 9 of 9

Thread: FYI: An alternative to creating Global Functions

  1. #1
    Alpha Software Employee Selwyn Rabins's Avatar
    Real Name
    Selwyn Rabins
    Join Date
    Mar 2000
    Location
    Boston, MA
    Posts
    5,299

    Default FYI: An alternative to creating Global Functions

    a user asked me this question:

    "i want to define a bunch of functions that can be used in multiple places, but I don't want to make each function into a global function (i.e. a function that is visible in the code tab of the control panel)"

    the solution is the xbasic compile_template() function.

    here is the answer i sent him:

    here is one way to define a whole bunch of functions that can be used in multiple places without having to make each function into a udf.


    1. define a string that contains all of the function definitions.

    string = ""%code%
    function foo as c ()
    ui_msg_box("","This is foo")
    end function

    function foo2 as c ()
    ui_msg_box("","This is foo2")
    end function

    function foo3 as c (name as c )
    ui_mg_box("","Hello : " + name)
    end function
    %code%

    2. when you want to use the functions in your code, get a pointer to them:

    dim p as p
    p = compile_template(string)

    3. to use a function, just call it, using the pointer as a prefix

    e.g.

    p.foo()
    p.foo2()
    p.foo3("name")

  2. #2
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,311

    Default RE: FYI: An alternative to creating Global Functio

    Selwyn,

    This could be very useful!

    1) What "scope" should be given to the pointer "p" if we want the functions to be "visible" throughout the app? i.e. to avoid having to "re-compile" the string each time we want to use one of its functions.

    2) It sounds as though the "string" could be stored in a memo field and retrieved for "compilation" in an autoexec script. Is that right?

    -- tom

  3. #3
    Alpha Software Employee Selwyn Rabins's Avatar
    Real Name
    Selwyn Rabins
    Join Date
    Mar 2000
    Location
    Boston, MA
    Posts
    5,299

    Default RE: FYI: An alternative to creating Global Functio

    you could make the pointer global if you wanted.

    you can give the pointer any scope that you want. it is just an a5 variable.

    compile_template() is very fast, making the pointer a local variable, and then doing compile_template() in each script that uses the functions is not going to result in a performance hit.

  4. #4
    Alpha Software Employee AaronBBrown's Avatar
    Real Name
    Aaron Brown
    Join Date
    Feb 2001
    Location
    Waltham, MA
    Posts
    2,836

    Default RE: FYI: An alternative to creating Global Functio

    Also keep in mind that, because it's a A5 pointer, they can be passed along to functions, as needed....

    funcs = ""%str%
    function foo as c ()
    end function
    %str%

    dim p as p
    p = compile_template(funcs)
    bar(p)


    function bar as c ( pFuncs AS P )
    bar = pFuncs.foo()
    end function

  5. #5
    "Certified" Alphaholic Marcel Kollenaar's Avatar
    Real Name
    Marcel
    Join Date
    Apr 2003
    Location
    Twente
    Posts
    1,514

    Default RE: FYI: An alternative to creating Global Functio

    Thanks...

  6. #6
    VAR
    Real Name
    Bill Parker
    Join Date
    Apr 2000
    Location
    Dallas, TX
    Posts
    1,714

    Default RE: FYI: An alternative to creating Global Functio

    Is there any practical difference between defining a group of functions this way vs. as a class?

    define class fooclass
    function foo as c ()
    ui_msg_box("","This is foo")
    end function

    function foo2 as c ()
    ui_msg_box("","This is foo2")
    end function

    function foo3 as c (name as c )
    ui_msg_box("","Hello : " + name)
    end function
    end class

    One thng I have noticed about a class is that Dim Private variables do seem to be public, e.g.
    ?p.myprivatevar gives a value.

    Bill.

  7. #7
    Alpha Software Employee AaronBBrown's Avatar
    Real Name
    Aaron Brown
    Join Date
    Feb 2001
    Location
    Waltham, MA
    Posts
    2,836

    Default RE: FYI: An alternative to creating Global Functio

    A class is a very different concept. Classes can be instantiated, be inherited, define interfaces, contain public and private members....

    For very very basic use, they do resemble each other, but they way they are implemented and used ends up being rather different.

  8. #8
    VAR
    Real Name
    Bill Parker
    Join Date
    Apr 2000
    Location
    Dallas, TX
    Posts
    1,714

    Default RE: FYI: An alternative to creating Global Functio

    I have not been able to create a private variable. If in a class definition I
    ...
    DIM private myvar as c
    myvar="a"
    ...

    then dim the class and get a pointer p to it.
    ?p.myvar should not return a value, but it does. The only variables that are not visible are local variables in a function of the class. But there is not much documentation on classes/interfaces.

    Bill.

  9. #9
    Alpha Software Employee AaronBBrown's Avatar
    Real Name
    Aaron Brown
    Join Date
    Feb 2001
    Location
    Waltham, MA
    Posts
    2,836

    Default RE: FYI: An alternative to creating Global Functio

    You're right, Bill. Currently private variables aren't private. Classes are not fully implemented, but be assured that in the future, A5 will be moving to a more Object Oriented Design and the class support will become very robust.

    My point is simply that compile_template() is very different conceptually than a class.

Similar Threads

  1. Problems updating global functions
    By gcaplan in forum Alpha Five Version 6
    Replies: 8
    Last Post: 07-06-2005, 01:46 PM
  2. Global Functions
    By Ken Styron in forum Alpha Five Version 5
    Replies: 10
    Last Post: 11-15-2004, 11:18 AM
  3. Creating a Global Calculated Field
    By Walter Lerch in forum Alpha Five Version 5
    Replies: 3
    Last Post: 04-08-2004, 05:40 AM
  4. Creating tabbed forms with skip functions
    By Theresa Watson in forum Upgrading to Alpha Five from Alpha Four
    Replies: 3
    Last Post: 09-19-2003, 07:03 AM
  5. global functions aren't global
    By richarddsmith in forum Alpha Five Version 4
    Replies: 5
    Last Post: 06-27-2002, 02:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •