I am setting up a mechanism in my film scanner software that allows you to save a config file for different types of film to be scanned. Things like default motor torque settings, camera and lens stage positions, etc.
I’m saving them to a text file, probably just a flat key/value pair setup. I first put all the variables I want to save into a dictionary, with the key having the same name as the corresponding Xojo variable, and the value to whatever that value is currently set to. For example:
var configValues as new dictionary
configValues.Value("ConfigName") = tfConfigName.Text
//Camera and lens positions
configValues.Value("imCamStageTarget") = imCamStagePos
configValues.Value("imLensStageTarget") = imLensStagePos
//Transport settings for this gauge
configValues.Value("trCapstanAcceleration") = trCapstanAcceleration
configValues.Value("trCapstanVelocity") = trCapstanVelocity
configValues.Value("trFeedSpeed") = trFeedSpeed
configValues.Value("trFeedTorque") = trFeedTorque
configValues.Value("trTakeupSpeed") = trTakeupSpeed
configValues.Value("trTakeupTorque") = trTakeupTorque
Upon loading a config file, it will read in the text file and write out the default values to the Xojo variables.
The keys are the same names as my variables in the application. When loading a config file, I will put all of the key/value pairs seen above in a new dictionary. Then I’ll walk through that dictionary to populate the variables one at a time.
And here’s where my question comes in:
How do I use the key name to reference a variable? That is, when I’m walking the dictionary and I’m at trCapstanAcceleration
, is there a way to translate that key name into the name of the variable? I don’t want to have to set up a complicated switch/case for all the pairs in the dictionary, because there could be several dozen. Ideally I tell it, “set the variable that has the same name as key
to value
” for each of the key/value pairs.
This will simplify the code a lot, and it means I can add items to the config file without having to add any code for that new pair when parsing the config file.