Results 1 to 7 of 7

Thread: [RESOLVED] Saving variables into text file

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Resolved [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:
    1. Select Case strKey
    2.                 Case "Opt_LengthOfCross"
    3.                     Opt_LengthOfCross = Val(strValue)
    4.                 Case "Opt_GeneralMarker_Color"
    5.                     Opt_GeneralMarker_Color = Val(strValue)
    6.                 Case "Opt_MeasurementMarker_Color"
    7.                     Opt_MeasurementMarker_Color = Val(strValue)
    8.                 .......
    9.  
    10.  
    11.                 Case "ShiftFactor_H(0)"
    12.                     ShiftFactor_H(0)  = Val(strValue)
    13.                 Case "ShiftFactor_H(1)"
    14.                     ShiftFactor_H(1)  = Val(strValue)
    15.                 Case "ShiftFactor_H(2)"
    16.                     ShiftFactor_H(2)  = Val(strValue)
    17.                 .......
    18.  
    19.             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.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    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!

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.
    Like Archer? Check out some Sterling Archer quotes.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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, ",")

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    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
  •  



Click Here to Expand Forum to Full Width