Solved

TMemoryStream basics

Posted on 1998-10-27
17
195 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

21 Experts available now in Live!

Get 1:1 Help Now