Jump to content
olmanRvr

Local Static Var

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

Share this post


Link to post
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 (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hey Water,

Yes I tried your suggestion,

ConsoleWrite($st & @CRLF)

Every time it returns  True. But after the  "$st=True Then" ,it was  set to false.

As I understand, as a static variable,  $st should be false after the first call.

Please correct my concept if wrong. And help

thanks & cheers

olmanR

Share this post


Link to post
Share on other sites

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


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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

 

Share this post


Link to post
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 (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Hi Water & mikell,

Thanks a lot to both of you.After removing that "Global Static" declaration as suggested, now the code is running smooth.I look forward  to you all for possible issues in future .

cheers

olmanR

Share this post


Link to post
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 (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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

Share this post


Link to post
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 (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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 (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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 (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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

×
×
  • Create New...