This message was edited by zibadian at 2006-2-3 4:31:54
: : : Hi all
: : :
: : : I need help
: : : I have 2 procedures ( 1 and 2 ) it is normal Delphi procedures now I assign a file in procedure 1 and I read from it and assign it to FileStr but I dont close the file with CloseFile (DataFile)
: : : now in my procedure 2 I want to use same of the data from procedure 1 like FileStr and then I will close the file in procedure 2 with CloseFile (DataFile)
: : :
: : : Now how do I share data between procedures?
: : :
: : :
: : :
: : : {===================================================}
: : : var
: : : DataFile : TextFile;
: : : FileStr : string;
: : : {===================================================}
: : :
: : : procedure TfrmQues2.btnDisplayFileClick(Sender: TObject);
: : :
: : : begin
: : : AssignFile(Datafile,edtFileName.Text);
: : : reset(DataFile);
: : : while not eof(DataFile) do
: : : begin
: : : readln(DataFile,FileStr);
: : : redOutput.Lines.Add(FileStr);
: : :
: : : end;
: : : end;
: : : {===================================================}
: : :
: : : procedure TfrmQues2.btnDisplayPictureClick(Sender: TObject);
: : : var
: : : Position,LengthLine : integer;
: : : CodeLine :string;
: : : Found : Boolean;
: : :
: : : begin
: : : Found := False;
: : :
: : :
: : : LengthLine := length(FileStr);
: : : Position := pos(edtCode.Text,FileStr);
: : :
: : : if ( Position > 0 ) then
: : : begin
: : : Found := True;
: : : CodeLine := copy(FileStr,1,LengthLine);
: : : form1.redOutput2.lines.Add(CodeLine);
: : :
: : : delete(CodeLine,Position,length(edtCode.Text));
: : : form1.RichEdit1.Lines.add(CodeLine);
: : :
: : : end;
: : : end;
: : :
: : : IF ( Found = False ) THEN
: : : ShowMessage('The code '+ frmQues2.edtCode.Text +' wasn''t'+' found');
: : :
: : : end;
: : :
: : :
: : :
: : You can share data by either using global variables or object fields. Given that both are methods of the same form, object fields are preferred, since their scope is less than the global variables.
: :
:
:
: I did declare my variables as global
:
: {===================================================}
: : var
: : DataFile : TextFile;
: : FileStr : string;
: : {===================================================}
: :
:
:
:
: and nothing happened
:
: what is an object fields ?
:
:
:
Object fields (singular: "object field" or "field) are variables declared within an object. Here's some examples:
type
TForm1 = class(TForm)
private
protected
FDataFile: TextFile;
FFileStr: string;
public
end;
You can use them like normal properties.
And I don't know why nothing is happening. Perhaps the case of the string and substring in the Pos() call don't match. You might what to investigate using the debug watches.