Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Number of elements in a enumerated Delphi type

Posted on 2013-11-12
3
Medium Priority
?
540 Views
Last Modified: 2013-11-13
How can I programmatically determine the number of elements of a enumerated type ?
For example, there is some the declaration
type
 TNumbers = (Zero,One,Two,Three  ...);

I need to find in my code the number of elements of TNumbers
Thanks
0
Comment
Question by:LeTay
3 Comments
 
LVL 3

Expert Comment

by:gskoczylas
ID: 39643872
Count := Succ(Ord(High(TNumbers));

The build-in function High() returns the highest element of the enumerated type.
0
 
LVL 19

Accepted Solution

by:
MerijnB earned 2000 total points
ID: 39643876
Try this:

type TNumbers = (Zero, One, Two, Three);
var i: integer;
begin
 i := integer(High(TNumbers)) + 1;   // i is now 4
end;

Open in new window

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39643877
with runtime type information from the TypInfo unit you can even get the name
and the number of elements

here is a sample:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TNumbers = (One, Two, Three, Four, Five);

  TForm1 = class(TForm)
    Button1: TButton;
    ComboBox1: TComboBox;
    procedure Button1Click(Sender: TObject);
  private
    procedure ListNumbers(List: TStrings);
  end;

var
  Form1: TForm1;

implementation

uses TypInfo;

{$R *.dfm}

procedure TForm1.ListNumbers(List: TStrings);
var
  I: TNumbers;
begin
  for I := Low(TNumbers) to High(TNumbers) do
    List.Add(GetEnumName(TypeInfo(TNumbers), Integer(I)));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListNumbers(Combobox1.Items);
end;

end.

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question