Recent

Author Topic: How do you move a Procedure to a separate file  (Read 1174 times)

Aruna

  • Sr. Member
  • ****
  • Posts: 456
How do you move a Procedure to a separate file
« on: September 15, 2024, 09:05:14 pm »
I am uncertain if I should ask this in the General forum category or here. I am using SynEdit so decided to ask here. Apologies if this is not the right place to ask.
 
I am working on my own text editor after the one I use regularly segfault-ed on me ( Geany ). I have a working unit and I have managed to put together some basic editing functionality. I noticed the code is getting a bit long so decided to move some procedures to their own separate file and call them from Unit1 but I am getting many different errors. The procedure below is one I am trying to move into a separate file. How do you usually do this?

The code below works just fine under Unit1. If I move it to a separate file starts complaining.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.GoToLine;
  2. var
  3.   LineNumber: Integer;
  4.   InputLine: string;
  5.  
  6. begin
  7.   // Prompt the user for the line number
  8.   InputLine := InputBox('Go to Line', 'Enter Line Number:', '');
  9.  
  10.   // Convert the input to an integer
  11.   if TryStrToInt(InputLine, LineNumber) then
  12.   begin
  13.     // Make sure the line number is valid
  14.     if (LineNumber > 0) and (LineNumber <= SynEdit1.Lines.Count) then
  15.     begin
  16.       // Move the caret to the specified line (column 1)
  17.       SynEdit1.CaretY := LineNumber;
  18.       SynEdit1.CaretX := 1;
  19.       // Ensure the caret is visible
  20.       SynEdit1.EnsureCursorPosVisible;
  21.     end
  22.     else
  23.       ShowMessage('Invalid line number!');
  24.   end
  25.   else
  26.     ShowMessage('Please enter a valid number.');
  27. end;                                          
Debian GNU/Linux 11 (bullseye)
https://pascal.chat/

alpine

  • Hero Member
  • *****
  • Posts: 1263
Re: How do you move a Procedure to a separate file
« Reply #1 on: September 15, 2024, 09:40:15 pm »
Quote
The code below works just fine under Unit1. If I move it to a separate file starts complaining.
Usually the reason for the complaint is shown as text. Proceed accordingly to remove that reason. Or tell us what exactly is the error message.

The given code is a method of TForm1, which includes in its scope the SynEdit1. Moving that code in another file (how exactly?) will take it out from TForm1 scope and SynEdit1 won't be visible. Or may be there is a syntax error at first?
 
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

440bx

  • Hero Member
  • *****
  • Posts: 4537
Re: How do you move a Procedure to a separate file
« Reply #2 on: September 15, 2024, 09:43:57 pm »
@alpine already alluded to what I'm going to ask, which is, when you say "separate file", do you mean a separate unit or an include file ?

That makes a big difference.

Also, as @alpine alluded to, just stating the compiler is complaining isn't very helpful.  What is the textual compiler complaint ?.

summary: a problem is easier to solve when it is known.
« Last Edit: September 16, 2024, 03:18:48 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Aruna

  • Sr. Member
  • ****
  • Posts: 456
Re: How do you move a Procedure to a separate file
« Reply #3 on: September 15, 2024, 10:20:08 pm »
Quote
The code below works just fine under Unit1. If I move it to a separate file starts complaining.
Usually the reason for the complaint is shown as text. Proceed accordingly to remove that reason. Or tell us what exactly is the error message.
I have attached a screenshot of the errors.

The given code is a method of TForm1, which includes in its scope the SynEdit1. Moving that code in another file (how exactly?) will take it out from TForm1 scope and SynEdit1 won't be visible. Or may be there is a syntax error at first?
Yes @alpine your absolutely correct. This is the problem. Once I move my code to another unit SynEdit is out of scope and no longer visible and I have no idea how to pas it along to my procedure file.
Debian GNU/Linux 11 (bullseye)
https://pascal.chat/

Aruna

  • Sr. Member
  • ****
  • Posts: 456
Re: How do you move a Procedure to a separate file
« Reply #4 on: September 15, 2024, 10:22:44 pm »
@alpine already alluded to what I'm going to ask, which is, when you say "separate file", do you mean a separate unit or an include file ?
Forgive me for not being specific. I am moving code from Unit1 to new Unit3. Hope that makes things clearer

That makes a big difference.

Also, as @alpine alluded to, just stating the compiler is complaining isn't very helpful.  What is the textual compiler complain.

summary: a problem is easier to solve when it is known.
I attached a screenshot showing the errors in my reply to to @alpines.
Debian GNU/Linux 11 (bullseye)
https://pascal.chat/

dsiders

  • Hero Member
  • *****
  • Posts: 1238
Re: How do you move a Procedure to a separate file
« Reply #5 on: September 15, 2024, 10:39:21 pm »
@alpine already alluded to what I'm going to ask, which is, when you say "separate file", do you mean a separate unit or an include file ?
Forgive me for not being specific. I am moving code from Unit1 to new Unit3. Hope that makes things clearer

That makes a big difference.

Also, as @alpine alluded to, just stating the compiler is complaining isn't very helpful.  What is the textual compiler complain.

summary: a problem is easier to solve when it is known.
I attached a screenshot showing the errors in my reply to to @alpines.

The object inspector shows the TSynEdit instance is named SynEdit1, SynEdit <> SynEdit1.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Sieben

  • Sr. Member
  • ****
  • Posts: 326
Re: How do you move a Procedure to a separate file
« Reply #6 on: September 15, 2024, 10:50:54 pm »
You need to precede the name of the instance of your TSynEdit - SynEdit1 - with the the name of the instance of your TForm1. Assuming that TForm1 is your main form and that it will be created automatically that would be Form1. Unit1 has to be in the uses of Unit3 to make this reference possible.

Code: Pascal  [Select][+][-]
  1. Form1.SynEdit1

Another and more generic way of doing that would be to pass the instance of TSynEdit as a parameter to your GoToLine method.

And, if I may give some advice, rename your units, forms and every other component you use to sth that tells you what that entity is about. Once your project grows or you want to handle a couple of them you will realize how much time it saves.

« Last Edit: September 15, 2024, 10:58:02 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Aruna

  • Sr. Member
  • ****
  • Posts: 456
Re: How do you move a Procedure to a separate file
« Reply #7 on: September 15, 2024, 10:53:44 pm »
The object inspector shows the TSynEdit instance is named SynEdit1, SynEdit <> SynEdit1.
I manged to get things working. Thank you @dsiders.

The zip has the source code. If anyone wants to fiddle with it.

So far I can open a file, make changes, then save the file. Edit menu option yet to be coded. Search menu option 'goto line' works the others not yet. The window menu option has a Vertical Split and a Horizontal split. Both work but I am yet learning how to correctly use this. Can use some help. It will open two files no problem. I do not how to save each one yet.

The status bar will show you the full path of the file currently being worked on. Current row and rowcount. Cursor Column position. Date and Time ( real-time clock)

All critiques and suggestions are welcome. Any feedback is very much appreciated. Thanks.

What I did to get things working is File-->New Unit then plugged in my gotoline procedure but had to declare it like this:.
Code: Pascal  [Select][+][-]
  1. procedure GoToLine(SynEdit: TSynEdit);


Then in the main Unit1  in the uses section plugged in unit3 and called teh procedure like this:
Code: Pascal  [Select][+][-]
  1.   GoToLine(SynEdit1);
Done!

EDIT: Sorry, forgot to attach the zip. It is there now.
edit2: added the *.lpi file
« Last Edit: September 16, 2024, 12:29:32 am by Aruna »
Debian GNU/Linux 11 (bullseye)
https://pascal.chat/

Aruna

  • Sr. Member
  • ****
  • Posts: 456
Re: How do you move a Procedure to a separate file
« Reply #8 on: September 15, 2024, 11:03:28 pm »
You need to precede the name of the instance of your TSynEdit - SynEdit1 - with the the name of the instance of your TForm1. Assuming that TForm1 is your main form and that it will be created automatically that would be Form1. Unit1 has to be in the uses of Unit3 to make this reference possible.
When I was experimenting I did this and it said something about a 'circular reference' 

Code: Pascal  [Select][+][-]
  1. Form1.SynEdit1

Another and more generic way of doing that would be to pass the instance of TSynEdit as a parameter to your GoToLine method.
Yes this is what I did. I am still very new to this object oriented stuff.

And, if I may give some advice, rename your units, forms and every other component you use to sth that tells you what that entity is about. Once your project grows or you want to handle a couple of them you will realize how much time it saves.
Thank you for the advice @Sieben I will try to do so.
Debian GNU/Linux 11 (bullseye)
https://pascal.chat/

Sieben

  • Sr. Member
  • ****
  • Posts: 326
Re: How do you move a Procedure to a separate file
« Reply #9 on: September 15, 2024, 11:11:18 pm »
Quote
When I was experimenting I did this and it said something about a 'circular reference

Sorry, I forgot to mention that it should be the uses list of the implementation section.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Aruna

  • Sr. Member
  • ****
  • Posts: 456
Re: How do you move a Procedure to a separate file
« Reply #10 on: September 15, 2024, 11:18:41 pm »
Quote
When I was experimenting I did this and it said something about a 'circular reference
Sorry, I forgot to mention that it should be the uses list of the implementation section.
I thought we can only have a single Uses list in the interface section? ( I am confused a bit now )
Debian GNU/Linux 11 (bullseye)
https://pascal.chat/

440bx

  • Hero Member
  • *****
  • Posts: 4537
Re: How do you move a Procedure to a separate file
« Reply #11 on: September 15, 2024, 11:21:27 pm »
Forgive me for not being specific.

No problem.  As you could tell from the replies that followed the additional (and more precise) information you provided was definitely useful. ;)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Sieben

  • Sr. Member
  • ****
  • Posts: 326
Re: How do you move a Procedure to a separate file
« Reply #12 on: September 15, 2024, 11:33:03 pm »
You can have another one in the implementation section, right below the keyword like with the interface section.

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. uses Variants, StrUtils;

Thus you can prevent a circular reference - provided you don't already have to reference contents of that unit in the interface section. You should always add to that uses clause if you don't need to do otherwise.
« Last Edit: September 15, 2024, 11:37:07 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Aruna

  • Sr. Member
  • ****
  • Posts: 456
Re: How do you move a Procedure to a separate file
« Reply #13 on: September 16, 2024, 12:59:17 am »
Forgive me for not being specific.

No problem.  As you could tell from the replies that followed the additional (and more precise) information you provided was definitely useful. ;)
Unit1 has the Menubar which I use to show the options form form2 which is in unit2.  Unit 2 has the checkbox that I use to enable/disable the statusbar clock. This is causing that 'circular unit refereance' again.How do I resolve this?

To show Form2 I have to include it in Unit1 uses section. And for form2 checkbox to send the checked/unchecked status I have to include Form1 in the uses section of Form2 thus the circular referance. What are my options? Can this be fixed? Or I  have to think of another way?
Debian GNU/Linux 11 (bullseye)
https://pascal.chat/

440bx

  • Hero Member
  • *****
  • Posts: 4537
Re: How do you move a Procedure to a separate file
« Reply #14 on: September 16, 2024, 03:17:40 am »
Unit1 has the Menubar which I use to show the options form form2 which is in unit2.  Unit 2 has the checkbox that I use to enable/disable the statusbar clock. This is causing that 'circular unit refereance' again.How do I resolve this?

To show Form2 I have to include it in Unit1 uses section. And for form2 checkbox to send the checked/unchecked status I have to include Form1 in the uses section of Form2 thus the circular referance. What are my options? Can this be fixed? Or I  have to think of another way?
From what you described, I guess the code you have does not currently compile.  In spite of that, I suggest you attach it to your next post.  The attachment will be more precise than a description and, a solution, if there is one, that is proven to work can be attached back by me or someone else.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018