Delphi and Kylix

Moderators: pritaeas
Number of threads: 7244
Number of posts: 19051

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Procedure help Posted by Hav0c #1 on 2 Feb 2006 at 6:35 AM
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;


Report
Re: Procedure help Posted by zibadian on 3 Feb 2006 at 3:39 AM
: 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.
Report
Re: Procedure help Posted by Hav0c #1 on 3 Feb 2006 at 4:21 AM
: : 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 ?


Report
Re: Procedure help Posted by zibadian on 3 Feb 2006 at 4:29 AM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.