Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;=======================
- ; Creating a class with a customized enumerator.
- ; This will allow the class to be passed to a for-loop.
- ; Unlike normal enumerators, this one uses 3 parameters.
- ; See the for-loop example below the class.
- class test_class {
- ; Define some properties to iterate through.
- static Name := 'GroggyOtter'
- static Age := 100
- static URL := 'www.reddit.com/r/AutoHotkey'
- static SSN := 123456789
- ; A for-loop always looks for an __Enum() method.
- ; If it finds it
- static __Enum(num_of_vars) {
- ; Normally, AHK's built-in enumerators only
- ; use 1-2 parameters.
- ; However, we're going to enforce 3 parameters
- if (num_of_vars != 3)
- throw(Error('3 parameters must be provided.'
- , A_ThisFunc
- , 'for key, value, value_type in test_class'))
- ; Make a call descriptor object. The assigned
- ; function will be called by the for-loop.
- enum := {call:get_next_prop}
- ; Make a list of properties to include.
- ; We skip SSN because we don't want it to
- ; show up while iterating through test_class.
- ; If propreties are listed in reverse order,
- ; .Pop() can be used, which is faster than .RemoveAt(1).
- enum.list := ['URL', 'Age', 'Name']
- ; And we need a reference to the
- ; class so we can get the values
- enum.class := this
- ; Return the enumerator to the for-loop.
- ; It can use it to loop through each specified item.
- return enum
- ; The function that gets called each
- ; iteration of the for-loop
- ; Normally, only 1-2 parameters are used.
- ; A For-loop can handle up to 19 VarRefs
- ; This example shows how to use three.
- get_next_prop(this, &prop, &value, &v_type) {
- ; When there are no properties left
- if (this.list.Length < 1)
- ; Return false to indicate none left
- return 0
- ; Get next property name
- prop := this.list.Pop()
- ; And its value
- value := this.class.%prop%
- ; And its type
- v_type := Type(value)
- }
- }
- }
- ; For-looping through test_class
- ; Notice it uses 3 parameter values
- ; key, value, and value type
- for key, value, value_type in test_class
- MsgBox('Property: ' key
- '`nValue: ' value
- '`nType: ' value_type)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement