Page 1 of 1

Preferences

Posted: Mon Dec 03, 2012 10:02 am
by nospam
Make the default value optional and return an error on a failed ReadPreference if no default is specified.

The preferences feature is very useful but being forced to provide a default creates extra work that is not necessary in many situations.

The help text states:
If the 'Keyword$' isn't found or the preference file haven't been opened correctly (file missing for example) the specified default value is returned.
Good programming practice requires that if the preferences file is missing then you create it and save the defaults accordingly. While I load all my preferences in one go, the requirement to specify a default would be especially onerous if ad hoc ReadPreference calls were being made throughout one's code; it would surely require the programmer to keep going back to find the correct default value.

Re: Preferences

Posted: Mon Dec 03, 2012 2:41 pm
by MachineCode
nospam wrote:Good programming practice requires that if the preferences file is missing then you create it and save the defaults accordingly.
Um, yes... so if OpenPreferences() fails due to no prefs file existing, it returns 0 and you can just go ahead and create it (as you said). What's the problem, then? It's not like you need to start using default values at all, if it returns 0 when opening.

Re: Preferences

Posted: Mon Dec 03, 2012 4:51 pm
by bobobo
no prob if a pref exits or not if it IS a pref-file.
of course a pref-file with noncompliant content will spoil this. (this may be academic)
you could check in advance, wether the pref-file is "your" pref-file (no file or filled with "my" pref-stuff)

Code: Select all

If OpenPreferences(GetTemporaryDirectory()+"mypref.ini") =0
  ;write def values
  PreferenceGroup("def")
  WritePreferenceString("val1","whatever")
  WritePreferenceString("val2","whatever")
  PreferenceGroup("g2")
  WritePreferenceString("val3","whatever")
Else
  ;readDefvalues
  ;check if it's "my" pref-file (finding specific group )
  If  ExaminePreferenceGroups()
    While NextPreferenceGroup()
      If PreferenceGroupName()="g2"
        found=1
      EndIf
    Wend
  EndIf
  If found
    PreferenceGroup("def")
    Debug "read "+ ReadPreferenceString("val1","")
    Debug "read "+ ReadPreferenceString("val3","")
    PreferenceGroup("g2")
    Debug "read "+ ReadPreferenceString("val3","")
  EndIf
EndIf
ClosePreferences()
RunProgram(GetTemporaryDirectory()+"mypref.ini")

Re: Preferences

Posted: Mon Dec 03, 2012 7:52 pm
by nospam
MachineCode wrote:
nospam wrote:Good programming practice requires that if the preferences file is missing then you create it and save the defaults accordingly.
Um, yes... so if OpenPreferences() fails due to no prefs file existing, it returns 0 and you can just go ahead and create it (as you said). What's the problem, then? It's not like you need to start using default values at all, if it returns 0 when opening.
I already said what the problem is. A default value is required, but the default value is not needed.

Code: Select all

      prefSystemNextID = ReadPreferenceLong(Str(#prefSystemNextID) ,0)
      prefSystemWindowLocationX = ReadPreferenceLong(Str(#prefSystemWindowLocationX) ,0)
      prefSystemWindowLocationY = ReadPreferenceLong(Str(#prefSystemWindowLocationY) ,0)
      prefOptionsProxyListeningIP = ReadPreferenceString(Str(#prefOptionsProxyListeningIP) ,"")
      prefSystemLogColumnDisplayIndex = ReadPreferenceString(Str(#prefSystemLogColumnDisplayIndex) ,"")
      prefSystemLogColumnNames = ReadPreferenceString(Str(#prefSystemLogColumnNames) ,"")
      prefSystemLogColumnVisible = ReadPreferenceString(Str(#prefSystemLogColumnVisible) ,"")
The code is inconsistent with other commands that use optional values, such as StringGadget's flags. The key word being "inconsistent".

Re: Preferences

Posted: Mon Dec 03, 2012 7:58 pm
by nospam
bobobo wrote:no prob if a pref exits or not if it IS a pref-file.
of course a pref-file with noncompliant content will spoil this. (this may be academic)
you could check in advance, wether the pref-file is "your" pref-file (no file or filled with "my" pref-stuff)
I don't know what you're talking about. Being forced to always specify a default value has nothing to do, per se, with whether or not a prefs file exists. It has nothing to do with non-compliant prefs files. It has nothing to do with checking in advance, and it has nothing to do with whether it's my pref file or someone else's.

Re: Preferences

Posted: Fri Dec 21, 2012 2:42 pm
by User_Russian
If you can, please add the functions

Code: Select all

CatchPreference(*Point, Length)
SavePreferenceMem(*Point, Length)
Sometimes you have to work with the data in memory (for example, program settings of its resources).