Article View: borland.public.delphi.language.objectpascal
Article #32473Re: Using constants & in
From: Rudy Velthuis (TeamB)
Date: Fri, 22 Nov 2002 13:02
Date: Fri, 22 Nov 2002 13:02
58 lines
2694 bytes
2694 bytes
In article <3dde225f$1@newsgroups.borland.com>, Stephen Froude says...
>
> >Are you sure that qryStatus.FieldByName('oldstatus').AsInteger return a
> >value between 10 and 1000, where 10 and 1000 in included?
>
> Thanks for your reply. Yes, I'm getting the value 370 in my testing.
>
> I've just created a new app with the following code to make sure, any ideas?
>
> procedure TForm1.Button1Click(Sender: TObject);
> const
> STATUS_NEW: Integer = 100;
> STATUS_CANCELLED: Integer = 1000;
> var
> iStatus: Integer;
> begin
> iStatus := 370;
> if (iStatus in [STATUS_NEW..STATUS_CANCELLED]) then
> ShowMessage('Hello');
> end;
Do you have range checking on? If you do (you should *always* do that,
even in deployed code), you'll get a range check error, and rightly so.
Why? Because [100..1000] would be a set, and a set can only contain
values 0..255. So without range checking, you probably get [100..1000
mod 256], or [100..232].
Because your values are not constant expressions, like [100..1000], you
won't get a compile time error. That is because your constants are not
true constants, they are typed constants.
Try this instead:
procedure TForm1.Button1Click(Sender: TObject);
const
STATUS_NEW = 100;
STATUS_CANCELLED = 1000;
var
iStatus: Integer;
begin
iStatus := 370;
if (iStatus in [STATUS_NEW..STATUS_CANCELLED]) then
ShowMessage('Hello');
end;
And you'll see it won't compile:
[Error] InOperatorTest.pas(34): Constant expression violates subrange
bounds
Line 34 is the if clause.
--
Rudy Velthuis (TeamB)
"We should leave our minds open, but not so open that our brains
fall out." - Alan Ross Anderson
Technical Details
Message-ID:
<3dde2ace$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-04!supernews.com!news.maxwell.syr.edu!newsfeed1.cidera.com!Cidera!news.uni-x.net!newsgroups.borland.com!not-for-mailReferences:
<3dde1a3b$1@newsgroups.borland.com> <3dde1f33@newsgroups.borland.com> <3dde225f$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109d30,1c6f6604b711dd81 X-Google-Attributes: gid109d30,public X-Google-ArrivalTime: 2002-11-22 05:17:23 PST From: "Rudy Velthuis (TeamB)" <rvelthuis@gmx.de> Newsgroups: borland.public.delphi.language.objectpascal Subject: Re: Using constants & in Date: Fri, 22 Nov 2002 14:02:36 +0100 References: <3dde1a3b$1@newsgroups.borland.com> <3dde1f33@newsgroups.borland.com> <3dde225f$1@newsgroups.borland.com> Organization: TeamB X-Newsreader: MicroPlanet Gravity v2.60 NNTP-Posting-Host: 80.135.214.253 Message-ID: <3dde2ace$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1037970126 80.135.214.253 (22 Nov 2002 05:02:06 -0800) Lines: 58 X-Authenticated-User: rvelthuis Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-04!supernews.com!news.maxwell.syr.edu!newsfeed1.cidera.com!Cidera!news.uni-x.net!newsgroups.borland.com!not-for-mail