user558126
user558126

Reputation: 1271

Enumeration inheritance delphi

I am looking to inherite a enumaration in other one:

for example:

Type TMyTypeKind = (TTypeKind, enBoolean, enPath);

Upvotes: 4

Views: 1993

Answers (6)

malom
malom

Reputation: 233

Something similar is possible in the reverse order. If you know all the possible values, define it as a base type and declare subrange types of it. The subranges will be assignement compatible with the base type and with each other. It may or may not be a benefit.

type
 TEnumAll = (enFirst, enSecond, enThird, enFourth, enFifth);
 TEnumLower = enFirst..enThird;
 TEnumMore = enFirst..enFourth;
procedure TForm1.Test1;
var
  All: TEnumAll;
  Lower: TEnumLower;
begin
  for All := Low(TEnumAll) to High(TEnumAll) do begin
   Lower := All;
  end;
  for Lower := Low(TEnumLower) to High(TEnumLower) do begin
    All := Lower;
  end;
end;

Upvotes: 6

Mikhail Kopylov
Mikhail Kopylov

Reputation: 2058

As it was already said, you can't. But you may do this way:

TBaseState = class
  public const
    stNone = 1;
    stSingle = 2;
  end;

  TMyState = class(TBaseState)
  public const
    stNewState = 3;
  end;

  var
    state: TMyState;

  begin
    ShowMessage(IntToStr(s.stNewState));
  end;

It isn't the same with enums, but sometimes it helps.

Upvotes: 1

Remko
Remko

Reputation: 7340

It can be done with an trick, using Include files. Example:

AdCommonAttributes.inc

canonicalName,
cn,
whenCreated,
description,
displayName,
distinguishedName,
instanceType,
memberOf,
modifyTimeStamp,
name,
objectCategory,
objectClass,
objectGuid,
showInAdvancedViewOnly

AdUserGroupCommonAttributes.inc:

msDSPrincipalName,
objectSid,
sAMAccountName

AdUserAttributers.inc:

accountExpires,
badPasswordTime,
badPwdCount,
c,
comment,
company,
department,
division,
employeeID,
givenName,
homeDirectory,
homeDrive,
lastLogon,
lockoutTime,
logonCount,
pwdLastSet,
sn,
telephoneNumber,
tokenGroups,
userAccountControl,
userPrincipalName

unit AdUserGroupCommonAttributes;

   TAdUserGroupCommonAttributes = (
    {$I AdCommonAttribs.inc}, {$I AdUserGroupCommonAttributes.inc}
   );

unit AdGroupAttributes;

type
  TAdGroupAttributes = (
    {$I AdCommonAttribs.inc},
    {$I AdUserGroupCommonAttributes.inc},
    {$I AdGroupAttributes.inc}
  );

unit AdUserAttributes;

type
  TAdUserAttributes = (
    {$I AdCommonAttribs.inc},
    {$I AdUserGroupCommonAttributes.inc},
    {$I AdUserAttributes.inc}
  );

Upvotes: 4

RBA
RBA

Reputation: 12584

You can not. Compiler does not know how to interpret this. From the wiki :

An enumerated type defines an ordered set of values by simply listing identifiers that denote these values. The values have no inherent meaning.

Upvotes: 6

hubalazs
hubalazs

Reputation: 449

This is not possible because the enumerated names should be unique. You cannot use the values of TTypeKind in another enumeration, it generates conflict.

However in Delphi 2009 there is a feature called scoped enums. You can say TMyTypeKind.enBoolean.

But this does not solve the inheritance.

One way is to assign integer constants to the enum values:

Type TMyTypeKind = (enBoolean = High(TTypeKind) + 1, enPath = High(TTypeKind) + 2);

So you can have an index number that begins in Low(TTypeKind) and ends in High(TMyTypeKind)

See it for yourself: Ord(enBoolean)

Upvotes: 3

Rafael Colucci
Rafael Colucci

Reputation: 6078

I am afraid this is not possible at all. Theres nothing you can do about it, I am sorry,

When you type:

Type TMyTypeKind = (TTypeKind, enBoolean, enPath);

Delphi will see that TTypeKind is already a type and it will give you the follow error:

[DCC Error] xxx.pas(41): E2004 Identifier redeclared: 'TTypeKind'

Upvotes: 1

Related Questions