Recent

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

devEric69

  • Hero Member
  • *****
  • Posts: 648
[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: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
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 »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
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

  • Hero Member
  • *****
  • Posts: 648
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: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

wp

  • Hero Member
  • *****
  • Posts: 11854
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.

devEric69

  • Hero Member
  • *****
  • Posts: 648
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: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
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.
Specialize a type, not a var.

devEric69

  • Hero Member
  • *****
  • Posts: 648
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: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Is it possible to make "over-typecasting"?
« Reply #8 on: June 18, 2019, 07:40:14 pm »
I already gave you the answer.
Specialize a type, not a var.

devEric69

  • Hero Member
  • *****
  • Posts: 648
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: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

 

TinyPortal © 2005-2018