Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

const indenfer is not constant in case statements??

Posted on 2004-04-27
7
Medium Priority
?
1,409 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

618 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