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.
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.
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.
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.
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").
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:
Regards.
Tom, Writeln
does not support a Enum as parameter. you must call to the Ord
function 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With