Creation of a copy of a class independent of other objects



  • Hello. I'ating a copy of this pattern:

        type
          TExercise = object;
    
    var
      TMyExercise: TExercise;
    

    So I may not declare the variables on the form to create a copy of the class and apply to the object without reference to the form. It also allows me to have the only copy that stores the data I need, which is used in several forms.

    But that's how it works because of the opposite interoperability, judging.

    Is there another way to create an independent class in Borland Delphi 7?



  • In Delphi, it's not common to call the big-word variables T at the beginning, as such a letter at the beginning of the literature usually indicates that it is a type (from angle type, e.g.: TExerciseTObjectTStringList etc.). It's easier to read the code, even your own :

    Usually class is declared, for example:

    TExercise = class(TObject)
    private
      FMyInt: Integer;
      FMyString: string;
    public
      property Int: Integer read FMyInt write FMyInt;
      property Str: string read FMyString; // только для чтения
    end;
    

    In order to create a copy of the class, the designer must be called:

    MyExercise := TExercise.Create;
    

    When the facility's over and it's no longer needed, It's necessary to save memory. Subject:

    MyExercise.Free;
    

    Otherwise, there will be a memory leak and it will end sooner or later.


    In order to create an " fast " independent global facility for use in different forms, one can:

    unit UnitExercise;
    

    interface

    type
    TExercise = class(TObject)
    private
    MyInt: Integer;
    MyString: string;
    public
    property Int: Integer read MyInt write MyInt;
    property Str: string read MyString; // только для чтения
    end;

    var
    MyExercise: TExercise;

    implementation

    initialization
    MyExercise := TExercise.Create;

    finalization
    MyExercise.Free;

    end.

    And add the module UnitExercise List of modules usedusesIn those modules where the facility is intended to be used.

    However, there is a lack of design: You have little control over the establishment and destruction of such facilities (if they are many, they are different modules and are still dependent on each other), which may be relevant large projects. Management of section calls initialization and finalization You can change the way the modules are announced in the project and its modules, but it's a very complex equilibristian. If it's interesting, it's a subject for a separate question.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2