Req. help with Object Property set() method

Get help with using AutoHotkey and its commands and hotkeys
ahbi
Posts: 16
Joined: 15 Jun 2016, 17:11

Req. help with Object Property set() method

18 Apr 2018, 11:00

Can I get some help in writing my first set method?

Up until this point I have basically used AHK objects as arrays with some functions (which you know ...) , but now I am trying to branch out.

I have a simple object with 5 properties. Every time I change Prop#1 or Prop#2, I want Prop#3 to also change. At the moment I am working on Prop#1's set().

My code is

Code: Select all

class iconClass
{
	; XCoordRel := 0
	; YCoordRel := 0
	; XYCoordRelPacked := 0
	; RelToHWND 
	; IconName := ""

	__New(nIconName, XCoord, YCoord, nRelToHWND=false)
	{
		this.IconName := nIconName
		this.RelToHWND := nRelToHWND
		this.XCoordRel := XCoord
		this.YCoordRel := YCoord
		this.XYCoordRelPacked := (XCoord & 0xFFFF) | ((YCoord & 0xFFFF) << 16)
		return this
	}
	
	XCoordRel
	{
		set
		{
			XCoord := value
			YCoord := this.YCoordRel
			this.XYCoordRelPacked := (XCoord & 0xFFFF) | ((YCoord & 0xFFFF) << 16)
			return value
		}
	}
	
	toString()
	{
		tmpStr := this.IconName ":" this.XYCoordRelPacked ":" this.XCoordRel "+" this.YCoordRel 
		return tmpStr
	}
}
What I am getting, when I call the constructor, is XCoordRel & XYCoordRelPacked never being set (i.e. null/empty)

Code: Select all

iconObj := new iconClass("this is icon", 123, 789, hwWindow)

; outputs
; this is icon::+789
; note lack of XCoordRel & XYCoordRelPacked
After that, using the set() method gets me a little bit further but it is still broken

Code: Select all

...
iconObj.XCoordRel := 456

; outputs
; this is icon:51708360:+789
; note lack of XCoordRel

(full test code)

Code: Select all

^W::
	ControlGet, hwWindow, HWND,, SysListView321, ahk_class Progman ; in pre Win10 is "ahk_class WorkerW"
	iconObj := new iconClass("this is icon", 123, 789, hwWindow)
	tmpStr1 := iconObj.toString() ; this is icon::+789
	iconObj.XCoordRel := 456
	tmpStr2 := iconObj.toString() ; this is icon:51708360:+789
	infoMsgBox(tmpStr1 "`n" tmpStr2)
return

Obviously I am not using Set() correctly at all.
Without using a generic meta-function (for both Prop #1 & Prop#2; I just want a bog-standard set() example), what should I be doing?

Thanks
Helgef
Posts: 3981
Joined: 17 Jul 2016, 01:02
Contact:

Re: Req. help with Object Property set() method

18 Apr 2018, 12:25

First, you need to assign YCoordRel before you assign to the XCoordRel property, look in __new. Second, your XCoordRel property has no get method, hence, this.XCoordRel doesn't return anything. Try something like this

Code: Select all

	XCoordRel
	{
		set
		{
			this._XCoordRel := XCoord := value	; Store the value in _XCoordRel
			YCoord := this.YCoordRel
			msgbox % "abc" YCoord
			this.XYCoordRelPacked := (XCoord & 0xFFFF) | ((YCoord & 0xFFFF) << 16)
			return value
		}
		get
		{
			return this._XCoordRel
		}
	}
Cheers.
ahbi
Posts: 16
Joined: 15 Jun 2016, 17:11

Re: Req. help with Object Property set() method

18 Apr 2018, 13:29

Thanks a lot, but I still have a ton of questions
you need to assign YCoordRel before you assign to the XCoordRel property, look in __new.
I don't understand.

It seems to work without flipping the assignment order in _New()

Plus, if I did have to assign X before Y to make X.set() work, I'd be stuck in a mobius loop when I wanted to write Y.set(). Again that can't be what is happening because I implemented Y.set() and it is happy.
this._XCoordRel := XCoord := value ; Store the value in _XCoordRel
What does the _Var syntax mean?

I am guessing it is not a new variable (like Var2 would be). What is its special meaning?
your XCoordRel property has no get method, hence, this.XCoordRel doesn't return anything.
That I understand. Instead of using a default get() method I have to implement set/get in pairs. OK

return value
This I don't understand.
If I did

Code: Select all

this._XCoordRel := XCoord := value
do I still need a return?




This is the new class. Anything I missed?

Code: Select all


class iconClass
{
	__New(nIconName, XCoord, YCoord, nRelToHWND=false)
	{
		this.IconName := nIconName
		this.RelToHWND := nRelToHWND
		this.XCoordRel := XCoord
		this.YCoordRel := YCoord
		this.XYCoordRelPacked := (XCoord & 0xFFFF) | ((YCoord & 0xFFFF) << 16)
		return this
	}
	
	XCoordRel
	{
		set
		{
			this._XCoordRel := XCoord := value	; Store the value in _XCoordRel
			YCoord := this.YCoordRel
			this.XYCoordRelPacked := (XCoord & 0xFFFF) | ((YCoord & 0xFFFF) << 16)
			return value
		}
		get
		{
			return this._XCoordRel
		}
	}
	
	YCoordRel
	{
		set
		{
			this._YCoordRel := YCoord := value	; Store the value in _XCoordRel
			XCoord := this.XCoordRel
			this.XYCoordRelPacked := (XCoord & 0xFFFF) | ((YCoord & 0xFFFF) << 16)
			return value
		}
		get
		{
			return this._YCoordRel
		}
	}
	
	toString()
	{
		tmpStr := this.IconName ":" this.XYCoordRelPacked ":" this.XCoordRel "+" this.YCoordRel
		return tmpStr
	}
}

awel20
Posts: 180
Joined: 19 Mar 2018, 14:09

Re: Req. help with Object Property set() method

18 Apr 2018, 15:33

ahbi wrote:Without using a generic meta-function (for both Prop #1 & Prop#2; I just want a bog-standard set() example), what should I be doing?
https://autohotkey.com/docs/Objects.htm ... Properties
(look at the RGB get/set part: ; Meta-functions can be mixed with properties:...)

Here are more examples. I added some comments that might answer some of your questions.

Code: Select all

iconObj := new iconClass("this is icon", 123, 789, hwWindow)
MsgBox % iconObj.toString()


class iconClass
{
	; XCoordRel := 0
	; YCoordRel := 0
	; XYCoordRelPacked := 0
	; RelToHWND 
	; IconName := ""

	__New(nIconName, XCoord, YCoord, nRelToHWND=false)
	{
		this.IconName := nIconName
		this.RelToHWND := nRelToHWND
		this.XCoordRel := XCoord ; YCoordRel is blank until it is set on the next line. (XCoordRel 'set' uses this.YCoordRel)
                                 ; Added if (this.YCoordRel != "") to 'set' to check for this. This way it won't  
                                 ; update this.XYCoordRelPacked unless both values are not blank.
		this.YCoordRel := YCoord
		
		; ObjRawSet(this, "XCoordRel", XCoord) ; or don't use get/set.
	}
	
	XCoordRel
	{
        get
        {
            ; return this.XCoordRel ; this would cause an infinite loop:
                                    ; this.XCoordRel > get > this.XCoordRel > get > this.XCoordRel...
                                    ; '_XCoordRel' is just a different key so as to avoid the infinite loop. AFAIK the 
                                    ; underscore is just conventional for internal keys.
            return this._XCoordRel
        }
		set
		{
            if (this.YCoordRel != "") ; do not assign 'XYCoordRelPacked' unless 'this.YCoordRel' has been set.
            {
                XCoord := value
                YCoord := this.YCoordRel
                this.XYCoordRelPacked := (XCoord & 0xFFFF) | ((YCoord & 0xFFFF) << 16)
            }
			return this._XCoordRel := value
		}
	}
    
    YCoordRel
	{
        get
        {
            return this._YCoordRel
        }
		set
		{
            if (this.XCoordRel != "")
            {
                XCoord := this.XCoordRel
                YCoord := value
                this.XYCoordRelPacked := (XCoord & 0xFFFF) | ((YCoord & 0xFFFF) << 16)
            }
			return this._YCoordRel := value
		}
	}
	
	toString()
	{
		tmpStr := this.IconName ":" this.XYCoordRelPacked ":" this.XCoordRel "+" this.YCoordRel 
		return tmpStr
	}
}

Code: Select all

iconObj := new iconClass("this is icon", 123, 789, hwWindow)
MsgBox % iconObj.toString


class iconClass
{
	__New(nIconName, XCoord, YCoord, nRelToHWND=false)
	{
		this.IconName := nIconName
		this.RelToHWND := nRelToHWND
		this.XCoordRel := XCoord
		this.YCoordRel := YCoord
	}
	
	toString
    {
        get
        {
            return this.IconName ":" this.XYCoordRelPacked ":" this.XCoordRel "+" this.YCoordRel
        }
	}
    
    XYCoordRelPacked
    {
        get
        {
            return (this.XCoordRel & 0xFFFF) | ((this.YCoordRel & 0xFFFF) << 16)
        }
    }
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Antoine, Bort P, CheshireCat, flyingDman, guest3456, Verminaard and 206 guests