Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

TMemoryStream basics

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
itamar
Asked:
itamar
  • 6
  • 5
  • 5
  • +1
1 Solution
 
rwilson032697Commented:
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
 
erajojCommented:
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
 
MadshiCommented:
Yes, John is absolutely right...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
erajojCommented:
Always...! ;-)

/// John
0
 
rwilson032697Commented:
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
 
itamarAuthor Commented:
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
 
erajojCommented:
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
 
rwilson032697Commented:
Here we go again! :-)
0
 
itamarAuthor Commented:
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
 
erajojCommented:
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
 
itamarAuthor Commented:
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
 
erajojCommented:
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
 
itamarAuthor Commented:
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
 
rwilson032697Commented:
So which one of us do you want to answer it. We both said use assign...

Cheers,

Raymond.
0
 
erajojCommented:
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
 
itamarAuthor Commented:
Ok !

Raymond, be my guest !
0
 
rwilson032697Commented:
Here we go...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now