Recent

Author Topic: Stupid behaviour of Memo1.Lines.Strings[100]  (Read 14239 times)

anna

  • Sr. Member
  • ****
  • Posts: 426
Stupid behaviour of Memo1.Lines.Strings[100]
« on: April 10, 2010, 05:15:54 pm »
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.
« Last Edit: April 10, 2010, 05:19:58 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #1 on: April 10, 2010, 05:32:30 pm »
Is Memo1.Lines.Strings[100] allocated/assigned? Does it have a value?

Update: Same behavior in Delphi.
« Last Edit: April 10, 2010, 06:16:32 pm by Troodon »
Lazarus/FPC on Linux

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #2 on: April 10, 2010, 05:34:38 pm »
SynEdit has the same behavior. ShowMessage(SynEdit.Lines[100]) returns an empty string.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #3 on: April 10, 2010, 06:15:27 pm »
Is Memo1.Lines.Strings[100] allocated/assigned? Does it have a value?

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: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  edit1.text:=Memo1.Lines.Strings[100];
end;  
. 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.
« Last Edit: April 10, 2010, 06:23:55 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #4 on: April 10, 2010, 11:10:22 pm »
I know that it is possible to check count manually, but it's stupid.
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.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2680
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #5 on: April 11, 2010, 11:12:50 am »
An edit with 99 lines of text and a CRLF and the end of the last line has in fact 100 lines. The last being empty. So getting line[100] should not cause an error in this case, but it should return an empty string and not some random garbage
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #6 on: April 12, 2010, 07:06:50 am »
An edit with 99 lines of text and a CRLF and the end of the last line has in fact 100 lines. The last being empty. So getting line[100] should not cause an error in this case, but it should return an empty string and not some random garbage

Clever explanation but in case of Memo it does not throwing an excepetion for any line number greater than 100.

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2680
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #7 on: April 12, 2010, 09:46:00 am »
err...
That looks like a bug. Can you bugrep this ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Wodzu

  • Full Member
  • ***
  • Posts: 171
« Last Edit: April 12, 2010, 10:43:17 am by Wodzu »

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #9 on: April 12, 2010, 12:29:21 pm »
With Delphi 5
ShowMessage(Memo1.Lines[Memo1.Lines.Count + 50]);

show an empty string.

Is the default behaviour ?

Regards

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #10 on: April 12, 2010, 12:37:14 pm »
In Delphi 2007 behaviour is the same. Even with "Range checking" options.
Strange...

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2680
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #11 on: April 12, 2010, 06:43:39 pm »
Rangechecking has nothing to do with the checking if list indexes
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: Stupid behaviour of Memo1.Lines.Strings[100]
« Reply #12 on: April 14, 2010, 10:30:27 am »
From my notes in bugtracker:

Quote
Delphi uses the EM_GETLINE Message to retrieve the text.
A return value of 0 indicates no chars were returned.
AFAICS there is no way to determine if this is because Index is out of range, or because the line is empty (at least not for a memo control).
(See: http://msdn.microsoft.com/en-us/library/bb761584(VS.85).aspx)

> And the more important, is it the right behaviour?

From: http://docwiki.embarcadero.com/VCL/en/Classes.TStrings.Strings
> Descendants of TStrings must implement an accessor function for the Strings
> property to return the string at the position indicated by Index. Index gives
> the position of the string, where 0 is the first string, 1 is the second
> string, and so on.

By this definition the behaviour when Index is out of bounds is undefined.

Bart

 

TinyPortal © 2005-2018