Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use WriteLn with an enum type?

I'm trying to make a custom data type for days of the week but I can't get it to write it. The compiler error says this:

[Error] hours.dpr(28): Illegal type in Write/Writeln statement

program hours;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TypeDay = (Sun,Mon,Tue,Wed,Thu,Fri,Sat);

var day: TypeDay;

begin
     for day := Sun to Sat do
     begin
         writeln(day);
     end;
end.

It's in Delphi 7 on Windows.

like image 582
Tom Avatar asked Dec 16 '10 09:12

Tom


People also ask

Can we use enum as type in TypeScript?

Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.

How do you declare an enum?

An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays. Above, the WeekDays enum declares members in each line separated by a comma.

Does JavaScript support enum?

Think of Enums as a kind of closed class, the instances of which are predefined statically. There's one small problem, though — Enums are not supported in JavaScript natively. However, since Enum is a pattern, it can be replicated via some techniques and features of native JavaScript.

How do you use enums?

You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: "permanent", "temp", "apprentice"), or flags ("execute now", "defer execution").


2 Answers

You don't need to write Assembler for this; TypInfo include all that you need for do this (get the string associated to an enumerated value).

This code:

program hours;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  TypInfo;

type
  TypeDay = (Sun,Mon,Tue,Wed,Thu,Fri,Sat);

var
  day: TypeDay;
  Str:String;

begin
     for day := Sun to Sat do begin
        Str := GetEnumName(TypeInfo(TypeDay),ord(day));
         writeln(Str);
     end;
end.

And this is the output:

alt text

Regards.

like image 103
Germán Estévez -Neftalí- Avatar answered Sep 25 '22 19:09

Germán Estévez -Neftalí-


Tom, Writeln does not support a Enum as parameter. you must call to the Ordfunction to get the ordinal representation. if you wanna show the names of your TypeDay you can write a code like this.

program hours;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TypeDay     = (Sun,Mon,Tue,Wed,Thu,Fri,Sat);
const
  TypeDayStr  : Array[TypeDay] of string = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

var day: TypeDay;

begin
     for day := Sun to Sat do
       writeln( Ord(day));

     for day := Sun to Sat do
       writeln( TypeDayStr[day]);

     Readln;
end.
like image 39
RRUZ Avatar answered Sep 26 '22 19:09

RRUZ