Recent

Author Topic: TObject(TColor) = Illegal type conversion ?  (Read 213 times)

Aamadeus

  • Newbie
  • Posts: 2
TObject(TColor) = Illegal type conversion ?
« on: August 21, 2019, 10:10:54 pm »
I update my Lazarus from 1.64 to 2.04 and now when I loaded this project I worked on I get conversion errors.

LBLog.Items.AddObject(PText,TObject(PColor));

I did this and then

procedure TFormLog.LBLogDrawItem(Control: TWinControl; Index: Integer;
  ARect: TRect; State: TOwnerDrawState);
begin
    with Control as TListBox do
begin
  Canvas.FillRect(ARect);
  Canvas.Font.Color := TColor(Items.Objects[Index]);
  Canvas.TextOut(ARect.Left + 2, ARect.Top, Items[Index]);
end;
end;

To have a Listbox with differently colored lines (Which serves as a Log in my program, Red = Error etc.).
I guess it's some compatibility thing maybe ?
But why shouldn't I be able to add a color as an object when basically everything is on object, right ?
What's going on.
Also it worked perfectly before but I'm not sure if I had some IDE options that I don't have now because it's been ages since I set up 1.64 and i never touched anything since besides loading some packages.

Thank you very much for your time, Amadeus.

Abelisto

  • Jr. Member
  • **
  • Posts: 81
Re: TObject(TColor) = Illegal type conversion ?
« Reply #1 on: August 21, 2019, 10:27:38 pm »
Actually TColor is not an object but pure 4-bytes integer (RGBA):

Code: Pascal  [Select]
  1. TColor = TGraphicsColor;
  2. ...
  3. TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Thaddy

  • Hero Member
  • *****
  • Posts: 8685
Re: TObject(TColor) = Illegal type conversion ?
« Reply #2 on: August 21, 2019, 10:36:18 pm »
But why shouldn't I be able to add a color as an object when basically everything is on object, right ?
It has always been wrong to hard-cast , because it is NOT an object. Anyway: Canvas.Font.Color := PColor(@Items.Objects[Index])^; should do the illegal trick.
You were lucky it worked in the first place.

Note: I did not test this, but should be OK.
« Last Edit: August 21, 2019, 10:38:33 pm by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

wp

  • Hero Member
  • *****
  • Posts: 6158
Re: TObject(TColor) = Illegal type conversion ?
« Reply #3 on: August 21, 2019, 10:40:31 pm »
You probably installed the 64-bit version of Lazarus now. It is important to know that in the transition from 32 to 64 bit the "integer" data type remains 32 bit, while the "pointer" increases to 64 bit. TColor is an integer, TObject a pointer - you cannot directly type-cast them in 64 bit any more because half of the pointer will be lost when cast to an integer.

As a solution you should use the integer type PtrInt (signed) or PtrUInt (unsigned) - this type always has the same size as a pointer.

Code: Text  [Select]
  1. LBLog.Items.AddObject(PText,TObject(PtrUInt(PColor)));
  2. anvas.Font.Color := TColor(PtrUInt(Items.Objects[Index]));
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jamie

  • Hero Member
  • *****
  • Posts: 1924
Re: TObject(TColor) = Illegal type conversion ?
« Reply #4 on: August 21, 2019, 10:43:56 pm »
works for me..
Code: Pascal  [Select]
  1.  ListBox1.Items.AddObject('Test', TObject(ClWhite));
  2.  


Akira1364

  • Hero Member
  • *****
  • Posts: 523
Re: TObject(TColor) = Illegal type conversion ?
« Reply #5 on: August 21, 2019, 11:02:40 pm »
works for me..
Code: Pascal  [Select]
  1.  ListBox1.Items.AddObject('Test', TObject(ClWhite));
  2.  

You're probably using 32-bit.

In general though this kind of obviously-wrong typecasting is just asking for trouble IMO...

basically everything is on object, right ?

Well, no, not at all. It only works by chance, basically, in situations where the numeric type being casted to is the same size as a pointer, as WP said. (Because TObject is a class, and thus ultimately a pointer.)
« Last Edit: August 21, 2019, 11:05:27 pm by Akira1364 »

Aamadeus

  • Newbie
  • Posts: 2
Re: TObject(TColor) = Illegal type conversion ?
« Reply #6 on: August 21, 2019, 11:13:58 pm »
Hey ! Yes the change to 64bit must have been it. PtrUInt works perfectly.
Pointers is something I never really understood. I really should read up on those.
Thanks so much for your answers, I really appreciate it :)
Wish you all a great week.

jamie

  • Hero Member
  • *****
  • Posts: 1924
Re: TObject(TColor) = Illegal type conversion ?
« Reply #7 on: August 21, 2019, 11:23:01 pm »
Sorry about that, I tend to stay in the 32 bit world while DEV because I do a lot of things that really does bring the OS down when generating 64 bits, like Threading using waits working on serial ports. I get a nice blue screen when things go wrong there but recovers fine using the 32 bit tools.

 When I think I have all the bugs out then I do a target for 64 bit and give it a spin on a different PC.