Analysis of some class. Seven questions. lazarus / 61-Beginners ---------------------------------------------------------------------- yurkad · 2017-04-22 13:53 We have some Class someClass. How to know: 1. Line of create of this class. someClass= class(BaseClass); or someClass= class; or ... 2. name of BaseClass, if exists. 3. If has already memory (was initialized) or not still (in RunTime). 4. List of all Methods of class with section name (public, publish, ...) for each Method. 5. List of all Fields of class with section name (public, publish, ...) for each Field. 6. List of all Properties of class with section name (public, publish, ...) for each Property. And  one additional question more: 7. How can be duplicate completely someClass to newSomeClass? I saw several information in Internet about this question and question 6. For duplicate it's used method Clone (http://stackoverflow.com/questions/4041760/correct-way-to-duplicate- delphi-object), and method Assign (http://stackoverflow.com/questions/23163655/can-i-use-assign-to- duplicate-an-object-of-objects) But I do not know which is better. ---------------------------------------------------------------------- jmm72 · 2017-04-22 17:02 I suggest you to check the TObject class from which so many classes are derived (except the ones you make yourself), and for example it implements this function to return the class as a string: http://freepascal.org/docs- html/current/rtl/system/tobject.classname.html It has some other related functions related to class identification. Some descendant classes of TObject are also useful, such as TPersistent which has the virtual method Assign to make copies of objects. For question 1: when you make a new class, you are just saying if you're using another class as ancestor, or not. With a base class, the new class will inherit all the characteristics of the base class: fields, methods, properties, and ancestors. Note that some base classes need certain methods to be implemented since they're declared but not implemented yet. For question 2: answered above. For question 3: don't confuse a class (a template which describes an object with fields and methods) and an instance of that class. So someClass = class(BaseClass) is a class declaration where you define the fields and methods and whatever; and somevar := someClass.Create will create and store an instance of that class inside the variable somevar. If you mix terminologies, you'll mix all the rest, both for you and for us to explain anything or to even understand your questions. Said that, if somevar = Nil then the variable doesn't contain an instance yet. If it's different from Nil, it might have been instantiated and stored there before, but you can't know the very moment you do that check if that instance (it's just a pointer to a memory place in the heap) is still valid or the instance was freed. That's why I only free local instance variables at the end of functions, in unit finalization blocks, or use FreeAndNil so I don't mix stuff. In short, if it's Nil it hasn't been created, if not Nil it was created but only you are responsible of knowing if that particular instance is valid through wise creation and freeing, or using indirect methods like storing the instances in collections which take care of freeing and nil-ing for you. For questions 4 to 6: I'm guessing you mean everything a class has, including whatever has inherited from ancestor classes. I don't think there's a way to list that within Lazarus given a certain class, someone correct me if I'm wrong. The old slow way is to use alt+up and control+down while the cursor is above the class name to navigate between the declaration and implementation of everything. If you're at a class name e.g. TPersistent, alt+up will move you to its declaration. There you can move to the declaration of its ancestor or down to the fields and method declarations. Note that if you're in a method declaration and press again alt+up, and the ancestor class also has that method, the code editor will move you to the method declaration of the ancestor. At any method declaration, control+down will move you to the implementation. And of course there's the help, the online help, and the wiki. For question 7: here you're mixing things as I've said before. It's not clear if you want to duplicate a class, or create a new instance of an already created instance which is an exact duplicate. Judging by the links you posted, it's the second option. As you might know, newobj := oldobj just duplicates the pointers, so both variables point to the same place in the heap. TPersistent has the Assign method, which is the standard name for duplication/cloning of instances; however that method must be implemented in each descendant class to copy each internal field from one instance to another. In other words, to clone an instance the class must have a method to copy each class field to the new instance similar to newobj.Assign(oldobj). Something like (if it comes from TObject and TPersistent, crude example): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TSomeClass.Assign(const instance: TPersistent)var  i : TSomeClass; begin  if instance is TSomeClass then  begin      i := TSomeClass(instance);      FFieldWhatever := i.FFieldWhatever;      FSomeOtherField := i.FSomeOtherField;      FImportantField := i.FImportantField ;  end; end; Assign implementations in classes descending from TPersistent call the ancestor method in case the instance you want to clone is not of the same class, using 'inherited Assign(instance)' and they call the instance to clone 'source', but more or less that's about it. Oh I have a pretty naive approach to objects. Others might have other answers. One final thing: it's a tradition of Pascal to name all types starting with uppercase T (except interfaces and exceptions which start with I and E respectively). So I'd not name it someClass, but TSomeClass. Also, fields start with F (just the fields, not the properties). I know you only used it as an example, but if you're not naming your types and classes starting with T, you can start right now ---------------------------------------------------------------------- Bart · 2017-04-22 17:34 Quote from: jmm72 on April 22, 2017, 07:02:21 pmI suggest you to check the TObject class from which so many classes are derived (except the ones you make yourself) All classes inherit from TObject. The following definitions are equal: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyClass = class  end;   TMyClass = class(TObject)  end;  Bart ---------------------------------------------------------------------- marcov · 2017-04-22 17:35 Quote from: yurkad on April 22, 2017, 03:53:12 pmFor duplicate it's used method Clone (http://stackoverflow.com/questions/4041760/correct-way-to-duplicate- delphi-object), I always use this method if I can. (  ) ---------------------------------------------------------------------- yurkad · 2017-04-22 22:59 jmm72,  Bart,  marcov many thanks! jmm72, All information is very useful for me. I understood almost all. But I have some question. Quote from: jmm72 on April 22, 2017, 07:02:21 pmFor question 7: here you're mixing things as I've said before. It's not clear if you want to duplicate a class, or create a new instance of an already created instance which is an exact duplicate. I see difference between "duplicate a class" and "create a new instance of an already created instance" by following mode: Sample: We have codeCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Type  TSomeClass = class(TObject)  ...  end;  TNewSomeClass = class(TSomeClass)end; Var  SomeClass:TSomeClass;  NewSomeClass:TNewSomeClass; Here TSomeClass and TNewSomeClass haven same content = same struct = same definition. Neither has values of RunTime. During RunTime SomeClass and NewSomeClass are independent and can have different values of fields and properties. Definitions are same but values are different. But using TPersistent/Assign in RunTime applicated to variable SomeClass we can have another class NewSomeClass2 with same definition and same values. Same values directly after assign. After this each class will have his own life and values can be different. Is it Thus? ---------------------------------------------------------------------- jmm72 · 2017-04-23 20:00 Quote from: Bart on April 22, 2017, 07:34:07 pmAll classes inherit from TObject. Ahhh I knew something was amiss in my explanation but couldn't identify it. Well I hope I can learn (again) from my own errors; all this iloveprogramming/ihateprogramming makes me forget even the most basic things. Although my last hiatus was not for hating programming, but for lack of time and energy. Thanks for the correction.