Recent

Author Topic: tmemorystream: no member set size / get memory  (Read 372 times)

Selfmade.exe

  • New Member
  • *
  • Posts: 36
tmemorystream: no member set size / get memory
« on: August 20, 2019, 02:57:05 am »
Hello everyone,

Data:= TMemoryStream.Create; // this is ok
Data.SetSize( 8 ); // this is NOT ok
s:= data.memory; // this is NOT ok


I get  Error: identifier idents no member "SetSize" & Error: identifier idents no member "memory"
« Last Edit: August 20, 2019, 03:00:00 am by Selfmade.exe »

Selfmade.exe

  • New Member
  • *
  • Posts: 36
Re: tmemorystream: no member set size / get memory
« Reply #1 on: August 20, 2019, 03:10:35 am »
Never mind fellas, I found it, my bad.

It was

var Data: TStream;
and
Data:=TMemoryStream.Create

The correct is

var Data: TMemoryStream;
and
Data:=TMemoryStream.Create

Thaddy

  • Hero Member
  • *****
  • Posts: 8664
Re: tmemorystream: no member set size / get memory
« Reply #2 on: August 20, 2019, 07:51:12 am »
For completeness, you were on the right track.
It is OK to declare as Tstream, but then you need to use it like this:
Code: Pascal  [Select]
  1. {$mode objfpc}
  2. uses classes;
  3. // access to memorystream specific methods and properties from TStream;
  4. var
  5.   data:Tstream;
  6. begin
  7.   data := TmemoryStream.Create;
  8.   // either soft cast
  9.   (data as TmemoryStream).SetSize(8);
  10.   // or hard cast
  11.   TMemoryStream(Data).SetSize(8);
  12.   Data.free;
  13. end.

That way you can still assign the data stream to any other Tstream descendant.
Methods and properties that are shared with the ancestor class (read/write) do not need a cast.
« Last Edit: August 20, 2019, 08:03:12 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

Selfmade.exe

  • New Member
  • *
  • Posts: 36
Re: tmemorystream: no member set size / get memory
« Reply #3 on: August 20, 2019, 08:42:40 pm »
Nice one @Thaddy! I just learned something new from your post! I hope that this thread gets the attention of anyone that might be in a similar situation.
« Last Edit: August 20, 2019, 09:49:45 pm by Selfmade.exe »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 651
    • Lebeau Software
Re: tmemorystream: no member set size / get memory
« Reply #4 on: August 22, 2019, 12:14:36 am »
Code: Pascal  [Select]
  1. // either soft cast
  2. (data as TmemoryStream).SetSize(8);
  3. // or hard cast
  4. TMemoryStream(Data).SetSize(8);

You don't need a cast at all.  Just use the public TStream.Size property instead:

Code: Pascal  [Select]
  1. Data.Size := 8;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 8664
Re: tmemorystream: no member set size / get memory
« Reply #5 on: August 22, 2019, 08:22:56 am »
[You don't need a cast at all.  Just use the public TStream.Size property instead:
Was merely meant as example in case it is a a member of TMemoryStream as opposed to TStream.
Furthermore, not every stream has a size. https://www.freepascal.org/docs-html/rtl/classes/tstream.size.html
(As such it is actually a theoretically flawed implementation, but it is is Delphi legacy)
« Last Edit: August 22, 2019, 08:25:57 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.