Procedure help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Procedure help

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?


[code]
{===================================================}
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;

[/code]

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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?
    :
    :
    : [code]
    : {===================================================}
    : 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;
    :
    : [/code]
    :
    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.
  • Hav0c #1Hav0c #1 Posts: 50Member
    : : 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?
    : :
    : :
    : : [code]
    : : {===================================================}
    : : 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;
    : :
    : : [/code]
    : :
    : 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
    [code]
    {===================================================}
    : var
    : DataFile : TextFile;
    : FileStr : string;
    : {===================================================}
    :

    [/code]

    and nothing happened

    what is an object fields ?


  • zibadianzibadian Posts: 6,349Member
    [b][red]This message was edited by zibadian at 2006-2-3 4:31:54[/red][/b][hr]
    : : : 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?
    : : :
    : : :
    : : : [code]
    : : : {===================================================}
    : : : 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;
    : : :
    : : : [/code]
    : : :
    : : 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
    : [code]
    : {===================================================}
    : : var
    : : DataFile : TextFile;
    : : FileStr : string;
    : : {===================================================}
    : :
    :
    : [/code]
    :
    : 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:
    [code]
    type
    TForm1 = class(TForm)
    private
    protected
    FDataFile: TextFile;
    FFileStr: string;
    public
    end;
    [/code]
    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.
Sign In or Register to comment.