Recent

Author Topic: [Solved] Is it possible to make "over-typecasting"?  (Read 601 times)

devEric69

  • Full Member
  • ***
  • Posts: 121
[Solved] Is it possible to make "over-typecasting"?
« on: June 18, 2019, 05:31:19 pm »
Hello,

In Delphi, I was doing "over-typecasting" like this example....:

Code: Pascal  [Select]
  1. TDrawGrid(grdMyStringGrid).ColWidths[1]:= 650;

... because the property TStringGrid.ColWidths was protected - therefore impossible to modify - while the property TCustomDrawGrid.ColWidths (from which the TDrawGrid inherits) was published. Hence the "over-typecasting".

The FPC compiler won't let me do this "trick": it tells me "TDBGrid and TDrawGrid are not related", which is true since they are on separate branches with a fork between the two (like their Delphi counterparts).

Does anyone know if there is a compiler option, or better, something more encompassing like a compilation directive framing this line \ "trick", to bypass this constraint?
« Last Edit: July 15, 2019, 07:24:43 pm by devEric69 »
use: Ubuntu 18.04 + Laz. 1.8.5 + FPC 3.0.5 (64 bits).

Thaddy

  • Hero Member
  • *****
  • Posts: 8679
Re: Is it possible to make "over-typecasting"?
« Reply #1 on: June 18, 2019, 05:34:20 pm »
This is possible in fpc too, but in this case the ancestors differ, so that's a problem.
Valid casting can only be done on a strict hierarchy.
I would consider this a design bug, you should report it.
« Last Edit: June 18, 2019, 05:36:48 pm by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7356
Re: Is it possible to make "over-typecasting"?
« Reply #2 on: June 18, 2019, 05:39:35 pm »
Compile in delphi mode ? But that it works in Delphi is sheer luck, fat chance that it only delivers a nice GPF.

devEric69

  • Full Member
  • ***
  • Posts: 121
Re: Is it possible to make "over-typecasting"?
« Reply #3 on: June 18, 2019, 05:52:17 pm »
All right, good news.

Quote
Compile in delphi mode ? But that it works in Delphi is sheer luck, fat chance that it only delivers a nice GPF.

In fact, I just double-checked and I was wrong: in the Delphi VCL hierarchy, TStringGrid inherits directly from TDrawGrid (no fork in between), hence this possible trick.
In the LCL, TStringGrid and TDrawGrid are on two distinct branches, with TCustomDrawGrid making the fork.
« Last Edit: June 18, 2019, 05:57:51 pm by devEric69 »
use: Ubuntu 18.04 + Laz. 1.8.5 + FPC 3.0.5 (64 bits).

wp

  • Hero Member
  • *****
  • Posts: 6149
Re: Is it possible to make "over-typecasting"?
« Reply #4 on: June 18, 2019, 05:56:07 pm »
Code: Pascal  [Select]
  1. TDrawGrid(grdMyStringGrid).ColWidths[1]:= 650;

... because the property TStringGrid.ColWidths was protected - therefore impossible to modify - while the property TCustomDrawGrid.ColWidths (from which the TDrawGrid inherits) was published. Hence the "over-typecasting".
[...]
"TDBGrid and TDrawGrid are not related"
This desciption is a bit confusing... TStringGrid, TDrawGrid, TDBGrid? Which type is grdMyStringGrid? You probably only did not do the type-cast correctly. "ColWidths" is introduced as protected by the most elemental grid, TCustomGrid, from which all Lazarus grids are derived.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

devEric69

  • Full Member
  • ***
  • Posts: 121
Re: Is it possible to make "over-typecasting"?
« Reply #5 on: June 18, 2019, 06:02:52 pm »
I'm sorry.
Indeed, I was wrong in my example which leads to confusion (I am the first one).

grdMyStringGrid (my example uses a very bad "name") is, as the compiler tells me, a TDBGrid (which, in the VCL, is not at all on the same branch as TDrawGrid, TCustomGrid branching between the two in the VCL. But with Delphi "TDrawGrid(myDBGrid).Colwidths" compiles).
Anyway, knowing what is possible or not now, I will do it differently with Lazarus.
« Last Edit: June 18, 2019, 06:28:12 pm by devEric69 »
use: Ubuntu 18.04 + Laz. 1.8.5 + FPC 3.0.5 (64 bits).

Thaddy

  • Hero Member
  • *****
  • Posts: 8679
Re: Is it possible to make "over-typecasting"?
« Reply #6 on: June 18, 2019, 06:31:50 pm »
It's a matter of TCustomGrid --> TDrawgrid?
Check the sources.
Most people that want to use threading should learn to patch their jeans first: use a needle.

devEric69

  • Full Member
  • ***
  • Posts: 121
Re: Is it possible to make "over-typecasting"?
« Reply #7 on: June 18, 2019, 06:54:13 pm »
I checked it out. My last post is correct: "TDrawGrid(myDBGrid).Col", for example, compiles with Delphi.


I can give another example to overcome the level of visibility: "TLisbox (myBDListBox). a_property_that_becomes_visible" (myBDListBox is TDBListBox).
But here, both are on the same branch in the Delphi VCL: they both inherit from TCustomListBox (appearanceally, it seams less "tricky", but it also allows you to bypass visibility).
For information, I'm translating a Delphi program, into Lazarus.
use: Ubuntu 18.04 + Laz. 1.8.5 + FPC 3.0.5 (64 bits).

Thaddy

  • Hero Member
  • *****
  • Posts: 8679
Re: Is it possible to make "over-typecasting"?
« Reply #8 on: June 18, 2019, 07:40:14 pm »
I already gave you the answer.
Most people that want to use threading should learn to patch their jeans first: use a needle.

devEric69

  • Full Member
  • ***
  • Posts: 121
Re: Is it possible to make "over-typecasting"?
« Reply #9 on: July 15, 2019, 07:24:07 pm »
Code: Pascal  [Select]
  1. TDBGrid2 = class(TDBGrid)
  2.   private
  3.   protected
  4.   public
  5.     (* Redeclaration of properties with a change in visibility ,
  6.     to be able to modify the height of the rows ,
  7.     and width of the columns of a TDBGrid .*)
  8.     property RowHeights;
  9.     property ColWidths;
  10.   published
  11. end;

use: Ubuntu 18.04 + Laz. 1.8.5 + FPC 3.0.5 (64 bits).