Using Delphi 2010…
I have a set of binary properties I want to group together. I have 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 unit, I can create a variable of type TTableAttrSet.
Another Unit, UTILS.PAS needs to understand the TTableAttrSet type as well. That is taken care of by the USES clauses…
Main USES Util…
Util USES Main (The 2nd uses clauses, under implementation section, so I don’t get circular reference 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 this, I run into several problems…
Problem 1). When I define my procedure definition at the top of utils.pas,
procedure TestAttr(var Attr: TTableAttrSet);
I get the error that TTableAttrSet is an Undeclared Identifier. This makes sense because the definition is in Main.pas, and the ‘uses Main.pas’ is AFTER my procedure definitions. How do I get around this? For now, I have duplicated the TTableAttrSet type definition at the top of the Utils.pas file as well as Main.pas, but this does not ‘seem the right way’.
Problem 2). The bigger issue that I am running into is a compile error. on the calling line in Main.pas
TestAttr(TabAttr);
I get the error ‘Types of actual and formal var parameters must be identifical’. To my knowledge they are identical. What is the compiler complaining about?
The simple solution is to move the declaration of
TTableAttributeTypeto the Utils unit. You can’t declare it twice because then you have two distinct types. That’s no use to you, you need only a single type.This solution will work so long as the Main unit does not need to reference
TTableAttributeTypein its interface section. Since the Utils unit clearly needs to do so then that would create a circular dependency between unit interface sections which is illegal.If both the Main and Utils units need to reference
TTableAttributeTypein their interface sections then you need to create another unit that just contains type declarations. That unit could be used by both Utils and Main in theirinterfacesections.