I have a class TChild derived from TParent. TParent has a property MyProp that is reading and setting some values in an array. Of course this property is inherited by the TChild, but I want to add few extra processing in child's property. The code below explains better what I want to do but it is not working. How can I implement it?
TParent = class...
private
function getStuff(index: integer): integer; virtual;
procedure setStuff(index: integer; value: integer); virtual;
public
property MyProp[index: integer] read GetStuff write SetStuff
end;
TChild = class...
private
procedure setStuff(index: integer; value: integer); override;
function getStuff(index: integer): integer; override;
public
property MyProp[index: integer] read GetStuff write SetStuff
end;
procedure TChild.setStuff(value: integer);
begin
inherited; // <-- execute parent 's code and
DoMoreStuff; // <-- do some extra suff
end;
function TChild.getStuff;
begin
result:= inherited; <---- problem was here
end;
Solved.
The child function implementation was wrong. Basically that code works.
The solution was:
Result := inherited getStuff(Index);
Related
I would like to use TObjectDataset which relies on TObjectList<> (System.Generics.Collections / Spring.Collections) but only have a TObjectList (System.Contnrs). Is there any way besides for iterating through objects and building a new TObjectList<> to get this working? Ultimately I would like to couple the TObjectList to an Objectdataset in order to bind to an UI.
Your question is slightly wrong. The Spring4d TObjectDataSet takes an IObjectList interface which is a specialization of IList<T> where T is TObject.
This contract is matched by the Contnrs.TObjectList. So "simply" create a wrapper class for your TObjectList that implements IObjectList. I put simply in quotes because this interface has quite a lot of methods. You can use TListBase<T> as base class for your adapter which already has all methods implemented. Then you only need to override a few (take a look at TList<T> which ones those are).
One important detail to know is that the TObjectDataSet needs to know the exact class of the objects in your list. This is done via the ElementType property of the IObjectList. If that returns TObject though this is not very helpful. So you need to override that method.
Edit: Here is the full code of such an adapter class:
unit Spring.Collections.ObjectListAdapter;
interface
uses
Contnrs,
TypInfo,
Spring.Collections,
Spring.Collections.Base;
type
TObjectListAdapter = class(TListBase<TObject>, IObjectList)
private
fList: TObjectList;
fClassType: TClass;
protected
function GetCapacity: Integer; override;
function GetCount: Integer; override;
function GetElementType: PTypeInfo; override;
function GetItem(index: Integer): TObject; override;
procedure SetCapacity(value: Integer); override;
procedure SetItem(index: Integer; const value: TObject); override;
public
constructor Create(const list: TObjectList; classType: TClass);
procedure Delete(index: Integer); override;
function Extract(const item: TObject): TObject; override;
procedure Insert(index: Integer; const item: TObject); override;
procedure Exchange(index1, index2: Integer); override;
procedure Move(currentIndex, newIndex: Integer); override;
end;
implementation
uses
Classes,
Types;
{ TObjectListAdapter }
constructor TObjectListAdapter.Create(const list: TObjectList; classType: TClass);
begin
inherited Create;
fList := list;
fClassType := classType;
end;
procedure TObjectListAdapter.Delete(index: Integer);
begin
fList.Delete(index);
end;
procedure TObjectListAdapter.Exchange(index1, index2: Integer);
begin
fList.Exchange(index1, index2);
end;
function TObjectListAdapter.Extract(const item: TObject): TObject;
begin
Result := fList.Extract(item);
end;
function TObjectListAdapter.GetCapacity: Integer;
begin
Result := fList.Capacity;
end;
function TObjectListAdapter.GetCount: Integer;
begin
Result := fList.Count;
end;
function TObjectListAdapter.GetElementType: PTypeInfo;
begin
Result := fClassType.ClassInfo;
end;
function TObjectListAdapter.GetItem(index: Integer): TObject;
begin
Result := fList[index];
end;
procedure TObjectListAdapter.Insert(index: Integer; const item: TObject);
begin
fList.Insert(index, item);
end;
procedure TObjectListAdapter.Move(currentIndex, newIndex: Integer);
begin
fList.Move(currentIndex, newIndex);
end;
procedure TObjectListAdapter.SetCapacity(value: Integer);
begin
fList.Capacity := value;
end;
procedure TObjectListAdapter.SetItem(index: Integer; const value: TObject);
begin
fList[index] := value;
end;
end.
Is there any way besides for iterating through objects and building a new TObjectList<T> to get this working?
There is not. The two types are not compatible.
I create my own class and I want to use it in my new component but I am getting an error...
The code is the following:
type
TMyClass = class
Name: string;
Number: double;
end;
TMyComponent = class(TCustomPanel)
private
FMyClass: TMyClass;
public
procedure SetMyClass(aName: string; aNumber: double);
published
property MyClass: TMyClass write SetMyClass;
end;
procedure SetMyClass(aName: string; aNumber: double);
begin
FMyClass.Name:= aName;
FMyClass.Number:= aNumber;
end;
it appears that the property has incompatible types, I don't know why.
Does anybody has a clue about that and how can I solve this problem.
Having a FName and FNumber as fields in TMyComponent is not an option, my code is more complex and this is a simple example to explain my goal.
thanks
The things that I can see wrong with your code at present are:
The property setter must receive a single parameter of the same type as the property, namely TMyClass.
The property setter must be a member of the class, but you've implemented it as a standalone procedure.
A published property needs to have a getter.
So the code would become:
type
TMyClass = class
Name: string;
Number: double;
end;
TMyComponent = class(TCustomPanel)
private
FMyClass: TMyClass;
procedure SetMyClass(Value: TMyClass);
published
property MyClass: TMyClass read FMyClass write SetMyClass;
end;
procedure TMyComponent.SetMyClass(Value: TMyClass);
begin
FMyClass.Name:= Value.Name;
FMyClass.Number:= Value.Number;
end;
This code does not instantiate FMyClass. I'm guessing that the code that does instantiate FMyClass is part of the larger component code that has been excised for the sake of this question. But obviously you do need to instantiate FMyClass.
An alternative to instantiating FMyClass is to turn TMyClass into a record. Whether or not that would suit your needs I cannot tell.
It looks like you are having some problems instantiating this object. Do it like this:
type
TMyClass = class
Name: string;
Number: double;
end;
TMyComponent = class(TCustomPanel)
private
FMyClass: TMyClass;
procedure SetMyClass(Value: TMyClass);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property MyClass: TMyClass read FMyClass write SetMyClass;
end;
constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited;
FMyClass:= TMyClass.Create;
end;
destructor TMyComponent.Destroy;
begin
FMyClass.Free;
inherited;
end;
procedure TMyComponent.SetMyClass(Value: TMyClass);
begin
FMyClass.Name:= Value.Name;
FMyClass.Number:= Value.Number;
end;
One final comment. Using MyClass for an object is a bad name. Use class for the type, and object for the instance. So, your property should be MyObject and the member field should be FMyObject etc.
Try this:
type
TMyClass = class
Name: string;
Number: double;
end;
TMyComponent = class(TCustomPanel)
private
FMyClass: TMyClass;
public
procedure SetMyClass(Value: TMyClass);
published
property MyClass: TMyClass write SetMyClass;
end;
procedure TMyComponent.SetMyClass(Value);
begin
FMyClass := Value;
end;
unit MyComponentTest2;
interface
uses SysUtils, Classes, Controls, Forms, ExtCtrls, Messages, Dialogs;
type
TMyClass = class
Name: string;
Number: double;
end;
TMyComponentTest2 = class(TCustomPanel)
private
FMyClass: TMyClass;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetMyClass(Value: TMyClass);
published
property MyClass: TMyClass read FMyClass write SetMyClass;
end;
procedure Register;
implementation
constructor TMyComponentTest2.Create(AOwner: TComponent);
begin
Inherited Create(AOwner);
FMyClass:= TMyClass.Create;
end;
destructor TMyComponentTest2.Destroy;
begin
Inherited;
FMyClass.Free;
end;
procedure TMyComponentTest2.SetMyClass(Value: TMyClass);
begin
FMyClass.Name:= Value.Name;
FMyClass.Number:= Value.Number;
end;
procedure Register;
begin
RegisterComponents('MyComponents', [TMyComponentTest2]);
end;
end.
I have a parent class with one important abstract procedure which I am overloading in many child classes as the example code shown below:
TCParent = Class
private
public
procedure SaveConfig; virtual; abstract;
end;
TCChild = Class(TCParent)
private
public
procedure SaveConfig; override;
end;
Now there I need to (overload) this procedure with another SaveConfig procedure that will accept parameters, yet I don't want to make big changes in the parent class that might require that I go and make changes in all other child classes.
Is there a way I can overload SaveConfig in this specific child class without making big changes to the parent class and other child classes that inherit from it?
You can use reintroduce to add a new overloaded method. Note that the order of reintroduce; overload; in the child class is required; if you reverse them, the code won't compile.
TCParent = Class
private
public
procedure SaveConfig; virtual; abstract;
end;
TCChild = Class(TCParent)
private
public
procedure SaveConfig; overload; override;
procedure SaveConfig(const FileName: string); reintroduce; overload;
end;
(Tested in Delphi 7, so should work in it and all later versions.)
Since you do not want to make changes to other descendants, I would suggest adding an optional field to the parent class to hold the parameters, then any descendant that wants to use parameters can use them. That way, you don't have to change the signature of the overridden SaveConfig(). For example:
type
TCParent = class
protected
SaveConfigParams: TStrings; // or whatever...
public
procedure SaveConfig; overload; virtual; abstract;
procedure SaveConfig(Params: TStrings); overload;
end;
procedure TCParent.SaveConfig(Params: TStrings);
begin
SaveConfigParams := Params;
try
SaveConfig;
finally
SaveConfigParams := nil;
end;
end;
.
type
TCChild = class(TCParent)
public
procedure SaveConfig; override;
end;
procedure TCChild.SaveConfig;
begin
if SaveConfigParams <> nil then
begin
// do something that uses the parameters...
end else begin
// do something else...
end;
end;
.
type
TCChild2 = class(TCParent)
public
procedure SaveConfig; override;
end;
procedure TCChild2.SaveConfig;
begin
// do something, ignoring the SaveConfigParams...
end;
TMyBaseClass=class
constructor(test:integer);
end;
TMyClass=class(TMyBaseClass);
TClass1<T: TMyBaseClass,constructor>=class()
public
FItem: T;
procedure Test;
end;
procedure TClass1<T>.Test;
begin
FItem:= T.Create;
end;
var u: TClass1<TMyClass>;
begin
u:=TClass1<TMyClass>.Create();
u.Test;
end;
How do I make it to create the class with the integer param. What is the workaround?
Just typecast to the correct class:
type
TMyBaseClassClass = class of TMyBaseClass;
procedure TClass1<T>.Test;
begin
FItem:= T(TMyBaseClassClass(T).Create(42));
end;
Also it's probably a good idea to make the constructor virtual.
You might consider giving the base class an explicit method for initialization instead of using the constructor:
TMyBaseClass = class
public
procedure Initialize(test : Integer); virtual;
end;
TMyClass = class(TMyBaseClass)
public
procedure Initialize(test : Integer); override;
end;
procedure TClass1<T>.Test;
begin
FItem:= T.Create;
T.Initialize(42);
end;
Of course this only works, if the base class and all subclasses are under your control.
Update
The solution offered by #TOndrej is far superior to what I wrote below, apart from one situation. If you need to take runtime decisions as to what class to create, then the approach below appears to be the optimal solution.
I've refreshed my memory of my own code base which also deals with this exact problem. My conclusion is that what you are attempting to achieve is impossible. I'd be delighted to be proved wrong if anyone wants to rise to the challenge.
My workaround is for the generic class to contain a field FClass which is of type class of TMyBaseClass. Then I can call my virtual constructor with FClass.Create(...). I test that FClass.InheritsFrom(T) in an assertion. It's all depressingly non-generic. As I said, if anyone can prove my belief wrong I will upvote, delete, and rejoice!
In your setting the workaround might look like this:
TMyBaseClass = class
public
constructor Create(test:integer); virtual;
end;
TMyBaseClassClass = class of TMyBaseClass;
TMyClass = class(TMyBaseClass)
public
constructor Create(test:integer); override;
end;
TClass1<T: TMyBaseClass> = class
private
FMemberClass: TMyBaseClassClass;
FItem: T;
public
constructor Create(MemberClass: TMyBaseClassClass); overload;
constructor Create; overload;
procedure Test;
end;
constructor TClass1<T>.Create(MemberClass: TMyBaseClassClass);
begin
inherited Create;
FMemberClass := MemberClass;
Assert(FMemberClass.InheritsFrom(T));
end;
constructor TClass1<T>.Create;
begin
Create(TMyBaseClassClass(T));
end;
procedure TClass1<T>.Test;
begin
FItem:= T(FMemberClass.Create(666));
end;
var
u: TClass1<TMyClass>;
begin
u:=TClass1<TMyClass>.Create(TMyClass);
u.Test;
end;
Another more elegant solution, if it is possible, is to use a parameterless constructor and pass in the extra information in a virtual method of T, perhaps called Initialize.
What seems to work in Delphi XE, is to call T.Create first, and then call the class-specific Create as a method afterwards. This is similar to Rudy Velthuis' (deleted) answer, although I don't introduce an overloaded constructor. This method also seems to work correctly if T is of TControl or classes like that, so you could construct visual controls in this fashion.
I can't test on Delphi 2010.
type
TMyBaseClass = class
FTest: Integer;
constructor Create(test: integer);
end;
TMyClass = class(TMyBaseClass);
TClass1<T: TMyBaseClass, constructor> = class
public
FItem: T;
procedure Test;
end;
constructor TMyBaseClass.Create(test: integer);
begin
FTest := Test;
end;
procedure TClass1<T>.Test;
begin
FItem := T.Create; // Allocation + 'dummy' constructor in TObject
try
TMyBaseClass(FItem).Create(42); // Call actual constructor as a method
except
// Normally this is done automatically when constructor fails
FItem.Free;
raise;
end;
end;
// Calling:
var
o: TClass1<TMyClass>;
begin
o := TClass1<TMyClass>.Create();
o.Test;
ShowMessageFmt('%d', [o.FItem.FTest]);
end;
type
TBase = class
constructor Create (aParam: Integer); virtual;
end;
TBaseClass = class of TBase;
TFabric = class
class function CreateAsBase (ConcreteClass: TBaseClass; aParam: Integer): TBase;
class function CreateMyClass<T: TBase>(aParam: Integer): T;
end;
TSpecial = class(TBase)
end;
TSuperSpecial = class(TSpecial)
constructor Create(aParam: Integer); override;
end;
class function TFabric.CreateAsBase(ConcreteClass: TBaseClass; aParam: Integer): TBase;
begin
Result := ConcreteClass.Create (aParam);
end;
class function TFabric.CreateMyClass<T>(aParam: Integer): T;
begin
Result := CreateAsBase (T, aParam) as T;
end;
// using
var
B: TBase;
S: TSpecial;
SS: TSuperSpecial;
begin
B := TFabric.CreateMyClass <TBase> (1);
S := TFabric.CreateMyClass <TSpecial> (1);
SS := TFabric.CreateMyClass <TSuperSpecial> (1);
I try to do list of procedures this way:
type
TProc = procedure of object;
TMyClass=class
private
fList:Tlist;
function getItem(index:integer):TProc;
{....}
public
{....}
end;
implementation
{....}
function TMyClass.getItem(index: Integer): TProc;
begin
Result:= TProc(flist[index]);// <--- error is here!
end;
{....}
end.
and get error:
E2089 Invalid typecast
How can I fix it?
As I see, I can make a fake class with only one property Proc:TProc; and make list of it. But I feel that it's a bad way, isn't it?
PS: project have to be delphi-7-compatible.
The typecast is invalid because you can not fit a method pointer to a pointer, a method pointer is in fact two pointers first being the address of the method and the second being a reference to the object that the method belongs. See Procedural Types in the documentation. This will not work in any version of Delphi.
Sertac has explained why your code doesn't work. In order to implement a list of such things in Delphi 7 you can do something like this.
type
PProc = ^TProc;
TProc = procedure of object;
TProcList = class(TList)
private
FList: TList;
function GetCount: Integer;
function GetItem(Index: Integer): TProc;
procedure SetItem(Index: Integer; const Item: TProc);
public
constructor Create;
destructor Destroy; override;
property Count: Integer read GetCount;
property Items[Index: Integer]: TProc read GetItem write SetItem; default;
function Add(const Item: TProc): Integer;
procedure Delete(Index: Integer);
procedure Clear;
end;
type
TProcListContainer = class(TList)
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
end;
procedure TProcListContainer.Notify(Ptr: Pointer; Action: TListNotification);
begin
inherited;
case Action of
lnDeleted:
Dispose(Ptr);
end;
end;
constructor TProcList.Create;
begin
inherited;
FList := TProcListContainer.Create;
end;
destructor TProcList.Destroy;
begin
FList.Free;
inherited;
end;
function TProcList.GetCount: Integer;
begin
Result := FList.Count;
end;
function TProcList.GetItem(Index: Integer): TProc;
begin
Result := PProc(FList[Index])^;
end;
procedure TProcList.SetItem(Index: Integer; const Item: TProc);
var
P: PProc;
begin
New(P);
P^ := Item;
FList[Index] := P;
end;
function TProcList.Add(const Item: TProc): Integer;
var
P: PProc;
begin
New(P);
P^ := Item;
Result := FList.Add(P);
end;
procedure TProcList.Delete(Index: Integer);
begin
FList.Delete(Index);
end;
procedure TProcList.Clear;
begin
FList.Clear;
end;
Disclaimer: completely untested code, use at your own risk.