calling class.method() from a function Topic is solved

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

calling class.method() from a function  Topic is solved

18 Sep 2018, 00:27

I have a class built for manipulating arrays.
Then a lot of other functions that do things.
I can get the methods to work when outside any functions, but not from within one.
What am i missing?

Code: Select all

Class ArrayManipulater {

	setArray(FullArray){
		...
	}

	setArrayElement(Element) {
		...
	}

	getArray() {
		...
	return FullArray
	}

	getArrayElement(WhichElement) {
		...
	return Element
	}
}
//// this method call inside function wont work.

Code: Select all

RandomFunction() {
...
...ValueForElement created
...
instanceOne.setArrayElement(ValueForElement)
}
//// but then i'm not in a function it does

Code: Select all

....
.... (not inside function)
instanceOne.setArray(ArrayToPassIn)
what am i missing here?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: calling class.method() from a function

18 Sep 2018, 02:15

The function doesn't have access to your class instance. Either declare the instance super global, declare it global from within the function, pass it as an argument or create it inside the function, to gain access to it.
valdikin

Re: calling class.method() from a function

18 Sep 2018, 02:26

Thank you swagfag! That answers it perfectly. I thought Classes were already global, thats my problem.

How do you pass as an argument, by instance name i assume? Function(InstanceName)

Thank you! this was driving my crazy...
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: calling class.method() from a function

18 Sep 2018, 13:19

Code: Select all

; superglobal
global instanceOne := new MyClass()

RandomFunction() {
	instanceOne.setArrayElement(ValueForElement)
}

Code: Select all

; global
instanceOne := new MyClass()

RandomFunction() {
	global instanceOne
	instanceOne.setArrayElement(ValueForElement)
}

Code: Select all

; argument
instanceOne := new MyClass()
RandomFunction(instanceOne)

RandomFunction(arg) {
	arg.setArrayElement(ValueForElement)
}

Code: Select all

; instantiate
RandomFunction() {
	instanceOne := new MyClass()
	instanceOne.setArrayElement(ValueForElement)
}
valdikin wrote:I thought Classes were already global, thats my problem.
this refers to something else entirely
[url]https://autohotkey.com/docs/Objects.htm#Custom_Classes[/url] wrote:When the script is loaded, this constructs an object and stores it in the global (or [in v1.1.05+] super-global) variable ClassName. To reference this class inside a force-local function (or an assume-local or assume-static function prior to [v1.1.05]), a declaration such as global ClassName is required.

Code: Select all

myFunc()

myFunc() {
	local

	; global MyClass
	instanceOne := new MyClass() ; 'MyClass' is a non initialized blank bar in this context
}

class MyClass
{
	__New() {
		MsgBox helloworld from new
	}
}
valdikin

Re: calling class.method() from a function

19 Sep 2018, 03:07

Thank you.
Very well laid out, that clears up my misunderstanding.

thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 106 guests