?
Solved

const indenfer is not constant in case statements??

Posted on 2004-04-27
7
Medium Priority
?
1,406 Views
Last Modified: 2007-12-19
Hi.all,I have puzzy of delphi's native grammar
//;===========================================
const
  CMPP_CONNECT_RESP:    DWORD = $80000001;

//............................
var
  i:dword;
//............................
  case i of
    CMPP_CONNECT_RESP:
    begin
      //someting
    end;
  end;
//;============================================
the complier told me at CMPP_CONNECT_RESP is "Constant expression expected"
faint!
it means is the const not an constant????
//;============================================
  case i of
    $80000001:
    begin
      //someting
    end;
  end;
//;============================================
passed comilite and worked.
I cant understand const $80000001 not $80000001.

can you told which i mistaked?

thx
0
Comment
Question by:azsd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:rbohac
ID: 10928791
If you declare your constant as the following, it will work.

const
 CMPP_CONNECT_RESP  = $80000001;
0
 
LVL 26

Assisted Solution

by:Russell Libby
Russell Libby earned 280 total points
ID: 10928804

It has to do with what Delphi can/cannot evaluate at compile time. As rbohac said, removing the cast and assigning a straight value will correct this issue.

Regards,
Russell
0
 
LVL 6

Accepted Solution

by:
rbohac earned 320 total points
ID: 10928886
I wasn't actually sure why that worked, but I knew I had run across that before
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:azsd
ID: 10928990
thx friends~~
Yes,I found most code use const with out type cast.
removed it and it worked
I dont know how i must removed it,is it means const with type cast are bad coding habit?
same DWORD const used in other code like
if i=CMPP_CONNECT_RESP works fine.
I dont know why the pascal allow me to use dword after indenfer and use in "if" but not allow me use it in case.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10929042

From the Delphi help:

A true constant is a declared identifier whose value cannot change. For example,

const MaxValue = 237;

Typed constants, unlike true constants, can hold values of array, record, procedural, and pointer types. Typed constants cannot occur in constant expressions.

-----

Also, an if statement does not require a "true" constant, but the case statement does, because a jump table is set up in assembler, and the values must be fixed.

Russell


0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10929058
The full info on typed constants:

Typed constants, unlike true constants, can hold values of array, record, procedural, and pointer types. Typed constants cannot occur in constant expressions.

In the default {$J+} compiler state, typed constants can have new values assigned to them; they behave essentially like initialized variables. But if the {$J–} compiler directive is in effect, typed constants cannot change value at runtime; they are, in effect, read-only variables.
Declare a typed constant like this:

const identifier: type = value

where identifier is any valid identifier, type is any type except files and variants, and value is an expression of type type. For example,

const Max: Integer = 100;

In most cases, value must be a constant expression; but if type is an array, record, procedural, or pointer type, special rules apply.
0
 

Author Comment

by:azsd
ID: 10929168
thx,rllibby
I have abit clear.
it my fake mistake.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question