Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case Statement Error

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
The following code I wrote produces "Constant expression violates subrange bounds" errors on the lines highlighted in red (in Delphi 6). I've tried playing about with it for a while now but am having no joy. Anyone got any ideas?
Code:
const
  BYTE_IN_BYTES = 1;
  KB_IN_BYTES = BYTE_IN_BYTES * 1024;
  MB_IN_BYTES = KB_IN_BYTES * 1024;
  GB_IN_BYTES = Int64(MB_IN_BYTES * 1024);
  TB_IN_BYTES = Int64(GB_IN_BYTES * 1024);
  PB_IN_BYTES = Int64(TB_IN_BYTES * 1024);

function FormatBytes(ASizeInBytes: Int64): String;
begin
  case ASizeInBytes of
    KB_IN_BYTES..MB_IN_BYTES - 1:   
      Result := Format('%n KB', [ASizeInBytes / KB_IN_BYTES]);
    MB_IN_BYTES..GB_IN_BYTES - 1:   
      Result := Format('%n MB', [ASizeInBytes / MB_IN_BYTES]);[COLOR=red]
    GB_IN_BYTES..TB_IN_BYTES - 1:[/color]   
      Result := Format('%n GB', [ASizeInBytes / GB_IN_BYTES]);[COLOR=red]
    TB_IN_BYTES..PB_IN_BYTES:[/color]       
      Result := Format('%n TB', [ASizeInBytes / TB_IN_BYTES]);
  else
    Result := Format('%n bytes', [ASizeInBytes]);
  end;
end;

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I dont know if it's this simple but your TB_IN_BYTES is a 13 digit number!! Quite a bit bigger than the maximum integer size, The way I read the help declaring constants in this way will assume integer?



Steve: Delphi a feersum engin indeed.
 
sggaunt said:
I dont know if it's this simple but your TB_IN_BYTES is a 13 digit number!! Quite a bit bigger than the maximum integer size
That's why I'm typecasting the GB, TB and PB constants as Int64. According to the help file, an Int64 is a signed 64-bit integer that has a range of –2^63..2^63–1 (a nineteen digit number) - sufficient to store even my petabytes!

sggaunt said:
The way I read the help declaring constants in this way will assume integer?
The example below shows that you can force a constant to be any type you want using a typecast. Which is what I have done above.
Delphi Help File said:
If constantExpression returns an ordinal value, you can specify the type of the declared constant using a value typecast. For example

const MyNumber = Int64(17);

declares a constant called MyNumber, of type Int64, that returns the integer 17. Otherwise, the type of the declared constant is the type of the constantExpression.

So I'm a bit clueless as to what the error means?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Hmm.. Dispite what it says, it seems to throw the error as soon as it trys to evaluate your TB_IN_BYTES value.
Have you tried assigning the constant directly rather than using the calculation?


Steve: Delphi a feersum engin indeed.
 
I agree it only has a problem with TB_IN_BYTES and PB_IN_BYTES. I tried assigning the actual value in bytes to the constant but the same error resulted. It's certainly a puzzling one!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I don't know if it is still being maintained but the 'Delphi Bug list' site might have somthing on this?


Steve: Delphi a feersum engin indeed.
 
Have you tried typing the constants? Would that possibly be producing what you're seeing?

i.e. instead of

GB_IN_BYTES = 1

GB_IN_BYTES: Int64 = 1;

Of course, that would be a guess without trying it out myself.
 
And another thought: what are valid integer lengths for set ranges, like you are using? It looks like it might be limited to "longint" ranges in what you are doing.

You might have to rewrite this as a series of IF statements if that is correct.
 
Glen, Thanks for your responses. I tried your 1st suggestion to no avail. However, I think you may be onto something with your 2nd suggestion - I now believe that a subrange limit is a 32-bit number which is why it can't handle an Int64. I think I'll rewrite with some IF statements as you suggested. But it looked much more elegant the way I was doing it!!!

Glen & Steve, thanks for your input - much appreciated.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top