Solved

const indenfer is not constant in case statements??

Posted on 2004-04-27
7
1,402 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 70 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 80 total points
ID: 10928886
I wasn't actually sure why that worked, but I knew I had run across that before
0
Independent Software Vendors: 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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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