Re: Trying to limit input ot certain numbers
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Fri, 20 Mar 2009 23:23:43 -0500
TD wrote:
This piece of code does not seem to be working. Example, when the
first digit of "t" is 5 the message "wrong first char" never shows.
What am I missong here? Can someone explain to me what the "In
[0..2]" part is and were in the help files I can learn about it.
t : string;
t := '5422';
if NOT ((StrToInt(MidStr(t,1,1)))) IN [0..2] then
ShowMessage('wrong first char');
Exit;
The "not" operator binds more tightly than the "in" operator, so you're inverting the bits of the integer and then checking whether that inverted value is in the set. Of course it's not, so you don't see the message.
Also, that you're using MidStr suggests that you used to program in VB. I'm terribly sorry. In Delphi, characters are different from strings. If you want one character from a string, then use the array-access notation to extract just that one character:
t[1]
Calling MidStr gives you a new string that happens to have just one character in it.
You're allowed to have sets of AnsiChars as well as sets of integers.
Here's what you should have instead:
if not (t[1] in ['0'..'2']) then
The parentheses are necessary to get the "in" result before the logical negation of the "not" operator.
The "in [0..2]" part in your code is check for set membership. You should be able to find information about that in the help using the keywords "in" or "set."
One way that you could have gone about investigating what was wrong with your code was to break up the complex expression into smaller operations. For example, you could call MidStr separately from the call to StrToInt. That way, you could inspect the intermediate value to make sure you were testing the character you expected:
var
c: string;
c := MidStr(t, 1, 1);
if not ((StrToInt(c))) in [0..2] then
Then, you could take out the StrToInt call and store it in an intermediate value, too:
var
v: Integer;
v := StrToInt(c);
if not ((v)) in [0..2] then
Doing that could also make it more obvious that your parentheses weren't really doing anything. Parentheses can make code easier to understand, but too many make code harder to understand.
Finally, you could break the "in" operation into a separate operation. I assume *you* knew that the "in" operation needed to be evaluated before the "not" operation, right?
var
b: Boolean;
b := v in [0..2];
if not b then
Once you did that, your code should have worked as you expected. At that point, you would have isolated the single change necessary to make your code work or not work, and then you would have asked a much more specific question about why your code works when you do the "in" operation separately from "not," but not when you do them both in the same expression.
The compiler interprets your code as though you had split it like so:
var
w: Integer;
w := not v;
if w in [0..2] then
--
Rob
.
- References:
- Trying to limit input ot certain numbers
- From: TD
- Re: Trying to limit input ot certain numbers
- From: Bart
- Re: Trying to limit input ot certain numbers
- From: alanglloyd@xxxxxxx
- Re: Trying to limit input ot certain numbers
- From: TD
- Re: Trying to limit input ot certain numbers
- From: TD
- Trying to limit input ot certain numbers
- Prev by Date: Re: Trying to limit input ot certain numbers
- Next by Date: Re: Why does Delphi Skip This Line?
- Previous by thread: Re: Trying to limit input ot certain numbers
- Next by thread: Delphi editor bugs affecting my coding style.
- Index(es):
Relevant Pages
|