Forum > General

Stupid behaviour of Memo1.Lines.Strings[100]

(1/3) > >>

anna:
Why access to Memo1.Lines.Strings[100] doesn't  occur error if  Memo1 has less than 100 lines?
For example TStrings variable in same conditions occurs error 'Bla-bla out of bounds'.
I need error. I know that it is possible to check count manually, but it's stupid.

Troodon:
Is Memo1.Lines.Strings[100] allocated/assigned? Does it have a value?

Update: Same behavior in Delphi.

typo:
SynEdit has the same behavior. ShowMessage(SynEdit.Lines[100]) returns an empty string.

anna:

--- Quote from: Troodon on April 10, 2010, 05:32:30 pm ---Is Memo1.Lines.Strings[100] allocated/assigned? Does it have a value?

--- End quote ---

Memo1 has 1 to 99 lines. I've put  Memo1 on form. By default it has one string 'Memo1'.
So then for example put edit1 & button1 & write for Button1Click

--- Code: ---procedure TForm1.Button1Click(Sender: TObject);
begin
  edit1.text:=Memo1.Lines.Strings[100];
end;  
--- End code ---
. If click button1 , i cannot see error. Nay, sometimes Edit1 gets abra-kadabra.  Edit1 must get either empty string or error message, but abra-kadabra from non-allocated memory is bad.

eny:

--- Quote from: anna on April 10, 2010, 05:15:54 pm ---I know that it is possible to check count manually, but it's stupid.

--- End quote ---
As a general rule one shouldn't access a list outside it's bounds.
Therefore a check for the line count is a good thing.
As for the number 100, we shouldn't use magic numbers anyway.

I do however agree with the fact that it's weird that no exception is thrown.

Navigation

[0] Message Index

[#] Next page

Go to full version