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?
Can I set a default value for an Object's Property, just like a Map ? [v2] Topic is solved
Re: Can I set a default value for an Object's Property, just like a Map ? [v2] Topic is solved
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')
Re: Can I set a default value for an Object's Property, just like a Map ? [v2]
@swagfag Thank you so much !
- 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]
swagfag wrote: ↑13 Mar 2023, 04:26Code: 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
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
- 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]
FanaticGuru wrote: ↑14 Mar 2023, 15:29I 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
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
Who is online
Users browsing this forum: No registered users and 69 guests