Recent

Author Topic: Object Pascal: Passing Arguments  (Read 1144 times)

hymcode

  • New Member
  • *
  • Posts: 20
Object Pascal: Passing Arguments
« on: August 17, 2020, 05:39:18 pm »
Hello everyone I am unsure of how to pass argumens in Object Pascal and wanted to know what the correct procedure for this is.

I have a Button1Click procedure and it stores the value entered into Edit1 into a variable myString. This stored value is then displayed in Edit2.

In my Button2Click procedure I would like to take the value of myString, store it in a variable myString2 and then display it in Edit3.

This is what I currently have:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   myString : String;
  4. begin
  5.   myString := Edit1.Text;
  6.  
  7.   Edit2.Caption := myString;
  8.   Edit1.Caption := '';
  9. end;
  10.  
  11. procedure TForm1.Button2Click(Sender: TObject);
  12. var
  13.   myString2 : String;
  14. begin
  15.   myString2 := {value stored in myString from button1Click procedure}
  16. end;

I hope this makes sense and thanks in advance
« Last Edit: August 25, 2020, 03:58:20 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Object Pascal Passing Arguments
« Reply #1 on: August 17, 2020, 05:47:02 pm »
You have to use global variable or field of Form1.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

hymcode

  • New Member
  • *
  • Posts: 20
Re: Object Pascal Passing Arguments
« Reply #2 on: August 17, 2020, 06:04:37 pm »
You have to use global variable or field of Form1.

Sorry I'm still very new to Object Pascal and am not sure what you're saying. To my understanding for normal Pascal it is recommended where possible to just use local variable declarations and then pass them to other procedures or functions that need access to them.
Don’t wish it were easier; wish you were better. – Jim Rohn

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Object Pascal Passing Arguments
« Reply #3 on: August 17, 2020, 06:26:13 pm »
Just look what happens:

User of your program clicks Button1.
Edit1.Text is stored to mystring.
Edit2.Text is changed to mystring.
Edit1.Text is reseted.
Now the method ends and it means that all local variables are lost.

When user clicks Button2, mystring does not exist anywhere. Therefore it has to be global.

Note: Edit1.Caption is wrong. Edit1.Text is right.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: Object Pascal Passing Arguments
« Reply #4 on: August 17, 2020, 06:31:54 pm »
Sorry I'm still very new to Object Pascal and am not sure what you're saying. To my understanding for normal Pascal it is recommended where possible to just use local variable declarations and then pass them to other procedures or functions that need access to them.
You are right that that is the recommended way of passing around variables/values. However the buttonclick methods are predefined and don't offer a way to pass along variables that way. So the best next thing is to create/add a form variable to store your value(s) (as suggested by Blaazen)

It is also allowed to use a global variable (or global to the unit by placing the declaration in your implementation section)  also as suggested by Blaazen, however when applying the same rules that you are trying to follow then that would be the most 'evil' way of doing it (which doesn't mean it is bad to do it anyway as sometimes you just can't accomplish certain tasks without it, at least not when you are new to objectpascal).

Even the fcl/lcl makes use of global declared variables because sometimes it just makes life a lot easier.

It common to add your own properties/methods/variable declarations to your form or implement your own class that takes care of keeping track of your 'global' variables in an organised manner.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Object Pascal Passing Arguments
« Reply #5 on: August 17, 2020, 08:46:47 pm »
To resume, using Blaazen way:

Code: Pascal  [Select][+][-]
  1. program myprog;
  2.  
  3. ...
  4.  
  5. var
  6.   myString : String;
  7.  
  8. ...
  9.  
  10. procedure TForm1.Button1Click(Sender: TObject);
  11. begin
  12.   myString := Edit1.Text;
  13.   Edit2.text := myString;
  14.   Edit1.text := '';
  15. end;
  16.  
  17. procedure TForm1.Button2Click(Sender: TObject);
  18. var
  19.   myString2 : String;
  20. begin
  21.   myString2 := myString;
  22.   Edit3.text :=  myString2;
  23. end;


But why dont you simply do this ?:

Code: Pascal  [Select][+][-]
  1. program myprog;
  2. ...
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   Edit2.text := Edit1.Text;
  7.   Edit1.text := '';
  8. end;
  9.  
  10. procedure TForm1.Button2Click(Sender: TObject);
  11. begin
  12.   Edit3.text := Edit2.text;
  13. end;
« Last Edit: August 17, 2020, 11:53:41 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

hymcode

  • New Member
  • *
  • Posts: 20
Re: Object Pascal Passing Arguments
« Reply #6 on: August 18, 2020, 09:59:07 am »
To resume, using Blaazen way:

Code: Pascal  [Select][+][-]
  1. program myprog;
  2.  
  3. ...
  4.  
  5. var
  6.   myString : String;
  7.  
  8. ...
  9.  
  10. procedure TForm1.Button1Click(Sender: TObject);
  11. begin
  12.   myString := Edit1.Text;
  13.   Edit2.text := myString;
  14.   Edit1.text := '';
  15. end;
  16.  
  17. procedure TForm1.Button2Click(Sender: TObject);
  18. var
  19.   myString2 : String;
  20. begin
  21.   myString2 := myString;
  22.   Edit3.text :=  myString2;
  23. end;


But why dont you simply do this ?:

Code: Pascal  [Select][+][-]
  1. program myprog;
  2. ...
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   Edit2.text := Edit1.Text;
  7.   Edit1.text := '';
  8. end;
  9.  
  10. procedure TForm1.Button2Click(Sender: TObject);
  11. begin
  12.   Edit3.text := Edit2.text;
  13. end;


Thank you for responding. Your approach is also possible, but I would like to store values input by the user so that I can manipulate them later. I'm working towards learning how to make an Object Pascal version of my Pascal terminal program, in which I took numbers, broke them up and displayed them in pieces

Based on your code I've modified it like so, which sort of achieves what I'm after.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   myString : String;
  4. begin
  5.   myString := Edit1.Text;
  6.  
  7.   Edit2.Caption := myString;
  8.   Edit1.Caption := ''; {clear Edit box after data is stored}
  9. end;
  10.  
  11. procedure TForm1.Button2Click(Sender: TObject);
  12. var
  13.   myString2 : String;
  14. begin
  15.   myString2 := Edit2.Text;
  16.   Edit2.Caption:= '';
  17.   Edit3.Caption := myString2;
  18. end;
Don’t wish it were easier; wish you were better. – Jim Rohn

 

TinyPortal © 2005-2018