Recent

Author Topic: Reading INI file using procedure in another unit  (Read 1711 times)

TomTom

  • Full Member
  • ***
  • Posts: 170
Reading INI file using procedure in another unit
« on: December 10, 2018, 09:47:10 am »
I know it's simple but can't find solution :|
I want to create program that have one form with few Edit boxes and two buttons.
One button runs procedure from other unit. This procedure is SaveIni(ed1,ed2,ed3:string); and it saves to ini file whatever is in Edit1, Edit2 and Edit3. It works fine. Problem appears when I want to use another procedure in Unit2 with which I want to load data from that ini file into variables and then sets Edit4,Edit5 and Edit6 text to those variables. It is called LoadIni(ed1,ed2,ed3:string);.
Here's the code for both units (Unit1 and Unit2):

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,inifiles;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;
  edd1,edd2,edd3:string;

implementation
uses
  Unit2;
{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin



end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  saveini(edit1.text,edit2.text,edit3.text);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
loadini(edd1,edd2,edd3);
  edit4.text:=edd1;
  edit5.text:=edd2;
  edit6.text:=edd3;

end;

end.


Code: [Select]
unit Unit2;

{$mode objfpc}{$H+}

interface
procedure loadini(ed1,ed2,ed3:string);
procedure saveini(ed1,ed2,ed3:string);



implementation
uses
  Classes, SysUtils,Inifiles;
var
inifig:Tinifile;
const
defEd1='100';
defEd2='200A';
defEd3='300 sad';
procedure loadini(ed1,ed2,ed3:string);
begin
  iniFig:=TIniFile.Create('config.ini');
  ed1:= iniFIG.ReadString('PATHS','ed1',defEd1);
  ed2:= iniFIG.ReadString('PATHS','ed2',defEd2);
  ed3:= iniFIG.ReadString('PATHS','ed3',defEd3);
 inifig.free;


end;
procedure saveini(ed1,ed2,ed3:string);
begin
 iniFig:=TIniFile.Create('config.ini');
   iniFIG.WriteString('PATHS','ed1',Ed1);
   iniFIG.WriteString('PATHS','ed2',Ed2);
   iniFIG.WriteString('PATHS','ed3',Ed3);
  inifig.free;
end;

end.


And this is Ini file
Code: [Select]
[PATHS]
ed1=Edit1
ed2=Edit2
ed3=Edit3



CCRDude

  • Hero Member
  • *****
  • Posts: 596
Re: Reading INI file using procedure in another unit
« Reply #1 on: December 10, 2018, 09:52:11 am »
1. If you want parameters to return something, declare them as "out" (or "var", if their initial value is important) :)
2. TEdit.Text is a property, you can pass it to SaveIni, but you need helping local vars for loading since you can't pass them as const/var/out string.

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Reading INI file using procedure in another unit
« Reply #2 on: December 10, 2018, 09:56:46 am »
Thank You CCRDude :) Now I think I understand this :)

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Reading INI file using procedure in another unit
« Reply #3 on: December 11, 2018, 12:46:15 am »
The mechanism for setting properties in most cases are via procedure in the background.

 Properties for the most part have a Get Function and a Set Procedure, the get function will work in your
case because it simply returns a real variable where as setting a property involves calling a procedure and passing a
value to it. That isn't going to work very well in your case.

 There are properties types that simply directly give you access to the real variable within and in that case it will
work. But that is kind of defeating the purpose of a property then since you could simply put that variable in some
public scope of the class.


The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018