Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Number of elements in a enumerated Delphi type

Posted on 2013-11-12
3
Medium Priority
?
536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
What we learned in Webroot's webinar on multi-vector protection.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 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