Announcement

Collapse
No announcement yet.

Globals vs Local and passing pointers

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

  • 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, 07:05 PM.
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    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
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      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 -
      Scott Turchin
      MCSE, MCP+I
      http://www.tngbbs.com
      ----------------------
      True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

      Comment


      • #4
        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
        CMD shortcut to open any location:
        %windir%\system32\cmd.exe /k " cd\ & x: & cd x:\xxxx
        Change to run as administrator
        How long is an idea? Write it down.

        Comment


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

          Comment

          Working...
          X