Can I set a default value for an Object's Property, just like a Map ? [v2] Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
jly
Posts: 89
Joined: 30 Sep 2020, 06:06

Can I set a default value for an Object's Property, just like a Map ? [v2]

13 Mar 2023, 03:51

I have a Gui that creates a lot of different controls based on different conditions.
When the Gui is submitted, I want to check the value of each control.
NamedCtrlContents := MyGui. Submit(Hide)

Due to some circumstances, some controls will not be created, so this will report an error:
NamedCtrlContents.%GuiCtrl.Name%
Error: This value of type "Object" has no property named "ChldAdd_JbFsDDLSt".


I know that "Obj.HasOwnProp(Name)" can detect whether a certain control exists, and then I can check its value.
But I have too many controls, this is too much trouble.

Can I set a default value for an Object's Property, just like a Map?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Can I set a default value for an Object's Property, just like a Map ? [v2]  Topic is solved

13 Mar 2023, 04:26

Code: Select all

MonkeyPatchDefault(Obj, Default) {
	Obj.DefineProp('__Get', {Call: __Get})
	
	__Get(this, Name, Params) {
		if Params.Length
			throw Error('illegal unsupported usage', -1, 'thisObj.' Name '[<' Params.Length ' parameters>]')

		return Default
	}

	return Obj
}

NamedCtrlContents := MonkeyPatchDefault(MyGui.Submit('Hide'), 'ur_default_value')
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Can I set a default value for an Object's Property, just like a Map ? [v2]

14 Mar 2023, 15:29

swagfag wrote:
13 Mar 2023, 04:26

Code: Select all

MonkeyPatchDefault(Obj, Default) {
	Obj.DefineProp('__Get', {Call: __Get})
	
	__Get(this, Name, Params) {
		if Params.Length
			throw Error('illegal unsupported usage', -1, 'thisObj.' Name '[<' Params.Length ' parameters>]')

		return Default
	}

	return Obj
}

NamedCtrlContents := MonkeyPatchDefault(MyGui.Submit('Hide'), 'ur_default_value')

I am still really trying to learn this Prototype thing. It seems so cool to extend built in objects to extend capabilities.

How would you convert that so it adds a Default property to objects so as to basically work the same as the Default for arrays and maps.

So you could do this:

Code: Select all

Obj := {}
Obj.Default := 13
MsgBox Obj.Test ; default to 13

Your MonkeyPatchDefault(Obj, Default) basically does it just as a function call.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Can I set a default value for an Object's Property, just like a Map ? [v2]

14 Mar 2023, 17:19

FanaticGuru wrote:
14 Mar 2023, 15:29
I am still really trying to learn this Prototype thing. It seems so cool to extend built in objects to extend capabilities.

How would you convert that so it adds a Default property to objects so as to basically work the same as the Default for arrays and maps.

So you could do this:

Code: Select all

Obj := {}
Obj.Default := 13
MsgBox Obj.Test ; default to 13

After tinkering around with it some, I seem to have something that works.

Code: Select all

ObjectDefault()
ObjectDefault()
{
	Object.Prototype.DefineProp('__Get', { Call: __Get_Default })
	Object.Prototype.DefineProp('Default', { Set: Set_Default, Get:Get_Default })
	Set_Default(this, DefaultValue)
	{
		this.DefaultValue := DefaultValue
	}
	Get_Default(this)
	{
		Return this.DefaultValue
	}
	__Get_Default(this, Name, Params)
	{
		If this.HasProp("DefaultValue")
			Return this.DefaultValue
		else
			throw Error('This value of type "Object" has no property named "' Name '".')
	}
}

Obj := {}
Obj.Default := "Default Value of 13"
Obj.One := "One Stuff"
MsgBox Obj.One 
MsgBox Obj.Test ; returns the Default


Obj2 := {}
Obj2.Two := "Two Stuff"
MsgBox Obj2.Two
MsgBox Obj2.Test ; throw error because there is no Test or Default

I am not sure if this is the best or correct way or going to cause other issues with other Objects.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada and 60 guests