Class in Class/Osztály az osztályban (Multilang) lazarus / 34-Windows ---------------------------------------------------------------------- TheProgrammer · 2011-08-20 16:50 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... ---------------------------------------------------------------------- Leledumbo · 2011-08-20 16:59 The problem is not class in class (because it passes compiling step and generates executable), but how you use it. QuoteClassInClass.Var := FWbemObject.Properties_.Item('L2CacheSize').Value;Have you initialized ClassInClass? ---------------------------------------------------------------------- TheProgrammer · 2011-08-20 17:08 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.... ---------------------------------------------------------------------- TheProgrammer · 2011-08-20 17:16 QuoteClassInClass.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 ---------------------------------------------------------------------- Arbee · 2011-08-20 17:36 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? ---------------------------------------------------------------------- JD · 2011-08-20 17:41 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 ---------------------------------------------------------------------- TheProgrammer · 2011-08-20 17:46 Quote from: Arbee on August 20, 2011, 07:36:52 pmIs that properly instantiated? Quote from: Leledumbo on August 20, 2011, 06:59:42 pmHave 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... ---------------------------------------------------------------------- TheProgrammer · 2011-08-20 17:54 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... ---------------------------------------------------------------------- JD · 2011-08-20 18:26 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. ---------------------------------------------------------------------- Carver413 · 2011-08-20 19:09 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 · 2011-08-20 21:10 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; ---------------------------------------------------------------------- Carver413 · 2011-08-20 21:39 do not forget that which you create you must also destroy ---------------------------------------------------------------------- TheProgrammer · 2011-08-20 23:03 Ok I write Destroy/destructor code... Oké Írok lezáró részeket (Destroy)...