Autore Topic: gb.settings: verificare se esiste uno slot  (Letto 352 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.269
  • Ne mors quidem nos iunget
    • Mostra profilo
gb.settings: verificare se esiste uno slot
« il: 29 Ottobre 2013, 15:18:25 »
Vi riporto questa discussione apparsa nella M.L. ufficiale:


" I have a need to detect if a particular Slot exists in the .config
file for an application. Just the Slot not whether any value lines exist
under it.

There doesn't seem to be a way to do this via gb.settings?  I have tried
using

Codice: gambas [Seleziona]
If Settings.Keys["Filter"] then


but this seems always to be true. Similarly
Codice: gambas [Seleziona]
If Settings["Filter"] then


always seems to be false.

So is there a way to find out if a Slot does exist?

---- more info -----
The problem is that the value lines depend on about 8 text boxes set or
cleared by the user. If one or more of the text boxes are empty then
that value line will not appear under the [Filter] slot. So I can't
detect the existence of the slot by detecting a particular value line.
In fact if they clear all the filter strings then that whole slot will
disappear.

Thus I need to check if the slot exists, so then I could

Codice: gambas [Seleziona]
If Settings.Exist["Filter"] then
          GetFilterSettings
        Else
          SetDefaultFilter
        Endif


Bruce "


" Salut Bruce,

Codice: gambas [Seleziona]
Settings.Read (gb.settings)
Sub Read ( hObject As Object [ , sKey As String, vDefault As Variant ] )

Settings("Filter", GetFilterSettings, SetDefaultFilter)


or

Codice: gambas [Seleziona]
Public Function KeyExists(objSetting As Settings, sFindKey As String) As Boolean
Dim sKey As String
Dim bRet As Boolean
   bRet = False
   For Each sKey In objSetting.Keys
      If sKey = sFindKey Then
          bRet = True
         Break
      Endif
   Next
   Return bRet
End


--
Amicalement
Charlie
"


" Hmm. Settings["Filter"] will return Null if the slot does not exist. Note
that the slot is also non-existent if you do assign Null like


  
Codice: gambas [Seleziona]
Settings["Filter"] = Null


which means Settings[sKey] returning Null is a necessary and sufficient
indicator that sKey is not a slot in the Settings.

So, use


Codice: gambas [Seleziona]
  If IsNull(Settings["Filter"]) Then
    ' Settings["Filter"] does not exist...
  Endif


in case you want to do something when the "Filter" key does not exist.

Regards,
Tobi
"
« Ultima modifica: 31 Ottobre 2013, 14:54:06 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.269
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: gb.settings: verificare se esiste uno slot
« Risposta #1 il: 31 Ottobre 2013, 14:56:44 »
...continua...


" That is what I thought too but it doesn't work.
If the .config is
        [Filter]
        filteractive=-1
then isNull(Settings["Filter"]) will always be true as Settings will
always return a null if the key is a slot not a value.

My problem is that if no Filter/things are set, when the settings are
saved then the entire slot disappears from the .config file.

> in case you want to do something when the "Filter" key does not exist.
>
> Regards,
> Tobi
>

I have figured out a workaround, but its messy and fragile. I create a
dummy boolean valueitem called "filtered". I set it true if any filters
are set and false if none are set. It is fragile because I do not know
at the time I close down the app how many filter things there are. So
each bit of code that creates or sets a filter item must manage a global
boolean value (and thus it is fragile because it involves each bit
checking on all other filter items....).

But at least that way I always will have a [Filter] slot with at least
one valueitem in it.


Hmm. You second idea works, but I was trying to avoid that as I will be
calling it multiple times, at least once for each filter item that gets defined.
But it might be better than the messy solution I just posted back to Tobi.

--
Bruce
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »