Solved

const indenfer is not constant in case statements??

Posted on 2004-04-27
7
1,398 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process Javascript errors with Delphi TWebBrowser 1 132
Delphi - replicating a form 8 74
delphi parse string to params 3 122
Tidtcpserver listening on multiports? 1 27
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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