Change type of class field lazarus / 62-General ---------------------------------------------------------------------- SymbolicFrank · 2022-01-31 11:40 I would like to do something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Item1 = class  end;   Item2 = class(Item1)  end;   Class1 = class  protected  MyItem: Item1;  end;   Class2 = class(Class1)  protected    MyItem: Item2;    // <-- This   end; There is a big class hierarchy of which I want to change a single thing, by changing a single method of an encapsulated field. But you cannot "upgrade" fields this way, AFAIK. The idea is that you create an instance of Item2 and assign that to MyItem, not the other way around. So, I don't think that, if it could be done, the right constructor is called. ---------------------------------------------------------------------- Zvoni · 2022-01-31 12:35 ?? This? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;Uses Classes;Type   { Item1 }   Item1 = class  Public    Constructor Create;  end;   { Item2 }   Item2 = class(Item1)    Public      Constructor Create;  end;   { Class1 }   Class1 = class  Strict Private    FMyItem:Item1;  protected    Property MyItem: Item1 Read FMyItem Write FMyItem;  end;   { Class2 }   Class2 = class(Class1)  Strict Private    FMyItem:Item2;  protected    Property MyItem: Item2 read FMyItem Write FMyItem;  end; { Item1 } constructor Item1.Create;begin  Writeln('Item1 created');end; { Item2 } constructor Item2.Create;begin  Writeln('Item2 created');end; Var  C1:Class1;  C2:Class2; begin  C1:=Class1.Create;  C2:=Class2.Create;  C1.MyItem:=Item1.Create;  C2.MyItem:=Item2.Create;  C1.MyItem.Free;  C2.MyItem.Free;  C1.Free;  C2.Free;end.  Prints for Lines 58/59 Item1 created Item2 created ---------------------------------------------------------------------- engkin · 2022-01-31 13:02 Quote from: SymbolicFrank on January 31, 2022, 12:40:13 pmThere is a big class hierarchy of which I want to change a single thing, by changing a single method of an encapsulated field. Can you show a more precise example? ---------------------------------------------------------------------- SymbolicFrank · 2022-02-01 08:56 Well, I keep changing unit dbf and some of the included ones. Like, I needed to overwrite AutoInc fields and change the field with the next value. All fields of TDbf are private, unfortunately. One of them, Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FDbfFile: TDbfFile; inherits from TPagedFile, which does those things. Like, it has this function: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ApplyAutoIncToBuffer(DestBuf: TRecordBuffer); which I need to change, which is unfortunately not virtual. And I'm part of a team, it would be nice if my teammates had the same version, which is diverging from trunk. The last bugfix submitted is not merged with trunk yet, so I have to maintain my own version. But I would prefer to keep the changes as small as possible, if possible by making new classes that inherit from and replace those. Not by duplicating 9 units with thousands of lines of code. @Zvoni: more like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  C1:=Class1.Create;  C2:=Class2.Create;  C1.MyItem:=Item1.Create;  C2.MyItem:=Item1.Create; // <-- This is done by an existing class, that knows nothing about Class2 or Item2.  C1.MyItem.Free;  C2.MyItem.Free;  C1.Free;  C2.Free;end. ---------------------------------------------------------------------- Zvoni · 2022-02-01 09:03 hmm....looks more like an Interface EDIT: have you thought about "reintroduce"? https://wiki.freepascal.org/Reintroduce ---------------------------------------------------------------------- SymbolicFrank · 2022-02-01 09:28 Quote from: Zvoni on February 01, 2022, 10:03:21 amhmm....looks more like an Interface EDIT: have you thought about "reintroduce"? https://wiki.freepascal.org/Reintroduce Exactly. But for fields. ---------------------------------------------------------------------- Zvoni · 2022-02-01 09:40 Maybe this: https://www.freepascal.org/docs-html/ref/refsu39.html Rewrite the Field to a Property? ---------------------------------------------------------------------- SymbolicFrank · 2022-02-01 09:46 I already changed things so it works, but that requires ~ 10 small changes in two files. Ah, well, I'll make a list of diffs to incorporate. I wanted to put FPC / Laz in our own Git repository as well, but it was deemed a tool and too big.