Recent

Author Topic: Using another unit  (Read 1147 times)

B4R

  • Newbie
  • Posts: 6
Using another unit
« on: June 29, 2024, 11:04:47 pm »
Created a new project.
Added another unit to the project and defined a class Utility in it. Saved the unit as 'utility.pas'.

Now, I want to use that unit in the 'unit1.pas' unit. So, I add its name to the 'uses' section under 'interface'.

This does not build: Error: Cannot find utility used by Unit1 of the Project Inspector.
Tried to add 'uses' section to 'implementation': no dice.
Tried to use 'Utility in 'utility.pas'', capitalized, all lowercase: no dice.

I know that my Delphi is rusty and I ran out of ideas.
I looked at examples, for instance at C:\lazarus\examples\dropfiles\unit2.pas and found that it simply does that which fails in my project: uses Unit1;
What am I doing wrong?

Paolo

  • Hero Member
  • *****
  • Posts: 531
Re: Using another unit
« Reply #1 on: June 29, 2024, 11:37:26 pm »
hello B4R,
not clear what you mean:

Quote
Added another unit to the project and defined a class Utility in it. Saved the unit as 'utility.pas'.

does it mean that you have a file named utility.pas with internally declared
Code: Pascal  [Select][+][-]
  1. Utility = class
  2. end;
  3.  

this combination does not compile. (Duplicate identifier)

However, If you added utility to uses and the compiler said:

Quote
Error: Cannot find utility used by Unit1 of the Project Inspector.

then your utility.pas is not in the search path of projects. where is utility.pas ?
(I suggest also to change the name of such unit in something more unique)

Thaddy

  • Hero Member
  • *****
  • Posts: 15554
  • Censorship about opinions does not belong here.
Re: Using another unit
« Reply #2 on: June 30, 2024, 11:58:02 am »
Simply keeping conventions would solve this:
Code: Pascal  [Select][+][-]
  1. TUtility = class
  2. end;
The convention for all user types is a T as prefix, although the language does not enforce it. And then the class name and the unit name do not clash anymore.
 
If I smell bad code it usually is bad code and that includes my own code.

B4R

  • Newbie
  • Posts: 6
Re: Using another unit
« Reply #3 on: June 30, 2024, 02:50:41 pm »
It's like this:

bobby100

  • Full Member
  • ***
  • Posts: 245
    • Malzilla
Re: Using another unit
« Reply #4 on: June 30, 2024, 02:58:37 pm »
You have a typo in your unit name (see your screenshot)
utility <> uitlity

Paolo

  • Hero Member
  • *****
  • Posts: 531
Re: Using another unit
« Reply #5 on: June 30, 2024, 02:59:06 pm »
But you wrote wrong file name!!!!

Thaddy

  • Hero Member
  • *****
  • Posts: 15554
  • Censorship about opinions does not belong here.
Re: Using another unit
« Reply #6 on: June 30, 2024, 04:16:38 pm »
With units it is also important that the file name on disk matches the unit name exactly, except for the pas or pp extension.
If I smell bad code it usually is bad code and that includes my own code.

B4R

  • Newbie
  • Posts: 6
Re: Using another unit
« Reply #7 on: June 30, 2024, 05:09:22 pm »
You have a typo in your unit name (see your screenshot)
utility <> uitlity

Bingo!
Thank you, sir!

 

TinyPortal © 2005-2018