Jump to content

Managing variables in and outside functions.


Recommended Posts

Hi all.

My problem is pretty simple.

I have a starting date i decide. I have a function that everytime i press a button in a guy it adds up a day to the previous said date with _DateAdd. This should simulate a monthly work routine, so, every 30 days, salary adds up day by day and the full salary is paid every 30 days.

Something like this

2010/06/30 Day 1; Starting date, Salary is 0$

Click on the button--> A day passes, salary +50$

2010/07/01 Day 2; Salary is 5$

Click on the button--> A day passes, salary +50$

2010/07/02 Day 3; Salary is 10$

Click on the button--> A day passes, salary +50$

2010/07/01 Day 4; Salary is 10$

......

Click on the button--> A day passes, salary +50$

2010/07/30 Day 30; Salary is 1500$

My question is: How i manage a function like this?

Global $StartingDate="2010/30/06"

Global $SalaryDate="2010/30/06"

Global $Daycount=1

Func DateAdd($Date)

Local $NewDate = $Date

$NewDate = _DateAdd('d', 1, $NewDate)

$StartingDate=$NewDate

$DayCount+=1

Salary($StartingDate)

Endfunc

I was thinking something like this

Func Salary($Day)

Local $StartCountDate=$Day

If $StartCountDate=$x=_DateAdd('d',30,$StartCountDate) Then

;PAY SALARY (i won't write this code as it's not the problem)

$SalaryDate=StartingDate

Endfunc

This is the raw code i had in mind. Could it work or there are more smart ways of doing it?

Link to comment
Share on other sites

nuibbone,

Variables declared as Dim/Global (although Global is deprecated) can be accessed (read/write) in any scope; i.e functions/while loop etc. This is a basic example of what I mean.

HotKeySet("{F8}", "_AddToDay")

Dim $intDate = 0

While 1
    Sleep(500) ; GUI event handling here
WEnd

Func _AddToDay()
    $intDate += 1 ; This is where you'd basically use _DateAdd

    ConsoleWrite($intDate & @CRLF)
EndFunc

I access the $intDate variable in the _AddToDay() function scope, adding one to its previous value (+= does that).

James

Link to comment
Share on other sites

  • Moderators

JamesBrooks,

I feel it is Dim that is deprecated (or should be according to Valik!) - Global and Local are alive and kicking! :mellow:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

JamesBrooks,

I feel it is Dim that is deprecated (or should be according to Valik!) - Global and Local are alive and kicking! :mellow:

M23

I'm sure that I've seen the world deprecated next to Global in the help file. Last time I looked was a few months ago though.

I use Dim over Global though - care to explain why I shouldn't?

James

Link to comment
Share on other sites

  • Moderators

JamesBrooks,

I clearly remember the conversation with Valik, but I cannot find the actual thread. His argument was that you should always declare the scope of variables explicitly - Dim relies on AutoIt applying the "inside/outside function" test, which might not be what you want in that particulr case.

This was also true for redeclaring arrays - use Global/Local again and not Dim.

His other argument is that you should always limit the number of Global variables to an absolute minimum - there was a thread about this not too long ago here where he offered some very sound advice.

M23

Edit: Wrong button too soon!

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

His other argument is that you should limit the number of Global variables to an absolute minimum - there was a thread about this not too long ago

So what should we use if he argues both ways? Posted Image

Here is his argument with an example.

In short, Dim should only be allowed to turn an existing variable into an array.

Edited by JamesBrooks
Link to comment
Share on other sites

  • Moderators

JamesBrooks,

Sorry, our posts crossed as I pressed the wrong button too soon.

Valik's view was that we should always use Global/Local, never Dim. But we should try and limit the Global variables to an absolute minimum. The thread I linked to has some good thoughts from Valik on how to do so.

M23

I have just seen your linked post that dates from 2006 - my conversation was much more recent and I seem to remember he was considering actually removing Dim from the lexicon. I will see if I can find it this evening.

Edit: Found it here

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

niubbone,

With plenty of study from you it will come sooner than you think! :mellow:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

So what should we use if he argues both ways? Posted Image

Here is his argument with an example.

How am I arguing both ways? I've never liked Dim and I've always used explicit Global or Local statements to declare my variables. I use Dim only to re-declare a previously declared variable as an array. This is the only safe use the keyword has. Were it up to me I would either remove it entirely, or I would modify it so that it could ONLY be used to re-declare an existing variable as an array. This is not arguing both ways.
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...