Validation on an interger - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Validation on an interger

[b][red]This message was edited by sarkiemarkie at 2006-2-10 14:46:27[/red][/b][hr]
[code]procedure TForm2.Edit1Exit(Sender: TObject);
{the member number is checked to see if it contains only numbers}
var
i:integer;
ok,inuse:boolean;
begin
ok := true;
inuse:=false;
for i:= 1 to length(edit1.text) do {checks the member Number for digits}
begin
if not (edit1.text[i] in Digits) then
ok := false;
end;

assignfile(memberfile,'members.dat');
reset(memberfile);
read(memberfile,memberrecord);
for i:= 1 to filesize(memberfile) do
if memberrecord.Membernumber = strtoint(edit1.text) then
inuse:=true;



if ok=false then edit1.Color:=clLime;
if inuse=true then edit1.Color:= clyellow; {checks for duplication}
if length(edit1.Text)=0 then edit1.Color:=clred {checks for presence}
else edit1.color:=clWhite;


end;[/code]

the for loops doint work. It says that the i variable is "inaccessable here due to optimization" when i put a watch on the i and thats why the for loops are not working

whats wrong?


Comments

  • zibadianzibadian Posts: 6,349Member
    : [b][red]This message was edited by sarkiemarkie at 2006-2-10 14:46:27[/red][/b][hr]
    : [code]procedure TForm2.Edit1Exit(Sender: TObject);
    : {the member number is checked to see if it contains only numbers}
    : var
    : i:integer;
    : ok,inuse:boolean;
    : begin
    : ok := true;
    : inuse:=false;
    : for i:= 1 to length(edit1.text) do {checks the member Number for digits}
    : begin
    : if not (edit1.text[i] in Digits) then
    : ok := false;
    : end;
    :
    : assignfile(memberfile,'members.dat');
    : reset(memberfile);
    : read(memberfile,memberrecord);
    : for i:= 1 to filesize(memberfile) do
    : if memberrecord.Membernumber = strtoint(edit1.text) then
    : inuse:=true;
    :
    :
    :
    : if ok=false then edit1.Color:=clLime;
    : if inuse=true then edit1.Color:= clyellow; {checks for duplication}
    : if length(edit1.Text)=0 then edit1.Color:=clred {checks for presence}
    : else edit1.color:=clWhite;
    :
    :
    : end;[/code]
    :
    : the for loops doint work. It says that the i variable is "inaccessable here due to optimization" when i put a watch on the i and thats why the for loops are not working
    :
    : whats wrong?
    :
    :
    Which line does it say that? I cannot see where that error would occur.
  • pritaeaspritaeas Posts: 274Member
    You could also use:

    var
    i: Integer;

    try
    i := StrToInt(Edit1.Text);
    except
    { Edit1.Text is not an integer }
    i := -1;
    end;

    if i <> -1 then begin
    { i is a valid number }
    end;
  • sarkiemarkiesarkiemarkie Posts: 89Member
    : : [b][red]This message was edited by sarkiemarkie at 2006-2-10 14:46:27[/red][/b][hr]
    : : [code]procedure TForm2.Edit1Exit(Sender: TObject);
    : : {the member number is checked to see if it contains only numbers}
    : : var
    : : i:integer;
    : : ok,inuse:boolean;
    : : begin
    : : ok := true;
    : : inuse:=false;
    : : for i:= 1 to length(edit1.text) do {checks the member Number for digits}
    : : begin
    : : if not (edit1.text[i] in Digits) then
    : : ok := false;
    : : end;
    : :
    : : assignfile(memberfile,'members.dat');
    : : reset(memberfile);
    : : read(memberfile,memberrecord);
    : : for i:= 1 to filesize(memberfile) do
    : : if memberrecord.Membernumber = strtoint(edit1.text) then
    : : inuse:=true;
    : :
    : :
    : :
    : : if ok=false then edit1.Color:=clLime;
    : : if inuse=true then edit1.Color:= clyellow; {checks for duplication}
    : : if length(edit1.Text)=0 then edit1.Color:=clred {checks for presence}
    : : else edit1.color:=clWhite;
    : :
    : :
    : : end;[/code]
    : :
    : : the for loops doint work. It says that the i variable is "inaccessable here due to optimization" when i put a watch on the i and thats why the for loops are not working
    : :
    : : whats wrong?
    : :
    : :
    : Which line does it say that? I cannot see where that error would occur.
    :

    it doesnt come up as an error but the for statements werent working properly so i put a watch on i and where it came to the for statements, thats the message that appear in i where an integer should have been.

  • zibadianzibadian Posts: 6,349Member
    : : : [b][red]This message was edited by sarkiemarkie at 2006-2-10 14:46:27[/red][/b][hr]
    : : : [code]procedure TForm2.Edit1Exit(Sender: TObject);
    : : : {the member number is checked to see if it contains only numbers}
    : : : var
    : : : i:integer;
    : : : ok,inuse:boolean;
    : : : begin
    : : : ok := true;
    : : : inuse:=false;
    : : : for i:= 1 to length(edit1.text) do {checks the member Number for digits}
    : : : begin
    : : : if not (edit1.text[i] in Digits) then
    : : : ok := false;
    : : : end;
    : : :
    : : : assignfile(memberfile,'members.dat');
    : : : reset(memberfile);
    : : : read(memberfile,memberrecord);
    : : : for i:= 1 to filesize(memberfile) do
    : : : if memberrecord.Membernumber = strtoint(edit1.text) then
    : : : inuse:=true;
    : : :
    : : :
    : : :
    : : : if ok=false then edit1.Color:=clLime;
    : : : if inuse=true then edit1.Color:= clyellow; {checks for duplication}
    : : : if length(edit1.Text)=0 then edit1.Color:=clred {checks for presence}
    : : : else edit1.color:=clWhite;
    : : :
    : : :
    : : : end;[/code]
    : : :
    : : : the for loops doint work. It says that the i variable is "inaccessable here due to optimization" when i put a watch on the i and thats why the for loops are not working
    : : :
    : : : whats wrong?
    : : :
    : : :
    : : Which line does it say that? I cannot see where that error would occur.
    : :
    :
    : it doesnt come up as an error but the for statements werent working properly so i put a watch on i and where it came to the for statements, thats the message that appear in i where an integer should have been.
    :
    :
    The second loop is not necessary, because during that loop the memberrecord value and the edit1.text do not change, thus the if-then will not change after the first run. Delphi optimalizer probably removed the loop, and kept the if-then statement within it.
Sign In or Register to comment.