Delphi's problem with using type definitions between units

Using Delphi 2010 ... I have a set of binary properties that I want to combine together. I defined it as such ...

type TTableAttributeType = ( tabROOT = 1, tabONLINE = 2, tabPARTITIONED = 3, tabCOMPRESSED = 4, ); // Make a set of of the Table Attribute types... type TTableAttrSet = Set of TTableAttributeType; 

In my MAIN.PAS module, I can create a variable of type TTableAttrSet. Another module, UTILS.PAS must also understand the type TTableAttrSet. This is stated in the USES articles ...

Main USES Util ... Util USES Main (The second uses the sentences in the implementation section, so I don't get any circular help problems ....

So far so good. My problem is that I need to pass a var variable of type TTableAttrSet FROM main to Utils.

In main.pas

 procedure TForm1.Button1Click(Sender: TObject); var TabAttr : TTableAttrSet; begin TestAttr (TabAttr); end; 

and in utils.pas I have

 procedure TestAttr(var Attr: TTableAttrSet); begin Attr := [tabROOT, tabCOMPRESSED]; end; 

When I try to do this, I have several problems ... Task 1). When I define my procedure definition at the top of utils.pas,

 procedure TestAttr(var Attr: TTableAttrSet); 

I get an error that TTableAttrSet is an Undeclared Identifier. This makes sense because the definition is in Main.pas, and "use Main.pas" AFTER the definition of my procedures. How do I get around this? At the moment, I have duplicated the definition of the type TTableAttrSet at the top of the Utils.pas file as well as Main.pas, but this does not seem to be the correct path.

Task 2). The big problem I am facing is a compilation error. on the calling line in Main.pas

 TestAttr(TabAttr); 

I get the error "The types of the real and formal parameters of var must be identical." As far as I know, they are identical. What does the compiler complain about?

+4
source share
1 answer

A simple solution is to move the TTableAttributeType to the Utils node. You cannot declare it twice, because then you have two different types. This does not suit you, you only need one type.

This solution will work until the main device needs to reference TTableAttributeType in the interface section. Since the Utils business unit must explicitly do this, this will create a cyclical dependency between sections of the interface section, which is illegal.

If both Main and Utils sections must reference TTableAttributeType in their sections of the interface, you need to create another unit that simply contains type declarations. This block can be used by both Utils and Main in the interface section.

+6
source

All Articles