🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.delphi.language.objectpascal
Article #32470

Re: Using constants & in

#32470
From: Rudy Velthuis (TeamB)
Date: Fri, 22 Nov 2002 13:07
42 lines
2277 bytes
In article <3dde25a1$1@newsgroups.borland.com>, Stephen Froude says...

> This is strange.  If I run the following code, the for loop breaks on 233.  
> Why does this happen???  I'm using Delphi 6 Ent
 
> procedure TForm1.Button1Click(Sender: TObject);
> const
>   STATUS_NEW: Integer = 100;
>   STATUS_CANCELLED: Integer = 1000;
> var
>   iLoop, iStatus: Integer;
> begin
>   for iLoop:= STATUS_NEW to STATUS_CANCELLED do
>   begin
>     iStatus := iLoop;
>     if not (iStatus in [STATUS_NEW..STATUS_CANCELLED]) then
>     begin
>       ShowMessage('Failed on ' + IntToStr(iStatus));
>       Break;
>     end;
>   end;
> end;

Because sets like [STATUS_NEW..STATUS_CANCELLED] can only contain values 
0..255. If you had range checking on, you would get a range check error. 
Now you simply get a cut off version, i.e. only the lower byte of 1000, 
which is 232.

Reminder: ALWAYS test (and IMHO deploy as well) with Range and Overflow 
Checking on! This will catch a lot of otherwise weird errors in your 
code.

"In" is only useful for values in the range 0..255. Otherwise you should 
use:

  if (iStatus >= STATUS_NEW) and (iStatus <= STATUS_CANCELLED) then

-- 
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: <3dde2bea@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-mail
References: <3dde1a3b$1@newsgroups.borland.com> <3dde1f33@newsgroups.borland.com> <3dde25a1$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:07:19 +0100
References: <3dde1a3b$1@newsgroups.borland.com> <3dde1f33@newsgroups.borland.com> <3dde25a1$1@newsgroups.borland.com>
Organization: TeamB
X-Newsreader: MicroPlanet Gravity v2.60
NNTP-Posting-Host: 80.135.214.253
Message-ID: <3dde2bea@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1037970410 80.135.214.253 (22 Nov 2002 05:06:50 -0800)
Lines: 42
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