|
-
Oct 7th, 2006, 07:05 AM
#1
Thread Starter
Member
[RESOLVED] Saving variables into text file
Hi, all,
I need to write functions that can save all the user settings variables as well as some variables that are to trace the progress of user's action into a file. And the user can restore the progress and settings by loading that file. Simply speaking, it is just like the save and load function in a computer game program.
What I did is to save all the variable namesand their values into text file by a series of Print commands. The text file looks like this:
Code:
Opt_LengthOfCross = 5
Opt_GeneralMarker_Color = 16777215
Opt_MeasurementMarker_Color = 16711808
Opt_ScaleMarker_Color = 255
Opt_ManualMarker_Color = 8388863
Opt_Scan_ColorThreshold = 3000000
Opt_ProfileMarker_Color = 16776960
Opt_Threshold_Sample_Size = 29
Opt_Threshold_HalfRange = 25
Opt_Align_Scan_Length_MM = 40
ShiftFactor_H(0) = 12
ShiftFactor_H(1) = 11
ShiftFactor_H(2) = 12
ShiftFactor_H(3) = 12
ShiftFactor_V(0) = 2
ShiftFactor_V(1) = 5
ShiftFactor_V(2) = 1
ShiftFactor_V(3) = 3
.....
When I load this file, I do something like this:
VB Code:
Select Case strKey
Case "Opt_LengthOfCross"
Opt_LengthOfCross = Val(strValue)
Case "Opt_GeneralMarker_Color"
Opt_GeneralMarker_Color = Val(strValue)
Case "Opt_MeasurementMarker_Color"
Opt_MeasurementMarker_Color = Val(strValue)
.......
Case "ShiftFactor_H(0)"
ShiftFactor_H(0) = Val(strValue)
Case "ShiftFactor_H(1)"
ShiftFactor_H(1) = Val(strValue)
Case "ShiftFactor_H(2)"
ShiftFactor_H(2) = Val(strValue)
.......
End Select
My problem is, the number of variables is much larger than I expected: almost 50 variables, and some of them are array of size >100. It is inpractical to handle the save/load function in this way.
Do anyone have a clue for it?
Some of the variables are just native data type, but some are user-defined type, and some are array. Also the variable names cannot be unified because the are used in different modules and for different purposes.
-
Oct 7th, 2006, 07:10 AM
#2
Re: Saving variables into text file
you could use a database instead, or the registry.
text files are ultimately slow. you could just use SaveSetting and GetSetting, which are native functions of VB and use the registry but you don't need to actually do anything to add the stuff to the registry except call those functions.
-
Oct 7th, 2006, 07:18 AM
#3
Thread Starter
Member
Re: Saving variables into text file
thanks for your idea, kows.
Sorry i haven't mentioned that the purpose of the text file is to let user carry on their works in different time/location, e.g. user can save the progress in a file, then start another job, and later on he/she can resume the previous job by loading the saved file...
SaveSetting /GetSetting sounds interesting....can they work with text (or maybe binary) files?
Thanks!
-
Oct 7th, 2006, 07:24 AM
#4
Re: Saving variables into text file
do a search for INI files or GetPrivateProfileString
SaveSetting & GetSetting read/write to the registry - which can be a problem if the user doesn't have sufficient access permissions
-
Oct 7th, 2006, 07:25 AM
#5
Re: Saving variables into text file
you could always use SaveSetting/GetSetting as the actual way of saving preferences, and then have an "Export Preferences" that gets all the settings and saves them to a textfile. This may or may not be something you would want to do. The only problem with writing to the registry is user permissions, which would make your program useless for having saved settings!
But, if you just want to use plain old textfiles, and if no human interaction with the file is needed, I would suggest cutting out all unneeded information (ie: useless strings) and only storing values, either with a delimiter combining similar variables into one long line (ie: opt_lengthofcross|"|opt_generalmarkercolor|"|etc, where |"| is a delimiter), and then splitting each set of similar variables with a line break (vbNewLine or vbCrLf), so that reading the text file can be as efficient as possible and requires no parsing (ie: look for this string, delete string part to get the value). However, this would be useless if an end user wanted to change the settings manually (for whatever reason) and didn't know the order in which they went... but also useful if you had some stuff you didn't want them to have access to, or whatever.
Anyway, if you use an Access database it would still be easy to package it with your program, you could just have the file "settings.mdb" and ask them what settings file to load, and reference that when you're opening the database with ADO for example. Not sure if it requires them to have Access installed, or just have the ADO dependencies that you can provide with your application.
Hope this stuff made sense to you @_@ was mostly a ramble, I think.
edit: Using INI files would be a good idea, too.
-
Oct 7th, 2006, 08:05 AM
#6
Re: Saving variables into text file
For the arrays do this instead
ShiftFactor_H = 12,11,12,12
ShiftFactor_V = 2,5,1,3
In code
ShiftFactor_H() = Split(strValue, ",")
-
Oct 7th, 2006, 10:37 AM
#7
Thread Starter
Member
Re: Saving variables into text file
It seems INI files + Split() function would be a very good combination.
Thank you all guys!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|