Discussion:
Delphi language problem: enumeration elements differ from array[enumeration]
(too old to reply)
Skybuck Flying
2012-06-01 11:23:21 UTC
Permalink
Hello,

The following example gives problems in Delphi XE2:

const
ErrorCode = ( 0, 1, 4, 5 );

ErrorString : array[ErrorCode] of string =
(
'Unknown', 'Success', 'Failure', 'Bad'
)

Delphi compiler will give an error message like:

[DCC Error] E2072 Number of elements (4)differs from declaration (6)

Needless to say this sux.

A solution could be something like:

ErrorString : array[ErrorCode] of string =
(
0 : ('Unknown'),
1 : ('Success'),
4 : ('Failure'),
5 : ('Bad')
)

This does not work, is there a solution ???

Bye,
Skybuck.
Skybuck Flying
2012-06-01 11:33:30 UTC
Permalink
The real code translated from C looks something like:

ErrorCode = ( 0, 1, 2, etc, 8, 200, 201, 999 );

I could insert empty strings so that a lookup can be done.

However if the api would return a new error code beyond the array limits
then it would still crash the app...

At least by insert empty string a lookup can be done easily without having
to write let's of code so only a range check needed.

So I am going with that solution for now ! ;)

Bye,
Skybuck.
Skybuck Flying
2012-06-01 11:39:38 UTC
Permalink
No, I changed my mind, this would require inserting too many
'error_undefined' strings.

Instead I am going to split the ErrorString array up into multiple
ErrorString arrays like so:

ErrorString0to8

ErrorString200to201

Etc

Since I have to write a function for safety just a few if statements/range
checks needed to see if error code falls within these ranges
and use the correct lookup table or else return error undefined, more
efficient that way and faster to code.

Bye,
Skybuck.
Jamie
2012-06-01 12:08:21 UTC
Permalink
Post by Skybuck Flying
Hello,
const
ErrorCode = ( 0, 1, 4, 5 );
ErrorString : array[ErrorCode] of string =
(
'Unknown', 'Success', 'Failure', 'Bad'
)
[DCC Error] E2072 Number of elements (4)differs from declaration (6)
Needless to say this sux.
ErrorString : array[ErrorCode] of string =
(
0 : ('Unknown'), 1 : ('Success'), 4 : ('Failure'), 5 : ('Bad')
)
This does not work, is there a solution ???
Bye,
Skybuck.
And the compiler is correct in telling you that..

You are using a set, the compiler sees the largest possible value
that every could be applied which is 6, because 0 is also considered a
usable index..
When creating an array based from this, the compile expects you to
have 6 elements.. It needs to, because you have allowed a enumerator to
reach that high..

It does not matter the number of indices you have declared, it is the
highest possible value that is in that enumerator.

I could do this

ErrorCode =(6).

It would bee 7 elements for the array.

PLus you are actually using that slightly incorrect...

if you were to assign tag names instead of numbers, the compiler
would generated a 0,1,2,3 list, instead. But you elected to directly insert
the numbers so it just picked out the largest number you have, including
the 0.

Jamie
Skybuck Flying
2012-06-01 12:09:02 UTC
Permalink
Example was slightly wrong, here is a more correct example:

Hello,

The following example gives problems in Delphi XE2:

type
ErrorCode = ( Unknown = 0, Success=1, Failure=4, Bad=5 );

const
ErrorString : array[ErrorCode] of string =
(
'Unknown', 'Success', 'Failure', 'Bad'
)

Delphi compiler will give an error message like:

[DCC Error] E2072 Number of elements (4)differs from declaration (6)

Needless to say this sux.

A solution could be something like:

ErrorString : array[ErrorCode] of string =
(
0 : ('Unknown'),
1 : ('Success'),
4 : ('Failure'),
5 : ('Bad')
)

This does not work, is there a solution ???

Bye,
Skybuck.

Loading...