Announcement

Collapse
No announcement yet.

Globals vs Local and passing pointers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • John Petty
    replied
    Remember that if the variable is a string then VARPTR will remain constant but STRPTR may change if you change the contents.

    Leave a comment:


  • Mike Doty
    replied
    Since this is probably just logic these notes may not apply:

    1. Logic seems correct
    2. %UNKNOWN_VALUE used a CASE ELSE instead for %FALSE and unknown
    3. FUNCTION always returns %FALSE


    Code:
     
    FUNCTION SetGetLogErrors(ValueToSet AS LONG, ValueResults AS LONG, ResetValue AS LONG) AS LONG
      STATIC FunctionValue AS LONG                'Static to hold current value
      SELECT CASE ResetValue                      'Decide whether to Set or to Get the current value
     
        CASE %TRUE                                'If set to True then Reset the Current Value
            FunctionValue = ValueToSet            'Reset the value
            ValueResults = FunctionValue          'Return Results as a parameter
     
        CASE ELSE                                 ' If set to False, or -1 Then Get Current Value
            ValueResults = FunctionValue          'Return Results as a parameter
       END SELECT
    FUNCTION = %False                             'Return if Function Failed
    END FUNCTION

    Leave a comment:


  • Scott Turchin
    replied
    If you do a search here - Michael showed me a great way to pass pointers using the callback CALL - so you won't need ANY globals - albeit I still used a few..

    Works great -

    Leave a comment:


  • Michael Mattias
    replied
    As long as the variable in the procedure is STATIC or GLOBAL, you don't even need to pass the value to be set as a parameter, you can return it directly as the function's return value if convenient. (Although I, like your example, prefer to pass these values so I can use the return value as a "success/fail" indicator).

    'STATIC somevalue' in a procedure dedicated to "getting or setting" is a perfectly safe and valid way to maintain a global (small G) value... it's just that you will need a procedure call to get it when you need it instead of just using the name of a GLOBAL variable.

    If you were looking for something a tad geekier... the address of STATIC and GLOBAL variables does not change during a process; it remains the same from the time the module in which that variable is defined as STATIC or GLOBAL is loaded until it is unloaded.


    MCM

    Leave a comment:


  • Cliff Nichols
    started a topic Globals vs Local and passing pointers

    Globals vs Local and passing pointers

    One of the few things I do not do often (knowingly at least) is manipulate pointers. So I have to ask if I have a fatal flaw with a thought I had.

    If I do not use a Global, but instead a function to Set or Get a value and then locally have that value, would the following be valid?
    Code:
    FUNCTION SetGetLogErrors(ValueToSet AS LONG, ValueResults AS LONG, ResetValue AS LONG) AS LONG
              STATIC FunctionValue AS LONG                                                    'Static to hold current value
              SELECT CASE ResetValue                                                          'Decide whether to Set or to Get the current value
                   CASE %False, %UNKNOWN_VALUE                                                'If set to False, or -1 Then Get Current Value
                        ValueResults = FunctionValue                                          'Return Results as a parameter
                   CASE = %TRUE                                                               'If set to True then Reset the Current Value
                        FunctionValue = ValueToSet                                            'Reset the value
                        ValueResults = FunctionValue                                          'Return Results as a parameter
              END SELECT
              FUNCTION = %False                                                               'Return if Function Failed
         END FUNCTION
    If I change something in the value passed, do I need to change the pointer? aka update ValueToSet to VarPtr(Variable), or am I just "Lucking Out" that I have not run into a problem yet?
    Last edited by Cliff Nichols; 27 Jun 2009, 06:05 PM.
Working...
X