Jump to content
Sign in to follow this  
Czar

memo lines ?

Recommended Posts

Hmmm, I am missing something.

 

Lines does not exist for memo (TW3Memo)

Syntax Error: There is no accessible member with name "lines" [line: 46, column: 23, file: Form1]

 

procedure TForm1.InitializeForm;
var item : string;
begin
  inherited;
  // this is a good place to initialize components

  W3Memo1.Add('one');
  W3Memo1.Add('one1');
  W3Memo1.Add('one2');
  W3Memo1.Add('one3');

  for item in w3memo1.lines do
    begin
      W3Memo2.add(item);
    end;
end;

 

Share this post


Link to post
Share on other sites
16 hours ago, Czar said:

I believe it is a good example where Smart Pascal should mirror Delphi

Well, introducing Lines as a property is not hard if we do it by splitting the memo text. However, when you'd loop through it, it'd be horribly inefficient as every access to Lines (count or accessing any line), would trigger the split from original text.

Share this post


Link to post
Share on other sites

interesting. Basically you would need to keep Text(string) and Lines(array of string) in sync at all times, and only sync when data changes in either one of them

I suppose you could define a Lines array, and override the js push method (for this array only)

Lines = []
Lines.push = function(data) {
  ... update Memo.text with data ....

  return Array.prototype.push.call(this, data);
}

and in this override update Memo.Text (or re-create Text from Lines). That would cover the use case of Memo1.Lines.Add(data) and I suppose similarly for deletes.

The other way around (changing Text instead of Lines) you would need to extend the SetText setter to update Lines (and not get into an infinite loop) 

Looking forward to see your solution :)

 

 

Share this post


Link to post
Share on other sites
7 hours ago, Czar said:

Sorry both the previous posts go over my head. I am a humble user and abuser of Smart Mobile Studio. 

Ok, lets elaborate. This is pretty universal regardless of programming languages.

Suppose I just add:

property Lines: TStrArray read GetLines;

...and GetLines does take the current text and splits it into an array.

Then you have code like this:

for var i:=0 to Memo.Lines.Count-1 do
  WriteLn(Memo.Lines[i]);

When the code is run, every time Memo.Lines is accessed, it results in a call to GetLines. So, if you have 1000 lines, the code above would result in 1001 calls to GetLines and 1001 splits of the whole text to an array. This is what my message meant.

What @lynkfswrote is about a more advanced solution where the memo would store the contents as both text (as it does now) and a separate Lines-array. That would eliminate the performance problem, but it'd introduce other problems like:

  • More memory usage as data is stored internally twice.
  • All the challenges to make sure that both the text and the array match, no matter how the memo contents are changed.

I haven't tested if @lynkfs solution works and what it requires.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×