4

I am looking to inherite a enumaration in other one:

for example:

Type TMyTypeKind = (TTypeKind, enBoolean, enPath);
ain
  • 22,394
  • 3
  • 54
  • 74
user558126
  • 1,303
  • 3
  • 21
  • 42
  • In what way can you point to any need to do this? An enumeration is just a a series of ordered ascending ordinal constant values. You can not inherit from enums. Is there any programming language out there that allows you to inherit from an enum? I'm not aware of it. Java and C# do not allow it either. http://stackoverflow.com/questions/55375/add-values-to-enum – Warren P Mar 20 '12 at 14:14

6 Answers6

6

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;
malom
  • 223
  • 2
  • 11
6

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.

RBA
  • 12,337
  • 16
  • 79
  • 126
4

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}
  );
Remko
  • 7,214
  • 2
  • 32
  • 52
3

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)

hubalazs
  • 448
  • 4
  • 13
1

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.

Mikhail Kopylov
  • 2,008
  • 5
  • 27
  • 58
1

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'
Rafael Colucci
  • 6,018
  • 4
  • 52
  • 121