App Design - Providing Default Values

Always appreciate all the help everyone gives here!

What’s the best way to provide default values, including multi-column data? My app has default values for properties, but also tabular data. When the app starts it loads all of the data into memory and then not use the source data again, and this data is immutable.

Currently, I have all the properties created in the Contents pane, and then assign values to them as part of the Opening event.

I had thought of a dictionary but posts in the forum suggest that a Class is better than a dictionary object. Using an sqlite DB is probably overkill as I have at most 30 values in the table, plus another 10 for properties.

I’m not getting a clear idea of what you’re describing. Is this user-facing data - for example, when they make a new document, these are the defaults for the document? Or is it internal configuration data that the app uses to operate?

it’s internal config for the app, the user can override some of the values later on and i save these values in a sqlite DB along with some other data.

my question is more from an architecture perspective - what is the ‘right’ way to provide defaults without burying them in code?

I typically use a class for this sort of purpose, but then the default values are often set as constants on the class (in case I need a reset). Either way, whether a class or dictionary, they’ll need to be stored as a property on a global object somewhere, e.g., the App class, so you could just as well put these non-changing starting values in constants as well. This includes the tabular data, where it could be comma or tab delimited, etc.

There’s no hard and fast rule, so sometimes simple is best.

OK, thanks. I was not far off in my thinking then.

Is there anyway I could configure a dictionary with values, or something similar, as a single object? Rather than having a long string that i then split

In Swift I can create an Array of Tuples which works great, and exactly the sort of thing I’d like in Xojo!

Well, you can always do this:

Var myDefaultsDictionary = new Dictionary("key1" : "value1", "key2" : "value2")

That will give you a dictionary that is pre-populated right from code.

1 Like

The good way is your way

In general, I use dictionary and JSON to save on the hard drive

This is my way:

' === creation dico
Var dico_Couleurs As New Dictionary
dico_Couleurs.value ( "label_Coul" ) = Color.blue
dico_Couleurs.value ( "champ_Coul" ) = Color.Green

Var dico_Valeurs As New Dictionary
dico_Valeurs.value ( "label_text" ) = "Le Joli label"
dico_Valeurs.value ( "champ_text" ) = "Ajouter votre valeur"

Var dico_Pref As New Dictionary
dico_Pref.value ( "couleurs" ) = dico_Couleurs
dico_Pref.value ( "valeurs" ) =dico_Valeurs

Var Json_Tx As String
Json_Tx = GenerateJSON ( dico_Pref , True )
 
' === crea dossier
Var f_Dos_Appli As FolderItem  
f_Dos_Appli = SpecialFolder.ApplicationData.Child ( app.ExecutableFile.Name ) 
If f_Dos_Appli.Exists = False Then
  f_Dos_Appli.CreateFolder 
End If

' === enregistrement
Var f_file As FolderItem
Var stream As BinaryStream 
f_file = f_Dos_Appli.Child ( "preferences" )
stream = BinaryStream.Create( f_file , True)
stream.Write( Json_Tx )
stream.Close

ok, thanks. i’ll consider an external JSON file to then build a dictionary via code

If you’re saving the values to an SQLite file, why not include an SQLite file in the bundle and if the user version doesn’t exist, copy the default version in place. It would make your code much simpler.

If you’re building a Dictionary you can use the .Lookup method to provide default values.

If you have GraffitiSuite, take a look at the GraffitiSettings class which makes this super easy.

1 Like