Solved

TMemoryStream basics

Posted on 1998-10-27
17
198 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
ID: 1344623
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
ID: 1344624
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
ID: 1344625
Yes, John is absolutely right...
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:erajoj
ID: 1344626
Always...! ;-)

/// John
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1344627
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
ID: 1344628
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
ID: 1344629
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
ID: 1344630
Here we go again! :-)
0
 
LVL 4

Author Comment

by:itamar
ID: 1344631
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
ID: 1344632
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
ID: 1344633
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
ID: 1344634
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
ID: 1344635
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
ID: 1344636
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
ID: 1344637
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
ID: 1344638
Ok !

Raymond, be my guest !
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 50 total points
ID: 1344639
Here we go...
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 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