Not sure what it does about nested classes. Rob It does the obvious: 'TOuter. Documentation says that the keyword is should be used instead of comparing by ClassType. Most of the time, you want to check if an object is assignment-compatible with a specific class, and is will do that.
But in this particular case, that's not what the OP is looking for, and comparing ClassType is. What you are describing is a direct violation of the Liskov substitution principle which states that: objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program I think you should explain what problem you are attempting to solve and then a better approach might become apparent.
David Heffernan David Heffernan k 40 40 gold badges silver badges bronze badges. This was the nudge that i needed, — Christopher Chase. Uwe Raabe Uwe Raabe I think ive solved what i was trying to do, It hit me on the head last night. There is a reserved word IS to check whether a instance is a type or a child of a type.
Robson Benedito Robson Benedito 75 4 4 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
There is a lot more to say about methods. Here are some short notes about the features available in Delphi:. Delphi supports method overloading. This means you can have two methods with the same name, provided that you mark the methods with the overload keyword and that the parameter lists of the two methods are sufficiently different. By checking the parameters, the compiler can determine which version you want to call. Methods can have one or more parameters with default values. If these parameters are omitted in the method call, they will be assigned the default value.
Within a method, you can use the Self keyword to access the current object. When you refer to local data of the object, the reference to self is implicit. For example, in the SetValue method of the TDate class listed earlier, you use Month to refer to a field of the current object, and the compiler translates Month into Self. You can define class methods, marked by the class keyword. A class method doesn't have an object instance to act upon, because it can be applied to an object of the class or to the class as a whole.
Delphi doesn't currently have a way to define class data, but you can mimic this functionality by adding global data in the implementation portion of the unit defining the class. By default, methods use the register calling convention: simple parameters and return values are passed from the calling code to the function and back using CPU registers, instead of the stack.
This process makes method calls much faster. To emphasize the fact that Delphi components aren't much different from other objects and also to demonstrate the use of the Self keyword , I've written the CreateComps example.
This program has a form with no components and a handler for its OnMouseDown event, which I've chosen because it receives as a parameter the position of the mouse click unlike the OnClick event. I need this information to create a button component in that position. Here is the method's code:. The effect of this code is to create buttons at mouse-click positions, as you can see in Figure 2. In the code, notice in particular the use of the Self keyword as both the parameter of the Create method to specify the component's owner and the value of the Parent property.
I'll discuss these two elements ownership and the Parent property in Chapter 4, "Core Library Classes. When writing code like this, you might be tempted to use the Form1 variable instead of Self.
In this specific example, that change wouldn't make any practical difference; but if there are multiple instances of a form, using Form1 would be an error. In fact, if the Form1 variable refers to the first form of that type being created, then by clicking in another form of the same type, the new button will always be displayed in the first form.
The button's Owner and Parent will be Form1 , not the form the user has clicked. In general, referring to a particular instance of a class when the current object is required is bad OOP practice.
Toggle navigation. See also. Home Programming Mastering Delphi 7. Classes and Objects Delphi is based on OOP concepts, and in particular on the definition of new class types. Note The terms class and object are commonly used and often misused, so let's be sure we agree on their definitions. In this case, you prefix each method name with the name of the class it belongs to, using dot notation: procedure TDate. SetValue 1, 1, ; if ADay.
Free; end; end; Notice that ADay. More on Methods There is a lot more to say about methods. Here are some short notes about the features available in Delphi: Delphi supports method overloading.
Creating Components Dynamically To emphasize the fact that Delphi components aren't much different from other objects and also to demonstrate the use of the Self keyword , I've written the CreateComps example. Here is the method's code: procedure TForm1. Create Self ; Btn. Remember the name: eTutorials. Mastering Delphi 7. The Structure of the Book. Free Source Code on the Web.
How to Reach the Author. Part I: Foundations. Editions of Delphi. An Overview of the IDE. The Delphi Editor. The Form Designer. Secrets of the Component Palette. Managing Projects. Additional and External Delphi Tools. The Files Produced by the System. The Object Repository. Debugger Updates in Delphi 7. What's Next? Chapter 2: The Delphi Programming Language. Core Language Features. Delphi's Object Reference Model. Inheriting from Existing Types. Late Binding and Polymorphism. Type-Safe Down-Casting.
Connect and share knowledge within a single location that is structured and easy to search. I've added a class variables to the base class of a deep class hierarchy. It's an integer intended to count number of instances created per class type.
But I've run into a problem. I assumed that I can use the class var to store the number of instances created per class , but this does not seem to be the case. Inspecting TBaseClass. I expected fCreated to be maintained per class type, isn't that the point?
What am I missing? You are missing nothing. Your analysis of how class variables work is correct. A class var is nothing more than a global variable that is scoped by the class. The following code uses something like:. Actively scan device characteristics for identification. Use precise geolocation data. Select personalised content.
Create a personalised content profile. Measure ad performance. Select basic ads. Create a personalised ads profile. Select personalised ads. Apply market research to generate audience insights. Measure content performance. Develop and improve products. List of Partners vendors.
0コメント