1

I need to add some functionality to the TObjectArray class in the XSuperObject library. I could just add it directly into the source code in XSuperObject.pas, but that's not how I should do it. Instead I want to derive my own interface+class from the ISuperArray/TSuperArray pair. But how? I know how to derive a class on its own, but I've never done it before when interfaces are involved.

XSuperObject.pas defines these classes as follows:

type
  ISuperArray = interface(IBaseJSON<IJSONArray, Integer>)
  ['{41A2D578-CFAB-4924-8F15-0D0227F35412}']
    ...
  end;

  TSuperArray = class(TBaseJSON<IJSONArray, Integer>, ISuperArray)
    ...
  end;

What I would like to do is this:

uses
  ...
  xsuperobject;

type
  TProductJson = class(TSuperArray)
    CategoryIndex: integer;
    SubcategoryIndex: integer;
    constructor Create(JSON: String);
    procedure AddCategory(Title: string);
    procedure AddSubcategory(Title: string);
  end;

Or should I also derive from the interface at the same time? Maybe something like this?

uses
  ...
  xsuperobject;

type
  IProductJson = interface(ISuperArray)
  end;

  TProductJson = class(TSuperArray, IProductJson)
    CategoryIndex: integer;
    SubcategoryIndex: integer;
    constructor Create(JSON: String);
    procedure AddCategory(Title: string);
    procedure AddSubcategory(Title: string);
  end;

So my question is: how do I properly derive my own class/interface from ISuperArray/TSuperArray?
And if I must derive from the interface as well, do I also need to duplicate the GUID, or create a new GUID, or drop the GUID?

And the second question: XSuperObject.pas defines 2 functions: SO and SA, to initialize a SuperObject resp. SuperArray. Will the SA function remain compatible with my derived interface/class? Or will it not be assignment compatible after I have derived from it?
(maybe a typecast could help there?)

Rob
  • 155
  • 1
  • 12
  • *Will these two functions remain compatible with my derived interface/class?* No they won't. Your proposed solution is no good. What you should rather do is state your problem, and ask us to help you find a solution. By asking about your solution, you are constraining the help we can give you. We can answer your question directly, but in the end it will be pointless since you'll not be able to use the solution you propose. – David Heffernan Nov 05 '15 at 10:27
  • Ok, so why is my solution no good? Should I not derive my own class to add functionality to TSuperArray? – Rob Nov 05 '15 at 10:40
  • It's no good because functions like `SO` and `SA` won't return objects with your added capability. As to what you should do, very hard for us to say. We don't know what your motivation is. You didn't present the problem, only the solution. This is what is known as the [XY problem](http://xyproblem.info/). – David Heffernan Nov 05 '15 at 10:59
  • The problem I have is that I do not know the correct syntax for deriving from a class+interface. I know how to do it from a class alone, but not if there is also a corresponding interface. – Rob Nov 05 '15 at 11:10
  • I could remove the two code examples of what I have tried so far. Would that actually improve the question? – Rob Nov 05 '15 at 11:11
  • In my view, the real issue is that you are asking about your solution rather than your problem. Did you follow the link in my previous comment? – David Heffernan Nov 05 '15 at 11:28

0 Answers0