2
votes

I'm converting an old code from Delphi 5 to XE5. It has this piece of code:

Boolean(RecBuf[0]) := False;

RecBuf is PChar. This works in Delphi 7, but not in XE5. In XE5, it gives "Left side cannot assign" error. How to implement this code in XE5?

3

3 Answers

13
votes

In Delphi 7, PChar is an alias for PAnsiChar. That is, pointer to 8 bit ANSI character. Which means that RecBuf[0] has type AnsiChar. Since AnsiChar and Boolean have the same size, the cast is valid.

In Delphi XE5, PChar is an alias for PWideChar, pointer to 16 bit wide character. And so RecBuf[0] has type WideChar. Thus the cast is invalid because WideChar and Boolean have different size.

Exactly how best to fix the problem cannot be discerned from the code that you have shown here. Quite possibly you need to redeclare RecBuf. Perhaps it needs to be declared as PAnsiChar. Although one does wonder why you are casting a character to a Boolean.

Another possibility is that the reason RecBuf is declared as PChar is to allow you to use the index operator [], something that in older versions of Delphi is a special capability of pointers to character types. In modern Delphi you can use {$POINTERMATH ON} to enable that functionality for all typed pointers. So, if you did that then perhaps RecBuf should be PBoolean.

The bottom line is that whilst we can explain why the compiler complains about your code, we cannot give you a definitive solution.

-1
votes

Judging from RecBuf name they are used not as a pointer to string but as a pointer to buffer allocated in memory.

If my assumption correct, you may want to redeclare RecBuf as a variable of type PByte.

-1
votes

You can't assign cast stuff on the left, you need to cast the right side.

Instead of

Newtype(Whatever) := NewtypeConstant;

you should use

Whatever := TUnknown(constant)

In yourcase, of recbuf is an array of say, byte, then it puts false (0) in it, so it should be

Recbuf[0] := Byte(False);

Or you could, you know, just put zero in there.