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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops. My comment blocks are reversed. Hope that didin't confuse you too much.
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.
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\S hareUser.i ni');
//set the TaxRate editbox
strTaxRate := MyIniFile.ReadString('TaxP refs', 'TaxRate', 'Default TaxRate');
//set the ImputationCreditRate editbox
strICRate := MyIniFile.ReadString('TaxP refs', 'ICRate', 'Default ICRate');
//set the MarginLendingRate editbox
strMLRate := MyIniFile.ReadString('TaxP refs', 'MLRate', 'Default MLRate');
MyIniFile.Free;
End;
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\S
//set the TaxRate editbox
strTaxRate := MyIniFile.ReadString('TaxP
//set the ImputationCreditRate editbox
strICRate := MyIniFile.ReadString('TaxP
//set the MarginLendingRate editbox
strMLRate := MyIniFile.ReadString('TaxP
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_FIL E);
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;
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_FIL
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;
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.
Thanks again.
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?
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;
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;
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?
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?
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?
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?