Recent

Author Topic: Class in Class/Osztály az osztályban (Multilang)  (Read 10048 times)

TheProgrammer

  • New Member
  • *
  • Posts: 48
  • Translator and programmer/Fordító és programozó
    • SourceCodePower.com
Class in Class/Osztály az osztályban (Multilang)
« on: August 20, 2011, 06:50:19 pm »
How can i create Class in class?
Hogyan csinálhatok osztályt az osztályban?

Example/Példa:
Code: [Select]
type
  TClassInClass = class
    var1, var2, var3 : String;
  end;

  TMyClass = class
    ClassInClass  : TClassInClass;
    function classfunction : Boolean;
    procedure classprocedure(const const1, const2, const3 : WideString);
  end; 


or/vagy

Code: [Select]
type
  TCecordInClass = record
    var1, var2, var3 : String;
  end;

  TMyClass = class
    CecordInClass : TCecordInClass;
    function classfunction : Boolean;
    procedure classprocedure(const const1, const2, const3 : WideString);
  end;

These codes don't work...
Ez a kód nem működik...

Error: Project xy raised exception class 'External:SIGSEGV'.

My System setting: Windows 7 32bit / Lazarus 0.9.30 and FPC 2.4.2

This is Unit file, not program...
Ez itt modul, nem program kód...

Mode: {$mode objfpc}{$H+}
My Uses list:
Ezeket a forrásokat használom:
Classes, SysUtils, Math, ActiveX, ComObj, Variants;

(I use Ole WMI function...)
(WMI Ole funkciót használok...)
Code: [Select]

procedure TMyClass.classprocedure(const const1, const2, const3 : WideString);
begin
ClassInClass.Var := FWbemObject.Properties_.Item('L2CacheSize').Value;
end;

All Code run in TThread class in Program...
AZ összes kód egy Programszál osztályban fut...
« Last Edit: August 22, 2011, 06:55:37 pm by TheProgrammer »
Translator and programmer/Fordító és programozó (Multilang)
My webpage/Weboldalam:
www.sourcecodepower.com

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Class in Class
« Reply #1 on: August 20, 2011, 06:59:42 pm »
The problem is not class in class (because it passes compiling step and generates executable), but how you use it.
Quote
ClassInClass.Var := FWbemObject.Properties_.Item('L2CacheSize').Value;
Have you initialized ClassInClass?

TheProgrammer

  • New Member
  • *
  • Posts: 48
  • Translator and programmer/Fordító és programozó
    • SourceCodePower.com
Re: Class in Class
« Reply #2 on: August 20, 2011, 07:08:44 pm »
I defined "ClassInClass" in type and I'd like to add value in "MyClass" procedure...
Definiáltam a típusoknál a "ClassInClass"-t és szeretnék adni neki egy értéket a "MyClass" procedurájában...

Defined: see examples...
Definíció: lásd példa....
« Last Edit: August 22, 2011, 05:58:52 pm by TheProgrammer »
Translator and programmer/Fordító és programozó (Multilang)
My webpage/Weboldalam:
www.sourcecodepower.com

TheProgrammer

  • New Member
  • *
  • Posts: 48
  • Translator and programmer/Fordító és programozó
    • SourceCodePower.com
Re: Class in Class
« Reply #3 on: August 20, 2011, 07:16:00 pm »
Quote
ClassInClass.Var := FWbemObject.Properties_.Item('L2CacheSize').Value;
Correction:
Javítás:
ClassInClass.Var1 := FWbemObject.Properties_.Item('L2CacheSize').Value;

I would like to do this in program:
Ezt szeretném elérni a programban:

MyClass.classprocedure('xy', 'xy', 'xy');

Label.Caption := MyClass.ClassInClass.Var1
« Last Edit: August 22, 2011, 06:00:17 pm by TheProgrammer »
Translator and programmer/Fordító és programozó (Multilang)
My webpage/Weboldalam:
www.sourcecodepower.com

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: Class in Class
« Reply #4 on: August 20, 2011, 07:36:52 pm »
What Leledumbo refers to is whether you have created an instance of ClassInClass, which would normally be done in the constructor of TMyClass.  Without it you will definitely get a SIGSEGV.

If you also get the SIGSEGV when using a Record, then it suggests the real issue is with the FWemObject.  Is that properly instantiated?

« Last Edit: August 20, 2011, 07:38:48 pm by Arbee »
1.0/2.6.0  XP SP3 & OS X 10.6.8

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Class in Class
« Reply #5 on: August 20, 2011, 07:41:39 pm »
I assume that you are familiar with associative classes & composite classes. If not read this
http://delphi.about.com/od/course/a/delphi_oop19.htm
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

TheProgrammer

  • New Member
  • *
  • Posts: 48
  • Translator and programmer/Fordító és programozó
    • SourceCodePower.com
Re: Class in Class
« Reply #6 on: August 20, 2011, 07:46:56 pm »
Is that properly instantiated?

Have you initialized ClassInClass?

How can i initalised the ClassInClass? This part I do not know...
Hogyan tudok inicializálni? Ezt a részét nem ismerem...
« Last Edit: August 22, 2011, 06:00:34 pm by TheProgrammer »
Translator and programmer/Fordító és programozó (Multilang)
My webpage/Weboldalam:
www.sourcecodepower.com

TheProgrammer

  • New Member
  • *
  • Posts: 48
  • Translator and programmer/Fordító és programozó
    • SourceCodePower.com
Re: Class in Class
« Reply #7 on: August 20, 2011, 07:54:06 pm »
I have to do "ClassInClass" variables in "Implementation" section? <-This by Google Translation
Kell csinálnom "ClassInClass" változókat az "Implementation" részhez?

Sorry, My English translation is not perfect...
« Last Edit: August 22, 2011, 06:00:45 pm by TheProgrammer »
Translator and programmer/Fordító és programozó (Multilang)
My webpage/Weboldalam:
www.sourcecodepower.com

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Class in Class
« Reply #8 on: August 20, 2011, 08:26:05 pm »
You can initialize TClassInClass in 2 places

a) inside the constructor of TMyClass. TMyClass will then be a composite class. TMyClass will then own TClassInClass.

b) elsewhere in the program. TMyClass will then be associated with TClassInClass & the two can have independent lifespans.
« Last Edit: August 20, 2011, 08:30:55 pm by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: Class in Class
« Reply #9 on: August 20, 2011, 09:09:16 pm »
When you declare a class in a class you end up with a nil pointer that represents where the class should go. only after you assign it
Code: [Select]
  CecordInClass:= TCecordInClass.Create;
will the class actually exist.
to do this properly you should declaire two function
Code: [Select]
Procedure TMyClass.AfterConstruction;
begin
  CecordInClass:= TCecordInClass.Create;
end;
Procedure TMyClass.BeforeDestruction;
begin
  CecordInClass.Destroy;
end;
  These are declare it TObject so you will need to add override to your  declaration
Code: [Select]
TMyClass = class
    CecordInClass : TCecordInClass;
    Procedure BeforeDestruction; override;
    Procedure AfterConstruction; override;
    function classfunction : Boolean;
    procedure classprocedure(const const1, const2, const3 : WideString);
  end;

TheProgrammer

  • New Member
  • *
  • Posts: 48
  • Translator and programmer/Fordító és programozó
    • SourceCodePower.com
Re: Class in Class
« Reply #10 on: August 20, 2011, 11:10:30 pm »
Thanks for All!Köszönöm a segítséget!

This is my solution 1:
1. megoldásom:
Code: [Select]
//Unit:
   TRecordInClass = record
     Var1, Var2 : String;
   end;

  TMyClass = class
    RecordInClass : TRecordInClass;
    constructor Create;
  end;


constructor TMyClass.Create;
begin
RecordInClass.Var1 := 'xy';
RecordInClass.Var2 := 'yx';
end;

//----------
//Program:
procedure TForm1.Button1Click(Sender: TObject);
begin
MyClass := TMyClass.Create;
Label1.Caption := MyClass.RecordInClass.Var1;
MyClass.Destroy; 
end;


This is my solution 2:
2. megoldásom:
Code: [Select]
//Unit:
   TClassInClass = Class
     Var1, Var2 : String;
     //Other
   end;

  TMyClass = class
    ClassInClass : TClassInClass;
    constructor Create;
    destructor Destroy;
  end;


constructor TMyClass.Create;
begin
ClassInClass := TClassInClass.Create;

ClassInClass.Var1 := 'xy';
ClassInClass.Var2 := 'yx';
end;

destructor TMyClass.Destroy;
begin
ClassInClass.Destroy;
end;

//----------
//Program:
procedure TForm1.Button1Click(Sender: TObject);
begin
MyClass := TMyClass.Create;
Label1.Caption := MyClass.ClassInClass.Var1;
MyClass.Destroy;
end;
« Last Edit: August 22, 2011, 06:01:41 pm by TheProgrammer »
Translator and programmer/Fordító és programozó (Multilang)
My webpage/Weboldalam:
www.sourcecodepower.com

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: Class in Class
« Reply #11 on: August 20, 2011, 11:39:57 pm »
do not forget that which you create you must also destroy

TheProgrammer

  • New Member
  • *
  • Posts: 48
  • Translator and programmer/Fordító és programozó
    • SourceCodePower.com
Re: Class in Class
« Reply #12 on: August 21, 2011, 01:03:59 am »
Ok :) I write Destroy/destructor code...
Oké :) Írok lezáró részeket (Destroy)...
« Last Edit: August 22, 2011, 06:02:09 pm by TheProgrammer »
Translator and programmer/Fordító és programozó (Multilang)
My webpage/Weboldalam:
www.sourcecodepower.com

 

TinyPortal © 2005-2018