Toggle in Function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guest

Toggle in Function

14 Nov 2014, 23:53

Greetings,

I'm trying to make this toggle, which works:

Code: Select all

t::
toggle := !toggle
	
if (toggle) {
	msgbox Toggled On
} else {
	msgbox Toggled Off
}
return
...into a function, which doesn't work as of yet:

Code: Select all

ToggleSwitch() {
	toggle := !toggle
	
	if (toggle) {
		msgbox Toggled On
	} else {
		msgbox Toggled Off
	}
}

t::
ToggleSwitch()
return
Thanks for any guidance and advice offered.

Shan
Coco-guest

Re: Toggle in Function

15 Nov 2014, 00:10

You need to initialize toggle and make it a static variable, the latter so that value is remembered between calls:

Code: Select all

ToggleSwitch() {
    static toggle := false ;// initialize
    if (toggle := !toggle) {
        msgbox Toggled On
    } else {
        msgbox Toggled Off
    }
}
Guest

Re: Toggle in Function

15 Nov 2014, 00:29

Coco-guest wrote:You need to initialize toggle and make it a static variable, the latter so that value is remembered between calls:
That was the missing piece of the puzzle I needed. It works as intended now. Thanks a million for your help!

Shan
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Toggle in Function  Topic is solved

15 Nov 2014, 11:34

Just to say that you quite possibly do not want a static var for this.

What is it that you want to toggle? If you are using another function to check the status of the toggle, you will not be able to, because it is private to the ToggleSwitch() function.

What you want is either to make toggle global, or have ToggleSwitch be a class method, and make toggle a property of the class.

So EITHER:

Code: Select all

toggle := 0

ToggleSwitch() {
    global toggle
    toggle := !toggle
}

CheckToggleStatus(){
   global toggle
    if (toggle) {
        msgbox Toggled On
    } else {
        msgbox Toggled Off
    }
}
OR

Code: Select all

Class CToggle {
   __New(){
      this.toggle := 0
   }

   ToggleSwitch() {
       this.toggle := !this.toggle
   }

   CheckToggleStatus(){
       if (this.toggle) {
           msgbox Toggled On
       } else {
           msgbox Toggled Off
       }
   }
}

tog := New CToggle
User avatar
Menixator
Posts: 69
Joined: 30 Sep 2013, 04:10

Re: Toggle in Function

17 Nov 2014, 06:18

evilC wrote: If you are using another function to check the status of the toggle, you will not be able to, because it is private to the ToggleSwitch() function.
Then just add a little if statement like this:

Code: Select all


ToggleSwitch(true) ;// get value

ToggleSwitch(get := false) {
    static toggle := false ;// initialize
    if (get){
    	return static
    }
    if (toggle := !toggle) {
        msgbox Toggled On
    } else {
        msgbox Toggled Off
    }
}
Guest

Re: Toggle in Function

18 Nov 2014, 00:19

evilC wrote:Just to say that you quite possibly do not want a static var for this.

What is it that you want to toggle? If you are using another function to check the status of the toggle, you will not be able to, because it is private to the ToggleSwitch() function.
For now, I'm just using very simple menu commands in my app to be toggled. For my learning process of functions, I wanted to get just a basic toggle in a function operating first prior to adding any arguments etc.
evilC wrote:What you want is either to make toggle global, or have ToggleSwitch be a class method, and make toggle a property of the class.
Both of these help enormously! I've been trying to understand the AHK approach to Classes recently and your example cleared up many questions I had. Thanks for taking the time to post both of these examples. The knowledge acquired has been used already.

Shan
Guest

Re: Toggle in Function

18 Nov 2014, 00:22

Menixator wrote:
evilC wrote: If you are using another function to check the status of the toggle, you will not be able to, because it is private to the ToggleSwitch() function.
Then just add a little if statement like this:

Code: Select all


ToggleSwitch(true) ;// get value

ToggleSwitch(get := false) {
    static toggle := false ;// initialize
    if (get){
    	return static
    }
    if (toggle := !toggle) {
        msgbox Toggled On
    } else {
        msgbox Toggled Off
    }
}
Interesting alternative approach. Thanks for taking the time to post this example.

Shan

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 169 guests