Link to home
Start Free TrialLog in
Avatar of begonz
begonz

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of RocketJockey
RocketJockey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RocketJockey
RocketJockey

Oops. My comment blocks are reversed. Hope that didin't confuse you too much.
Avatar of begonz

ASKER

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.
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;
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;
Avatar of begonz

ASKER

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.
Avatar of begonz

ASKER

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?
Avatar of begonz

ASKER

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

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;

Avatar of begonz

ASKER

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?
Avatar of begonz

ASKER

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?