Follow Up: Indy | Records, and Typecasts

Hello there,
This Q is a Follow up of http:Q_21063476.html

I tried to add a field to the TLoginSizes and as soon as I did it the compiler stopped at "TLoginSizes(mr.DataLen).UsernameLen:=Length(Edit1.Text);" with the message "invalid typecast"

  TLoginSizes = record
    UsernameLen: Word;
    PasswordLen: Word;
    HexArraylen: Word;     //    New field...

  TMsgRec = record
    RecType: Byte;
    Command: Byte;
    DataLen: Cardinal;
Who is Participating?
Wim ten BrinkConnect With a Mentor Self-employed developerCommented:
Use packed records...

TLoginSizes and TMsgRec might appear to be only 6 bytes byt don't have to be, just because the way Delphi aligns these fields. It's not a very safe typecast anyway and will be invalid in future Delphi versions so you better not use it. PErhaps a variant record is a better option. Example:

  TYourRecord = record
    case boolean of
      False: (RecType: Byte; Command: Byte; DataLen: Cardinal);
      True: (UsernameLen: Word; PasswordLen: Word; HexArraylen: Word);     //    New field...    

Or something similar. I don't have Delphi available right now so can't check...

If you're using global variables, you might also think of using the 'absolute' keyword:

  LoginSizes: TLoginSizes;
  MsgRec: TMsgRec absolute LoginSizes;

As a final solution, consider using pointers.
CerfAuthor Commented:
This Q has turned into "extremely urgent" for me... Raising to 500p
CerfAuthor Commented:
I see what you wrote previously,

>"it's a kindof an optimization .. I only send one record and the data once with an existing record type
DataLen is a cardinal field - 4 bytes
that TLoginSizes record has two word fields - 2x 2bytes
so the datasize is the same and using that typecast it can represent various things
if you take a look at TRect you'll see a similar thing"

and I guess it is about  the size of cardinal and 3 word fields = 6bytes but I don't really understand what are you doing there...
CerfAuthor Commented:
Thanks Alex!

I already fixed it changing a bit the comm protocol between the server and the clients to a simplier thing. As a friend told me...
"KISS" keep it simple, stupid :) so I did and works fine.
I only send the header record (TMsgRec) which is fixed size for sure and don't do any typecasting... the rest is interpreted and manipulated by the server. However, your post makes sense.

Thanks for interesting,


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.