Delphi: How to have non-contiguous subrange enumeration type?
While the following subrange enumeration declaration works:
type
TReceiptCode = 'A'..'F';
This does not:
type
TReceiptCode = ' ','A'..'F', 'R';
Nor does
开发者_如何学Gotype
TReceiptCode = ' ','A','B','C','D','E','F','R';
How can i declare a subrange type with non-contiguous values?
Unfortunately, I don't think there's any way to do that. You can declare a (new) non-contiguous enumeration, or a subrange of an existing type, but not both.
Could you use a set instead?
TSomeCharSet= Set of Char;
SomeChars: TSomeCharSet = [' ','A','B','C','D','E','F','R'];
Could be granny and egg situation but I'm not sure what you are using then for :) ...
Well all you are left with then is creating TNonContigousCharRange yourself using a Set or array as the limiting "Range" and raising an exception when it is out of range or having a SetReceiptCode procedure to do a similar thing.
To all previous answers I would add simply that the clue is in the name of the type: subrange
Simply put, a range has a lower and an upper bound. What you describe is a set (or a subset), not a subrange so of course you cannot express it as a subrange.
精彩评论