Jump to content

Local Static Var


 Share

Recommended Posts

#RequireAdm
AutoItSetOption("WinTitleMatchMode",2)

Func  makeOdrs()
Local  $AD=0.05
Local $PRICE=ControlGetText("try - DP0280","NSE",2639)
Local Static $st=True
Local $margin=9
If                           $st= True  Then
    $st=False
    Local $QTY= 3000/$PRICE*$margin; $margin already defined
    $QTY = Round($QTY)
    ControlSetText("try - DP0280","NSE",2640,$QTY);qty
    Local $wTITLE=WinGetTitle("try - DP0280","NSE")
        If              $wTITLE= "Sell Order Entry - DP0280" Then
        $AD  *= -1; to substract $AD from $PRICE if ‘sell order’
        EndIf
    $PRICE += $AD
    ControlSetText("try - DP0280","NSE",2802,$PRICE);trigger
    $PRICE += $AD
    ControlSetText("try - DP0280","NSE",2639,$PRICE);price
Else
    Local $wTITLE=WinGetTitle("try - DP0280","NSE")
        If $wTITLE= "Sell Order Entry - DP0280" Then
        $AD  *=-1
        EndIf
    ControlSetText("try - DP0280","NSE",2802,$PRICE);trigger
    $PRICE += $AD
    ControlSetText("try - DP0280","NSE",2639,$PRICE);price
EndIf

EndFunc

Hello Everybody,

I just joined the forum.I am new to scripting .This my first post.

A belated Happy New Year to all.

 i  am trying to write a function where  i want  a variable($QTY) to be assigned only in the first call.Using  $st as static variable to do so.But still the $QTY changes value on subsequent calls.Can any one please help me.The code is as above.

cheers & thanks

olmanR

Link to comment
Share on other sites

I do not see anything wrong with the posted part of your script.
Can you insert

ConsoleWrite($st & @CRLF)

after

Local Static $st=True

so you can see if the value of $st is as expected.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Any chance that $st is modified somewhere else in your script?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

 

Hi,

If initially assigned outside  "if" or  "while Wend" constructs , @consoleWrite shows true,false,false,false,false,false,.......But still $QTY changes on subsequent calls.I tried global static instead of local, with same result but an extra issue----> error:Cannot make existing variables static.:Pl have a look at the revised script.

Also the func agnAgn() is  looping only once.

#RequireAdm
AutoItSetOption("WinTitleMatchMode",2)
agnAgn()
;makeOdrs()
Func  makeOdrs()
        Global Static $st="True"
       Local  $AD=0.05
        Local $wTITLE=WinGetTitle("try - DP0280","NSE")
  If    $wTITLE= "Sell Order Entry - DP0280" Then
        $AD  *= -1; to substract $AD from $PRICE if ‘sell order’.now is -0.05
  EndIf
        Local $margin=9
        Local $PRICE=ControlGetText("try - DP0280","NSE",2639)
If      $st= True  Then
        $st=False
        Local $QTY= 3000/$PRICE*$margin
        $QTY = Round($QTY)
        ControlSetText("try - DP0280","NSE",2640,$QTY);qty
        $PRICE += $AD; trigger price
        ControlSetText("try - DP0280","NSE",2802,$PRICE); setting  trigger
        $PRICE += $AD ;price
        ControlSetText("try - DP0280","NSE",2639,$PRICE);setting price
Else
       $PRICE += $AD; trigger price
        ControlSetText("try - DP0280","NSE",2802,$PRICE); setting  trigger
        $PRICE += $AD ;price
        ControlSetText("try - DP0280","NSE",2639,$PRICE);setting price
EndIf
EndFunc
Func agnAgn()
    Local $x
    Local $i=0
    for  $x =0 To  10
        makeOdrs()
        $i +=1
      ConsoleWrite($x &@CRLF)
    Next
EndFunc

 

Link to comment
Share on other sites

$st is defined/used somewhere else in your script (what you posted is only a part of your script).
A little reproducer to test:

Global $test = True
_Test()

Func _Test()
    Global Static $test = False
    ConsoleWrite($test & @CRLF)
EndFunc   ;==>_Test

returns: "Cannot make existing variables static."

Remove line

Global $test = True

and you do not get the error.
 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Declaring "Global Static" is redundant and makes no sense...

agnAgn()

Func  makeOdrs($n)
      Local Static $st = True
      If $st= True  Then
           ConsoleWrite("$st=True" & ", makeOdrs=" & $n & @CRLF)
           $st=False
      Else
           ConsoleWrite("$st=False"& ", makeOdrs=" & $n & @CRLF)
     EndIf
EndFunc

Func agnAgn()
    for  $x = 1 To  10
        ConsoleWrite("for loop : " & $x &@CRLF)
        makeOdrs($x)
    Next
EndFunc

 

Link to comment
Share on other sites

Still don't understand why the OPs original code didn't work. There is already a Local Static $st in his first post.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Developers

Doesn't it make sense that the Quantity gets updated with each subsequent call as it is a Local variable defined each time in that first example?

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

5 hours ago, water said:

Still don't understand why the OPs original code didn't work. There is already a Local Static $st in his first post.

The only explanation I can think of is that the function might only have been called once in the script. Subsequent runs call the function for the first time. Perhaps this is a matter of misinterpretation of the keyword Static, or a faulty debugging procedure (all variables get destroyed when a script terminates). I cannot reproduce the problem.

Edited by czardas
Link to comment
Share on other sites

That's why I hate when users only post a few lines of code and tell us that the problem has to be there.
I should more often insist on a stripped down reproducer script :)
 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Me too. Posting the final solution to a problem is helpful for others users in the future. It's forum Netiquette.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

It isn't used on the forum or wiki but you get details here: https://en.wikipedia.org/wiki/Etiquette_in_technology#Netiquette

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

`Hi Guys
Sorry for the delayed post.
Been working on my code, given below which is working fine.
thanks for all the help & support
cheers
olmanR

Func setQty()
            Static Local $sta =True
             if $sta=True   Then
                $sta=False
                   Local $SYM=ControlGetText("try - DP0280","NSE",2657)
                     Local $prc=ControlGetText("try - DP0280","NSE",2639);price
                        Local $magn=Call("getMarg","$SYM")
                            Local $QTY=1000/$prc*$magn
                                $QTY = Round($QTY)
                                    ControlSetText("try - DP0280","NSE",2640,$QTY);qty
             EndIf

EndFunc

 

Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...