Solved

const indenfer is not constant in case statements??

Posted on 2004-04-27
7
1,386 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now