Enumeration inheritance delphi

I am looking to inherite a enumaration in other one:

for example:

Type TMyTypeKind = (TTypeKind, enBoolean, enPath);

Answers


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.


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;

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}
  );

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)


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'

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.


Need Your Help

How do you keep html semantically correct and and handle big css of complex designs like Big portals or News sites?

css xhtml accessibility semantic-markup

In personal and small business websites it's not so difficult but when we comes to New sites,Portals and E-commerce sites, where so many content comes on each pages, it's little hard.

How to get the last typed word in jtextpane?

java string input jtextpane

How can I get the last typed word of a JTextPane? I am NOT asking for the last word of the string in the JTextPane. I want to store every typed word in a stack in the end, in order, and simply just