The idea is a Settings class that read/writes to an INI file and can be used across different scripts.
I would initiate the object with
Code: Select all
settings := Settings(["PosX", "PosY"])
and then use code like ...
Code: Select all
windowPositionX := settings.PosX
The script crashes in __New() at the use of this.settingsValues := {}. I never get to the "hello2" MsgBox.
What am I doing wrong? And ELI5, please.
Thanks
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
arrayOfProperties := []
arrayOfProperties[1] := new SettingsProperty("PosX", "PosX", -1, false)
arrayOfProperties[2] := new SettingsProperty("PosY", "PosY", -1, false)
arrayOfProperties[3] := new SettingsProperty("DrivesToShow", "DrivesToShow", "C|D|E|N", true)
arrayOfProperties[4] := new SettingsProperty("correctResChanges", "correctResChanges", true, true)
s := ""
for k, v in arrayOfProperties
{
s .= k "="
if isObject(v)
{
for kk, vv in v
s .= "`n`t" kk "=" vv
}
else
{
s .= v
}
s .= "`n"
}
MsgBox % s
zettings := new Settings(arrayOfProperties)
s := ""
s .= "PosX=" zettings.PosX
MsgBox % s
Exit
class Settings
{
__New(_arrayOfProperties)
{
msgbox, "hello"
this.settingsValues := {}
; this.settingsConfigutations := {}
msgbox, % "hello2 -- " _arrayOfProperties.Length()
Loop, % _arrayOfProperties.Length()
{
currentSetting := _arrayOfProperties[A_Index]
settingName := currentSetting.propertyName
msgbox, % A_Index " -- " settingName " -- " currentSetting.defaultValue
this.settingsValues[settingName] := currentSetting.defaultValue
; this.settingsConfigurations[settingName] := currentSetting
}
}
__Get(aName)
{
if (this.settingsValues.HasKey(aName))
{
aValue := this.settingsValues[aName]
return aValue
}
}
__Set(aName, aValue)
{
if (this.settingsValues.HasKey(aName))
{
this.settingsValues[aName] := aValue
return aValue
}
; NOTE: Using 'return' here would break this.stored_RGB and this.RGB.
}
readINIFile(aIniFile="")
{
; read values from INI file and put in this.settingsValues{}
}
writeINIFile(aIniFile="")
{
; write values to INI file from this.settingsValues{}
}
writeINIFileOnClose(aIniFile="")
{
; fired when application closes.
; write values to INI file from this.settingsValues{}
}
}
class SettingsProperty
{
__New(propertyName, iniName, defaultValue, isWriteOnClose)
{
this.propertyName := propertyName
this.iniName := iniName
this.defaultValue := defaultValue
this.isWriteOnClose := isWriteOnClose
}
}