### Use a Type for Settings

A type can be used to handle application settings.
The Type holds the values, are stored in a RandomAccessFile and read accordingly.
The B4J Standard Library jRandomAccessFile is required.
Example (only the related code is shown)
`Sub Process_Globals
'Custom Types
Type ConfigRecord(Version As String, Logging As Boolean, LastChange As String)
'Configuration record
Public ConfigRec As ConfigRecord
Public ConfigFile As String = "config.cfg"
...
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
ReadConfig
...
End Sub`
'Close the app - add any specifics here
`Sub AppClose
SaveConfig
MainForm.Close
End Sub`
'Handle form closing via system close (top right X button)
`Sub MainForm_CloseRequest (EventData As Event)
AppClose
End Sub`
'Read the configuration parameter
'Requires global type and vars
'Type ConfigRecord(Version As String, Logging As Boolean, LastChange As String)
'Public ConfigRec As ConfigRecord
'Public ConfigFile As String = "config.cfg"
`Sub ReadConfig
Dim ConfigRaf As RandomAccessFile
'Init the raf
ConfigRaf.Initialize(File.DirApp, ConfigFile, False)
'Read the one configuration record in to the global config record
Try
ConfigRec = ConfigRaf.ReadObject(0)
Catch
'No file or record found, create initial values and save
ConfigRec.Version = "1"
ConfigRec.Logging = True
ConfigRec.LastChange = DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now)
SaveConfig
End Try
ConfigRaf.Close
End Sub`
'Save the configuration parameter
`Sub SaveConfig
Dim ConfigRaf As RandomAccessFile
Try
ConfigRaf.Initialize(File.DirApp, ConfigFile, False)
ConfigRaf.WriteObject(ConfigRec, False, 0)
ConfigRaf.Close
Catch
Log(LastException)
End Try
End Sub`

Changed: , Category: Type Definition, File: T6C274M2031.htm, ID: 2031