Search code examples
delphidelphi-10.2-tokyo

Cannot set enum type in published property of a component


I am writing a component and this is the main class with the most important pieces of code:

uses
  Equation;

type
 TEquationSolver = class(TComponent)
   private
     FSolver: TSolverType;
   published
     property RootFindAlgorithm: TSolverType read FSolver write FSolver;
 end;

In the uses clauses I've added Equation because inside Equation.pas I have declared this kind of enum:

type
 TSolverType = (TNewtonRaphson = 0, TSecant, TBisection, TBrent);

In this way I am able to have in the IDE an option in the Object Inspector with a dropdown menu.


I have installed the component and while I was testing I've found this problem:

procedure TForm1.Button1Click(Sender: TObject);
begin
   EquationSolver1.RootFindAlgorithm := TSolverType.Secant;
end;

The error is the following:

[dcc32 Error] Unit1.pas(29): E2003 Undeclared identifier: 'TSolverType'

My question is very simple: why?

In the Unit where I am running the test (simple VCL form) there is the component with its uses included and so I am able to "see" TEquationSolver. As you can see at the top inside the unit of TEquationSolver I have included Equation and the latter has TSolverType.

The situation is the following:

enter image description here

Do I have to add something under the uses somewhere? I don't want to add stuff to the uses of Unit1.


Solution

  • If you want to make TSolverType visible to a unit (eg a Form), you must tell that unit where TSolverType is defined. That is part of how Delphi works.

    Therefore, you have to either:

    • include Equation in the uses clause of the unit where you want to us the definition (eg the Form's unit)

    • include TSolverType in your component's unit

    • hide the property (eg by making it private or protected).

    Delphi does not support implied definitions in the way you hope.