Lazarus – ways to extract values from an enumeration
Hello, guys! We're continuing to master Lazarus. It's a wonderful programming language similar to Delphi. There's a data type called an enumeration. And sometimes we want to extract values from it. Here's a funny question: How do we do this? We'll look at that today.
Let's
create a standard project in Lazarus. With a Windows window. In the TYPE
section, we'll add our enumeration. Let's say it's the seasons of the year:
Autumn, Winter, Spring, Summer.
ESeason =
(Autumn, Winter, Spring, Summer);
Method 1.
WriteStr. The simplest.
And here's
the simplest method. It's using WriteStr. We write the desired enumeration type
element into a string.
Var
MySeason:
ESeason;
StrVal:String;
begin
WriteStr(StrVal,MySeason);
Memo1.Lines.Add('MySeason='
+ StrVal);
WriteStr(StrVal,Winter);
Memo1.Lines.Add(Winter='
+ StrVal);
WriteStr(StrVal,ESeason.Spring);
Memo1.Lines.Add('ESeason.Spring='
+ StrVal);
End;
Our StrVal
text variable can hold a wide variety of enumeration values. This is all
possible! Quite flexible and variable! Notice how many possibilities there are.
You can pass a variable of an enumeration type, like MySeason. You can also
pass one of the enumeration elements directly by name, like Winter. And you can
pass one of the enumeration values with the full name, as expected. The
enumeration type and its specific value separated by a dot. For example,
ESeason.Spring.
Method 2.
GetEnumName (), Ord(). More complicated.
First, we
include TypInfo in Uses.
And here's
an important detail. TypeInfo doesn't work if enumerations have assigned
numeric values.
For
example, if an enumeration is defined like this:
ESeason2 =
(Autumn=1, Winter, Spring, Summer); //typeinfo doesn't work when there are
numeric values!
So, we're
continuing to work with our enumeration from the beginning. This will work:
ESeason =
(Autumn, Winter, Spring, Summer);
Now let's
move on to the code. Extracting a value from the enumeration is done in several
steps.
Preliminary
detail. We obtain the numeric equivalent of the value via Ord().
Ord(MySeason)
Preliminary
detail. We obtain information about our enumeration. Via TypeInfo.
TypeInfo(ESeason)
And we do
everything through the conversion operator. Convert to a string using
GetEnumType
StrVal:=
GetEnumType(TypeInfo(ESeason),Ord(MySeason))
Pass the
type of our enumeration to GetEnumType. This is ESeason. And the numeric value
we're interested in is MySeason.
IntVal:=ord(MySeason);
Example
Program
var
MySeason:ESeason;
IntVal:
integer;
StrVal:String;
begin
StrVal:=GetEnumName(TypeInfo(ESeason),ord(MySeason));
Memo1.Lines.Add('MySeason
Integer value = ' + IntToStr(IntVal));
Memo1.Lines.Add('MySeason
String value = ' + StrVal);
IntVal:=Ord(Winter);
StrVal:=GetEnumName(TypeInfo(ESeason),ord(Winter));
Memo1.Lines.Add('Winter
Integer - ' + IntToStr(IntVal));
Memo1.Lines.Add('Winter
String - ' + StrVal);
IntVal:=Ord(ESeason.Spring);
StrVal:=GetEnumName(TypeInfo(ESeason),ord(ESeason.Spring));
Memo1.Lines.Add('ESeason.Spring
Integer - ' + IntToStr(IntVal));
Memo1.Lines.Add('ESeason.Spring
String - ' + StrVal);
End;
Method 3.
Manually assigning values. The most sophisticated method.
To do this,
we'll create a chain. We'll also write the identifiers into a string array.
This still
works as a constant. And with ESeason.
const
MySeasonNames: Array[ESeason] of string =('Autumn','Winter','Spring','Summer');
Let me
remind you that the enumeration itself looks like this:
ESeason =
(Autumn, Winter, Spring, Summer);
We're
essentially associating the enumeration with a special array. In the array, all
values are written as strings.
Now we can
access this array by passing the enumeration value as an array index.
For
example, like this
StrVal:=MySeasonNames[MySeason];
Or like
this
StrVal:=MySeasonNames[Winter];
Or like
this
StrVal:=MySeasonNames[ESeason.Spring];
We've sort
of mapped the array, associating it with the enumeration elements. A clever
move. And now we access our map. By throwing enumeration values into an
array. The array index is the enumeration value.
Var
MySeason:ESeason;
IntVal:
integer;
StrVal:
String;
begin
StrVal:=MySeasonNames[MySeason];
Memo1.Lines.Add('MySeason
=' + StrVal);
StrVal:=MySeasonNames[Winter];
Memo1.Lines.Add('Winter
- ' + StrVal);
StrVal:=MySeasonNames[ESeason.Spring];
Memo1.Lines.Add('MySeason.Spring
- ' + StrVal);
End;
Those are
three such methods! Delphi is interesting! Let's learn, guys! I'm learning too!
And I'm learning everything gradually. Sometimes I spend a lot of time even on
a couple of lines of code.
Dima Link is making retro videogames, apps, a little of music, write stories, and some retro more.
WEBSITE: http://www.dimalink.tv-games.ru/home_eng.html
ITCHIO: https://dimalink.itch.io/
TUMBLR: https://dimalink.tumblr.com/
BLOGGER: https://dimalinkeng.blogspot.com/
MASTODON: https://mastodon.social/@DimaLink





Comments
Post a Comment