Solved

TMemoryStream basics

Posted on 1998-10-27
17
190 Views
Last Modified: 2011-04-14
Hi all !

Why this piece of code doesn´t work:

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  ListBox2.Items.SaveToStream(Stream);
  ListBox1.Items.LoadFromStream(Stream);
  Stream.Free;
  //Please ! Don´t tell me I could make ListBox1.Items := ListBox2.Items
end;

As I never used TMemoryStream, I guess I am really missing smth...

Bye,
Itamar
0
Comment
Question by:itamar
  • 6
  • 5
  • 5
  • +1
17 Comments
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
Nope, ListBox1.Items := ListBox2.Items is wrong as you say, though you can do ListBox1.Items.assign(ListBox2.Items).

Apart from perhaps setting the sizxe of the TMemoryStream I am not sure what might be causing that to fail (perhaps you could tell us what does go wrong...)

Cheers,

Raymond.
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
Hi,
Raymond's solution is a very good one.
The reason your code doesn't work is that when you load from the stream the stream's data offset is set to the end of it's data, since that's where it goes after saving to the stream. That means you are trying to read data from the end of the stream. And there is exactly zero bytes available in that position, at that time.
The solution, using your code, would be:

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  ListBox2.Items.SaveToStream( Stream );
  Stream.Position := 0; // << HERE'S WHERE YOU RESET THE STREAM OFFSET
  ListBox1.Items.LoadFromStream( Stream );
  Stream.Free;
end;

This code, however, is the exact equivalent of:
  ListBox1.Items.Text := ListBox2.Items.Text;

Otherwise, the streams are excellent. I use them very frequently, but that, of course, is a totally other history...

/// John
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
Yes, John is absolutely right...
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
Always...! ;-)

/// John
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
Well - I guess I'd better submit this as the answer...

Nope, ListBox1.Items := ListBox2.Items is wrong as you say, though you can do
       ListBox1.Items.assign(ListBox2.Items).

erajoj pointed out the problem with not setting position so if that was the better answer reject this one and erjoj will submit it as the answer.

       Cheers,

       Raymond.
0
 
LVL 4

Author Comment

by:itamar
Comment Utility
Hi all,

In fact this code is just a dummy to show an example of my doubt.
What I am trying to do is to transfer the contents of a TStringList to the lines of a combo.
I thought the best way was using streams, but I didn´t know about Stream.Position.

So, erajoj (John) solved my problem. Post your comment as an answer, so I can grade it.

BTW, there is a better way to transfer a TStringList contents to a Combo ?

Thanks to all !

Itamar
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
Hi Itamar,
I still think Raymonds answer is the best. It gives you the least overhead.

The best ways to move strings from one TStringList/TStrings to another:

  List1.BeginUpdate;         { prevent repaints until done }
  List1.Clear;               { empty the list of any old values }
  List1.AddStrings( List2 ); { add all strings from list2 }
  List1.EndUpdate;           { reenable painting }

  List1.BeginUpdate;
  List1.Assign( List2 ); { add all strings from list2 }
  List1.EndUpdate;

Raymond, re-answer the question, and Itamar, give the man back his rightfully earned points.

/// John

0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
Here we go again! :-)
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Author Comment

by:itamar
Comment Utility
Hey people,

I think I´m having problems to make me being understood !!

I need to transfer the contents of a TStringList to a TStrings !!!

When I found out TStream I couldn´t use it because my code was missing the Position := 0;

My example is just a DUMMY code !!!!!

I think I have to make an English course ;(((
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
Hi Itamar,
You cannot transfer anything to a TStrings object. Only through it, since is not a container but an interface.
What is the problem? Explain further! Send the code you're working on.

/// John
0
 
LVL 4

Author Comment

by:itamar
Comment Utility
I will try to explain:

1. Create a TStringList instance

2. Fill this TStringList with some strings

3. Now I need to show those strings in a Memo or ListView

How to ?
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
Hi,
the memo is easy, the listview less easy:

var
  MyList: TStringList;
  iIndex: Integer;
begin
  MyList := TStringList.Create;
  with MyList do
  begin
    Add( 'string 1' );
    Add( 'string 2' );
    Add( 'string 3' );
  end;
  MyMemo.Lines.Assign( MyList ); // load to memo
  with MyListView.Items do // load to listview
  begin
    Clear;
    for iIndex := 0 to MyList.Count - 1 do
      Add.Caption := MyList[ iIndex ];
  end;
  MyList.Free;
end;

This code is not tested, so there might be some typos.

/// John
0
 
LVL 4

Author Comment

by:itamar
Comment Utility
Hi John,

the Assign method is the answer to my question.

Post your comment as a question, so I can grade it.

Thanks,
Itamar
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
So which one of us do you want to answer it. We both said use assign...

Cheers,

Raymond.
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
How many times do I have to say this; the points are yours, Raymond! :-)
My latest comment was just a note on your answer, since Itamar didn't seem to
understand the memo/listbox architecture. Not a correction.

Itamar, give the points to Raymond, I don't seem to get my Tshirts anyway. ;-(

///  John
0
 
LVL 4

Author Comment

by:itamar
Comment Utility
Ok !

Raymond, be my guest !
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 50 total points
Comment Utility
Here we go...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now