Make var in Class.Method1() accessible for Class.Method2()

Get help with using AutoHotkey and its commands and hotkeys
tmplinshi
Posts: 1560
Joined: 01 Oct 2013, 14:57

Make var in Class.Method1() accessible for Class.Method2()

16 Dec 2013, 06:49

For example:

Code: Select all

Class phpBB
{
	Login(url, user, pwd) {
		static ForumUrl := "url"
		; Login ...
	}

	NewTopic(Title, Content) {
		MsgBox, % This.Login().ForumUrl "/posting.php?mode=post&f=5"
		; Post new topic ...
	}
}

phpBB.Login("http://ahkscript.org/boards", "u", "*")
phpBB.NewTopic("title", "content")
Return
How can I save the url in Login(), and retrieve the url in NewTopic(), the This.Login().ForumUrl is incorrect.

==========================================================

Currently I'm using something like global url in every methods, e.g.:

Code: Select all

Class test
{
	m1() {
		global url
	}

	m2() {
		global url
	}

	; ...
}
==========================================================

And I can't code like this, because the url is dynamic.

Code: Select all

Class test
{
	static url := url
}
Last edited by tmplinshi on 16 Dec 2013, 08:18, edited 1 time in total.
User avatar
TLM
Posts: 1582
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Retrieve variable in Class.Method()

16 Dec 2013, 07:35

You were very close with the static assignment
Try something like this

Code: Select all

Class phpBB
{
    Login(url, user, pwd) {
        static ForumUrl := "url"
        ; Login ...
        return url
    }

    NewTopic(Title, Content) {
        MsgBox, % This.Login().ForumUrl "/posting.php?mode=post&f=5"
        ; Post new topic ...
    }
}

url := phpBB.Login("http://ahkscript.org/boards", "u", "*")

msgbox % url

phpBB.NewTopic("title", "content")
Return
htms
tmplinshi
Posts: 1560
Joined: 01 Oct 2013, 14:57

Re: Retrieve variable in Class.Method()

16 Dec 2013, 07:45

Thanks for the quick reply, TLM.
However, that's not I'm trying to do. I want the NewTopic() to retrieve some variable in Login().
User avatar
TLM
Posts: 1582
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Retrieve variable in Class.Method()

16 Dec 2013, 08:18

sorry about that, I misread the question.

Code: Select all

Class phpBB
{
    Login(url, user, pwd) {
        static ForumUrl := "url"
        ; Login ...
        this.url := url
    }

    NewTopic(Title, Content) {
        MsgBox, % this.url "/posting.php?mode=post&f=5"
        ; Post new topic ...
    }
}

phpBB.Login("http://ahkscript.org/boards", "u", "*")
phpBB.NewTopic("title", "content")
Return
tmplinshi
Posts: 1560
Joined: 01 Oct 2013, 14:57

Re: Make var in Class.Method1() accessible for Class.Method2

16 Dec 2013, 08:23

Cool, Thanks a lot!
Sorry for my poor english. I can't describes this question easily.

Return to “Ask For Help”

Who is online

Users browsing this forum: Ben, Bing [Bot], Forgetabyteit, Google [Bot], kdservices, ManualColdSwitch, tee_rock, vsub and 137 guests