Hello,
How to store a variable when the software is closed and, reload is when the software is re-opened?
Thanks!
I use an SQLite database for that. Save all the settings into it and reload at app startup.
(Xojo 2019r3.1 Desktop Project)
i use a Settings class
using app open & close event
Sub Close() Handles Close
Settings.Save
End Sub
Sub Open() Handles Open
Settings.Load
End Sub
Property
Protected Property SoundEnabled as Boolean = True
Method for Filename
[code]Public Function File() as FolderItem
Var f As FolderItem = SpecialFolder.ApplicationData.Child(“YourAppName”)
If f.Exists = False Then f.CreateFolder
Return f.Child(“Settings.txt”)
End Function
[/code]
Save
it use a dictionary to json
[code]Public Sub Save()
Var d As New Dictionary
d.Value(“SoundEnabled”) = SoundEnabled
Var j As JSONItem
j = d
j.Compact = False
Var st As TextOutputStream = TextOutputStream.Create(File)
st.Write(j.ToString)
st.Close
End Sub
[/code]
Load
read json
[code]Public Sub Load()
Var f As FolderItem = File
If f.Exists = False Then Return
Var st As TextInputStream = TextInputStream.Open(File)
Var j As New JSONItem(st.ReadAll)
st.Close
SoundEnabled = j.Lookup(“SoundEnabled”, True)
End Sub
[/code]
Hello Markus, thank you! I got the app 'close, ‘open’, property and method. However, cannot seem to figure out add ‘Public sub save’ and ‘Publ;ic Sub load’. Are these classes? Thanks Naren
that is named shared method. because i need only a single instance i used it this way.
i newer apps i use JSONItem direct without Dictionary.
with .Lookup("…",value) you can set default values if key value pair not exists, that is useful if you add properties later.
Thank you! Appreciate your guidance. I Will work on this. Thanks