Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with nested/inner classes

Status
Not open for further replies.

liscio

Programmer
Jul 11, 2007
8
IT
hi all,
i'm developing an application with Delphi 6.
I need to define some Inner Classes (Nested..).
I really don't know how to do. I found an article explaining the syntax, but it doesn't compile on delphi 6.
The article say to do this:

type OuterClass=Class
procedure prova;

type InnerClass = class
procedure prova2;
end;
end;


but it does not compile...
what can i do?

Thanks All.
Andrea.
 
What are you trying to do exactly i.e. can you explain your aims? Are you attempting inheritance?

I've not seen this done before. If you are intending to use an instance of InnerClass in OuterClass then I personally would define the InnerClass first and then define the OuterClass below it. The OuterClass could use a member variable of type InnerClass - see below.
Code:
type
  TInnerClass = class
  public
    procedure prova2;
  end;

  TOuterClass = class
  private
    fInnerClass: TInnerClass;
  public
    procedure prova;
  end;

If however you are attempting inheritance and you want InnerClass to override/extend the implementation of OuterClass's prova method then you would structure it like this:
Code:
type
  TOuterClass = class
  protected
    procedure prova; virtual;
  end;

  TInnerClass = class(TOuterClass)
  public
    procedure prova; override;
  end;
...and then in the implementation of TInnerClass.prova you call inherited to ensure that TOuterClass.prova code is called first:
Code:
procedure TInnerClass.prova;
begin
  inherited;
  // TInnerClass specific code goes here
end;

Hope this helps!



Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
thanks for the reply....
I try to do a different thing.
I want to do this:





Unit unit1

interface

uses
classes;


type TOuterClass = class
type TInnerClass = class
public
string strInner1;
constructor Create;
function getDataEqStr:boolean;
end;
public
string strOuter1;
TInnerClass classInn1;
TInnerClass classInn2;
constructor create;
end;

implementation


constructor TOuterClass.create;
begin
strOuter1:='';
classInn1:=TInnerClass.Create;
classInn2:=TInnerClass.Create;
end;

constructor TInnerClass.create;
begin
strInner1:='';
end;

procedure TInnerClass.GetDataStr:boolean;
begin
if (strInner1 = strOuter1) then
result:=true
else
result:=false;
end;
 
I don't believe it's possible in Delphi 6. It was a new feature of Delphi 7 (
In Delphi 6, I would implement the above as follows:
Code:
interface

type
  TInnerClass = class
  public
    strInner1: String;
    constructor Create;
  end;

  TOuterClass = class
  public
    strOuter1: String;
    classInn1: TInnerClass;
    classInn2: TInnerClass;
    constructor Create;
    function getDataEqStr(CompareObj: TInnerClass): Boolean;
  end;

implementation

{ TOuterClass }

constructor TOuterClass.Create;
begin
  strOuter1 := '';
  classInn1 := TInnerClass.Create;
  classInn2 := TInnerClass.Create;
end;

function TOuterClass.getDataEqStr(CompareObj: TInnerClass): Boolean;
begin
  Result := (strOuter1 = CompareObj.strInner1);
end;

{ TInnerClass }

constructor TInnerClass.Create;
begin
  strInner1 := '';
end;

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Ok, thanks.
I'll try to do it another way.
Thanks a lot again.
Andrea.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top