Re: Length of a Delphi record

[ DelphiLand FAQ ]

Posted by webmaster Guido on June 12, 2004 at 16:45:51:

In Reply to: Length of a Delphi record posted by Joey on May 29, 2004 at 15:47:32:

: what i need to know is... how can i find the length of a custom type i have set up in Delphi?

: I am wanting to send information via TWSocket and want to send them the data in a record, TMsg which has a Name, Msg var as strings and a Color var as TColor.

: //Code for it
: TMsg = record
: Name, Msg: String;
: Color: TColor;
: end;

: But the function Send in the TWSocket asks for Data as a pointer and Lenght as Integer

: //Syntax
: function Send(Data: Pointer; Length: Integer): Integer;

: so what i did was make a var Msg as TMsg and set the value and used a @ to turn the TMsg into a pointer but.. how do i calculate the length of the TMsg record??

The size of a TColor is 4 bytes, that's easy.

But string variables can be any length in 32bits Delphi versions. In Delphi 1 however, this was 256 bytes by default and it could be set to a length from 1 to 255. This old type of string still exists in 32bits Delphi as "short string".

So, instead of using string variables, you could use "short strings". Their size is the length that you specify plus one byte. Example:

 TMsg = record
   Name: string[32]; // 33 bytes
   Msg: string[255]; // 256 bytes
   Color: TColor;    // 4 bytes

Total size of the record: 296 bytes.


Related Articles and Replies:

[ DelphiLand FAQ ]