• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 139
  • Last Modified:

create/update/reading a file

Hi,

I creating a little app that analysis stock.  I want the user to be able to select preferences that won't change for the, i.e. TaxRate, ImputationCreditRate and MarginLendingRate .

I have a form that contains 3 fields for input and a button that captures these inputs.   I want this information to be saved in a file say 'C:\temp\ShareUser.dat'.  

At startup this file is read and each bit of data is stored in corresponding variables to be used in later calculations.  This file will only be created if the user actually go into Preferences form and changes values.

I quessing each field would be stored in either a line or comma dilimited format and a routine is required to update the appropriate field and read the appropriate field?

Need more info, just ask.  Help!  I'm new to this.  :-(

Thanks.
0
begonz
Asked:
begonz
  • 6
  • 4
1 Solution
 
RocketJockeyCommented:
Use the TINIFile (which is basically a text file anyway):

{******************************************
Used to create and populate the INI File
with default values if it doesn't already
exist
*****************************************}
procedure TForm2.FormCreate(Sender: TObject);
Var
  MyIniFile: TIniFile;
Begin
  //create the INI file
  MyIniFile := TIniFile.Create('C:\temp\ShareUser.ini');
 
  //set the TaxRate editbox
  edtTaxRate.text := MyIniFile.ReadString('TaxPrefs', 'TaxRate', 'Default TaxRate');
  //set the ImputationCreditRate editbox
  edtICRate.text := MyIniFile.ReadString('TaxPrefs', 'ICRate', 'Default ICRate');
  //set the MarginLendingRate editbox
  edtMLRate.text := MyIniFile.ReadString('TaxPrefs', 'MLRate', 'Default MLRate');
 
  MyIniFile.Free;
End;

{******************************************
Used to read and populate the form with
the values of the INI file. Uses the
default values if no entry in INI File is
found.
*****************************************}
procedure TForm2.Button1Click(Sender: TObject);
Var
  MyIniFile: TIniFile;
Begin
  //create the INI file
  MyIniFile := TIniFile.Create('C:\temp\ShareUser.ini');

  //read the TaxRate editbox
  MyIniFile.WriteString('TaxPrefs', 'TaxRate', edtTaxRate.text);
  //read the ImputationCreditRate editbox
  MyIniFile.WriteString('TaxPrefs', 'ICRate', edtICRate.text);
  //read the MarginLendingRate editbox
  MyIniFile.WriteString('TaxPrefs', 'MLRate', edtMLRate.text);
 
  MyIniFile.Free;

End;



I hope this helps
0
 
RocketJockeyCommented:
Oops. My comment blocks are reversed. Hope that didin't confuse you too much.
0
 
begonzAuthor Commented:
Thanks for this I will give it a shot.  

I guess in my in my main unit I can read the file at start up and populate global variables that can be used in claculation later.

How would I read in from the file and save to a variable?  This is quite a major part of what I'm trying to do.

What you have advised looks good, I will give it a try tonight and see how I go.

Thanks.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RocketJockeyCommented:
The following is the procedure that is run when your applicaiton starts up. If you want the values read from the INI file put into variables, simply replace the edit boxes with your variables.

If you want to add any new Sections or Values to the INI File use the following syntax:

TIniFile.WriteString('The Section Name', 'The Value's Name', 'The value');

procedure TForm2.FormCreate(Sender: TObject);
Var
 MyIniFile: TIniFile;
 strTaxRate, strICRate, strMLRate : String;
Begin
 //create the INI file
 MyIniFile := TIniFile.Create('C:\temp\ShareUser.ini');
 
 //set the TaxRate editbox
 strTaxRate := MyIniFile.ReadString('TaxPrefs', 'TaxRate', 'Default TaxRate');
 //set the ImputationCreditRate editbox
 strICRate := MyIniFile.ReadString('TaxPrefs', 'ICRate', 'Default ICRate');
 //set the MarginLendingRate editbox
 strMLRate  := MyIniFile.ReadString('TaxPrefs', 'MLRate', 'Default MLRate');
 
 MyIniFile.Free;
End;
0
 
LukA_YJKCommented:
IniFile is good choice. Maybe you can do it simpler:
const
  INI_FILE = 'C:\temp\ShareUser.ini';
TForm2 = class(TForm)
...
private
...
  IniFile: TextFile;
  TaxRate: double;
  ICRate: double;
  MLRate: double;
end;

//Load all values every time you start
procedure TForm2.FormCreate(Sender: TObject);
Begin
  AssignFile(IniFile,INI_FILE);
  try
    Reset(IniFile);
    Readln(IniFile, TaxRate);
    Readln(IniFile, ICRate);
    Readln(IniFile, MLRate);
    CloseFile(IniFile);
  except
    TaxRate := 1.0;
    ICRate := 1.0;
    MLRate := 1.0;
  end;
End;

//Save all values every time you quit
procedure TForm2.FormClose(...);
Begin
  try
    Rewrite(IniFile);
    Writeln(IniFile, TaxRate);
    Writeln(IniFile, ICRate);
    Writeln(IniFile, MLRate);
    CloseFile(IniFile);
  except
  end;
End;
0
 
begonzAuthor Commented:
Awesome stuff RocketJockey, I have got it working.  Once thing though, how can I use the variables in other calcs?  How can I make them global?

Thanks again.
0
 
begonzAuthor Commented:
In addition to my last comment I guess I can just read the INI file for each calculation needed on each different form.  Is that an efficiant way of doing it?
0
 
begonzAuthor Commented:
Great help and does what I want.  Not sure if when I close the question you are still able to post comments so if you can answer my last email me at boss2000_nz@yahoo.co.nz

0
 
RocketJockeyCommented:
Nah, no need to keep reading the INI file. It is more effecient to define global variables. Simply place your variable declarations in the "public" section at the top of your main pas file.

To reference those Globals from other forms, just include the main form in the other form's Uses definitions.

public
{ Public declarations }
//Your Global vars
TAXRATE: double;
ICRATE: double;
MLRATE: double;

0
 
begonzAuthor Commented:
hmmmmm...

I have done as suggested and created 3 variables of Real type in the public section of my main pas file.

I have included the main pas file in the Uses section of the other calc but keep getting the message saying that 'Undeclared identifier: 'TAXRATE''

I am only trying to write to tthat Variable.

Any suggestions?
0
 
begonzAuthor Commented:
hmmmmm...

I have done as suggested and created 3 variables of Real type in the public section of my main pas file.

I have included the main pas file in the Uses section of the other calc but keep getting the message saying that 'Undeclared identifier: 'TAXRATE''

I am only trying to write to tthat Variable.

Any suggestions?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now