Gambas By Example: Settings

DISCLAIMER: The information contained within these pages is strictly for educational purposes and I do not provide any warranties. It is at your own risk to use this information. I have made every effort to provide correct and suitable information However it is not assumed that these examples will not contain errors of any type whatsoever. However, just in case this should happen, I will gladly appreciate any reports regarding any errors found.

gb.settings is used by your project to create/edit it's own application configuration files.

Example

If I wanted to save the path to where the data files are kept I would use the following.

Settings["Data/Path"] = Application.Path &/ "Data/"

Where Application.Path &/ "Data/" would be the path of where your application is.

TIP: When you are joining two strings to build a path you can use a &/ instead of a & the to detect the need for a / and auto insert it

Now suppose the application is named Test.gambas and it is in in a folder named MyProjects within the user's home folder. A configuration file named Test.conf will be created. Within that file will be the lines

[Data]

Path="/home/user/MyProjects/Test/Data/"

Where /user/ is the name of the user's home folder

If you want to read this path into a string variable you can use the following method.

DIM DataPath AS String

IF NOT IsNull(Settings["Data/Path"]) THEN 'Checks to see if the ["Data/Path"] exists or not.

DataPath = Settings["Data/Path"] 'If the record exists, then copy the value into the string varable named DataPath

ENDIF

Caution: If the config file was deleted or manually edited by user, or if the program is being ran the first time, the Settings record may not exist and be null. In some circumstances, if you tried to copy a value from a non existent Settings record to something like a window object properties setting, it could crash your program. For example by trying to set a Checkbox1.Value with a null value will return "Type mismatch: wanted Integer, got Null instead" error. By testing if the Settings record is null or not, you can prevent this.

Now if you have a good idea of what the default value (or in this circumstance, a path) would be, you could do this instead

DataPath = Settings["Data/Path", Application.Path &/ "Data/"]

If Settings["Data/Path"] is null then the default Application.Path &/ "Data/" will be used.

Here are some other examples for usning the Settings feature in Gambas

    • Message(Settings.Path) will popup a Message box with the path to where the configuration file is.

    • The Settings.Path is handy if you want your app to have the ability to access the configuration file outside of the Settings feature. You don't have to hard code a path into your program.

    • Settings.Clear["Data"] will clear all keys under "Data" For example if you had two keys named "Data/Path" and "Data/ListName" then both would be cleared.

    • But if Settings.Clear["Data/ListName"] was used instead, then only the "Data/ListName" would be cleared.

    • Settngs.Save - Saves all changes to the configuration file. This is good for when you need to save a setting without waiting for when the application is closed down. For example

    • you may want your application to refer back to the updated value in the configuration file.

    • Settings.Reload will reload all the configuration values. All unsaved changes will be lost.

Troubleshooting

If you can not get Settings to work you may have to load the component.

To do this click on Project and then Properties (See Fig. 1)

Fig.1

Then Select the Components tab and scroll down list till you see gb.settings and place a checkmark by it.

(See Fig. 2)

Fig. 2

End of Settings Example

Example created by using version 2.19