Recent

Author Topic: Can a class create a class within itself?  (Read 822 times)

petejones

  • New Member
  • *
  • Posts: 13
Can a class create a class within itself?
« on: May 19, 2022, 10:37:40 pm »
Can a class create a class within itself? Here is the code:

type
    TParameter = class(TObject)
    private
      ParamValue: Variant;
      ParamType: TParameterType;
    end;

type
    TParameters = class
    public
      Items: specialize TFPGObjectList<TParameter>;
      constructor Create();
      procedure Add(ParameterValue: Variant; ParameterType: TParameterType);
    end;

I want the TParameters class to create the Items list when an instance of the TParameters class is created. Is this possible?

Thanks
Pete

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Can a class create a class within itself?
« Reply #1 on: May 19, 2022, 10:54:37 pm »
specialize:
Code: Pascal  [Select][+][-]
  1. type
  2.   TParameterList=specialize TFPGObjectList<TParameter>;
  3.  

Create:
Code: Pascal  [Select][+][-]
  1. constructor TParameters.Create;
  2. begin
  3.   inherited;
  4.   Items:=TParameterList.Create;
  5. end;
  6.  

Free:
Code: Pascal  [Select][+][-]
  1. destructor TParameters.Destroy;
  2. begin
  3.   Items.Free;
  4. end;
  5.  

How to add:
Code: Pascal  [Select][+][-]
  1. procedure TParameters.Add(ParameterValue:Variant; ParameterType:TParameterType);
  2. var
  3.   Parameter:TParameter;
  4. begin
  5.   Parameter:=TParameter.Create;
  6.   Parameter.ParamValue:=ParameterValue;
  7.   Parameter.ParamType:=ParameterType;
  8.   Items.Add(Parameter);
  9. end;
  10.  
« Last Edit: May 19, 2022, 11:05:33 pm by Thausand »

petejones

  • New Member
  • *
  • Posts: 13
Re: Can a class create a class within itself?
« Reply #2 on: May 19, 2022, 11:33:32 pm »
Thanks  8)

petejones

  • New Member
  • *
  • Posts: 13
Re: Can a class create a class within itself?
« Reply #3 on: May 20, 2022, 02:21:17 pm »
I'm getting an error to do with the constructor - when I put a breakpoint on Items:=TParameterList.Create and step over that line, Items isn't set to anything and I get a SIGSEGV error when I attempt to add to Items.

Any ideas?
« Last Edit: May 20, 2022, 02:34:39 pm by petejones »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Can a class create a class within itself?
« Reply #4 on: May 20, 2022, 03:52:42 pm »
My best idea is to get you to share a compilable project that gives the error.
Then others can pinpoint its cause quickly without have to guess or make assumptions about what the rest of your code might contain.

petejones

  • New Member
  • *
  • Posts: 13
Re: Can a class create a class within itself?
« Reply #5 on: May 20, 2022, 04:54:39 pm »
Good idea, I've attached a zip of the test project.

Cheers
Pete

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Can a class create a class within itself?
« Reply #6 on: May 20, 2022, 05:11:07 pm »
not good:
Code: Pascal  [Select][+][-]
  1.   Parameters.Create;
  2.   Parameters.Add('Test', TParameterType.TText);
  3.   Parameters.Free;
  4.  

better:
Code: Pascal  [Select][+][-]
  1.   Parameters := TParameters.Create;
  2.   Parameters.Add('Test', TParameterType.TText);
  3.   Parameters.Free;
  4.  

Is not point you ask how to create class ? Then you forget to do ;D

petejones

  • New Member
  • *
  • Posts: 13
Re: Can a class create a class within itself?
« Reply #7 on: May 20, 2022, 05:21:42 pm »
Ah yes, thanks!

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Can a class create a class within itself?
« Reply #8 on: May 20, 2022, 05:35:58 pm »
While you can use generics, as you do, I think generics + variants is somewhat overkill.
I would simply use a TFPObjectList from contnrs unit.

 

TinyPortal © 2005-2018