I Wish I Knew How To Begin Object Oriented Programming With Xojo July Edition (1.0) By Eugene Dakin
Table of Contents Chapter 1 - Introduction to Object Oriented Programming... 6 Being the Architect... 7 Classes... 7 Make an Object... 9 Set and Get Object Data... 10 Dot Operator... 12 Chapter 2 Methods... 14 Simple Method... 14 Method Parameter... 15 Return Method Value... 17 Chapter 3 Constructor and Destructor... 19 Constructor... 19 Constructor Parameters... 21 Constructor Overloading... 22 Destructor... 25 Chapter 4 - Encapsulation... 28 Scope... 28 Encapsulated Property... 28 Chapter 5 Inheritance... 31 Inheritance ClsCar... 32 Base or Parent Class... 36 Chapter 6 Polymorphism... 38 Chapter 7 Introspection... 42 Retrieve Object Properties and Values... 42 Copy Data at Runtime... 46 Chapter 8 Extends and Assigns... 49 Extends... 49 Assigns... 52 Chapter 9 - Boxing and Unboxing... 55 Value Type... 55 Reference Type... 55 Implicit... 56 Explicit... 56 4
Boxing... 56 Unboxing... 56 Implicit Boxing... 57 Explicit Boxing... 57 Implicit Unboxing... 58 Explicit Unboxing... 58 Chapter 10 Setters and Getters... 61 Mutator... 65 Accessor... 65 Immutable... 65 Chapter 11 Overloading and Overriding... 66 Overloading... 66 Overriding... 68 Chapter 12 Delegate... 71 Simple Delegate... 71 Delegate Parameter... 73 Delegate Return Value... 75 Abstract Pointer Delegate... 76 Class Abstract Pointer Delegate... 80 Chapter 13 Class Interface... 82 Index... 88 5
Chapter 4 - Encapsulation Encapsulation is a common word when Object Oriented Programming is used. Encapsulation has all of the objects data hidden within the object and can only be accessed through the methods of the class. Here are some of the many benefits to hiding (making properties Private) which allows the validation of changes to the data, protecting the data from unauthorized access, and allows the data to be consistent. Scope With classes there are three types of scope in Xojo: Public, Protected, and Private. Scope is also called the visibility of the entity (eg. Property, Method, etc.). Scope is very important when working with encapsulation. The general definitions of scope are: a) Public All methods, Windows, and Objects can see and likely access the entity (i.e. Property) within a class. b) Protected The class and any subclasses can access and modify the property/method. c) Private Only the class can access and modify the property/method. None of the properties/methods are given access from outside of the program. Encapsulated Property This example will make a property (Speed) which is invisible to the outside program and is only accessible by calling methods in the Class by setting the property to Private. Figure 14. Example 4-1: Encapsulating a Property Screen Grab The object is made and property is set with a private scope, meaning only a class method can change the property value. Lets show how this program is made. 28
The first step is to create a blank desktop application and drag-and-drop a class Figure 15. Example 4-1: Encapsulated Property Screen Grab Class1 was renamed to ClsCar and a property called Speed was added to the car. When the properties scope was changed to Private, then a red dot appears to the right of the property. Figure 16. Adding a Private Class Property A private property can only be changed by the methods within the class. Two methods with public properties were created that can be accessed outside of the class. Function Constructor() Code 24. Example 4-1: Class Constructor Method 29
//Set PRIVATE Default Class Property value Me.Speed = 15 //Car has zero speed End Function Using our knowledge from the previous chapter, a method called a Constructor is added to the class and is automatically called when a new object from a class is created. Code in the Constructor method sets the speed of the object to 15. Another method is created to update the Window label from the class. Code 25. Example 4-1: Class UpdateLabels Method //Show MyFastCar Settings in Window Window1.LblSpeed.Text = Me.Speed.ToText This is the part of the method where the value in the speed variable is shown on a label in the window. To make all of the methods and property work together, the following code is added to the pushbutton action event. Code 26. Example 4-1: Create Encapsulated Object //Create a Fast Car Dim MyFastCar as New ClsCar //Property values are set with the method MyFastCar.UpdateLabels() When the MyFastCar variable is created from the Class ClsCar, then the Constructor fires and places a default value into the Speed property. The Class UpdateLabels method is then called to show the property value on the label in Window1 which has the value of 15. This example shows how to encapsulate (hide) a property so that only methods in the property can change or be updated by public methods in the class. 30
Index Abstract, 76 Abstract Pointer, 76 Abstract Pointer Delegate, 76 abstraction, 76 Accessor, 65 Assigns, 52 Base Class, 36 Boxing, 56 called, 14 CanRead, 44 CanWrite, 44 Class, 7 Class Interface, 82 Constructor, 19 Constructor Super, 34 coupled, 76 Delegate, 71 Delegate Invoke, 73 Delegate Reference, 72 Delegate Reference Pointer, 72 Dot Operator, 12 entity, 28 Examples 01-01 Create Class Properties, 9 01-02 Create Object, 10 01-03 Set Object Propeties, 11 01-04 Dot Operator, 12 02-01 Using a Method, 14 02-02 Method Parameter, 16 02-03 Return Method Value, 17 03-01 Constructor Method, 20 03-01 Object Constructor, 20 03-02 Constructor Parameter, 21 03-03 Constructor Overload, 23 03-03 Constructor Overloading, 23 03-04 Destructor Method, 26 04-01 Encapsulated Property, 30 05-01 Inherited Class, 35 05-02 Class IsA Comparison, 36 06-01 Polymorphic Method, 39 07-01 Introspection Show Properties, 44 07-02 Introspection Copy Object, 46 08-01 Extends Function, 50 08-02 Assigns Keyword, 52 09-01 Boxing and Unboxing, 59 10-01 Use Computed Get and Set, 63 11-01 Method Overloading, 66 11-02 Method Overriding, 69 12-01 Simple Delegate, 73 12-02 Delegate Parameter, 74 12-03 Delegate Return Value, 76 12-04 Abstract Delegate, 79 12-05 Class Abstract Delegate, 81 13-01 Class Interface, 87 88
Explicit, 56 Explicit Boxing, 57 Explicit Unboxing, 58 Extends, 50 Get, 63 GetProperties, 44 Getter Propery, 61 GetTypeInfo, 44 immutable, 65 Implicit, 56 Implicit Boxing, 57 Implicit Unboxing, 58 Inheritance, 31 Instance, 9 Interface Class, 82 Introspection ArrayInfo, 45 AttributeInfo, 45 AttributeInfoImp, 45 ClassInfo, 45 ConstructorInfo, 45 ConstructorInfoImp, 45 EnumInfo, 45 GenericPrimitiveTypeInfo, 45 GetType, 45 MemberInfo, 45 MethodBase, 45 MethodInfo, 45 MethodInfoImp, 45 ObjectClassInfo, 45 ParameterInfo, 45 ParameterInfoImp, 45 PointerTypeInfo, 45 PropertyInfo, 45 PropertyInfoImp, 45 StructureInfo, 45 TypeInfo, 45 VariantInfo, 45 Invoke Delegate, 73 Invoked, 14 IsA, 36, 37 IsPublic, 44 Me, 15 Member Methods, 12 Member Variables, 12 Method Members, 12 Mutator, 65 NaN, 67 Not a Number, 67 Object, 9 Overloading, 22, 66 Overriding, 68 Parameters, 14 Parent Class, 36 pointer, 76 Private, 28 Property 89
Get, 61 Set, 61 PropertyInfo, 44 Protected, 28 Public, 28 Reference Delegate, 72 Reference Type, 55 Return, 14 Return Type, 14 Scope, 28 Private, 28 Protected, 28 Public, 28 Set, 63 Set Object Property, 10 Setter Property, 61 Super, 31 Super.Constructor, 34 TypeInfo, 44 Unboxing, 56 uncoupling, 76 Value Type, 55 Variable Members, 12 90
The I Wish I Knew series contains technical data and advice that makes sense and contains practical and numerous examples with explanations to allow you to ease into the steep programming curve. You can extend Xojo applications today! This book I Wish I Knew How to Begin Object Oriented Programming with Xojo shows you how to program with object in the three major operating systems. All code has been tested on Xojo r2.2 with Windows 8.1, OS X Yosemite 10.10.4, and Ubuntu 15.04 32-bit versions. The book is written as a guide and reference to Xojo programmers who program Desktop Applications. There are 12 chapters and contains over 90 pages with 29 example programs. Examples include topics such as Polymorphism, Introspection, Extends, Explicit Unboxing, Overriding, and definitions of common terms used in OOP. Many screenshots have been added to show the results of the code with an index to help find topics quickly. This is one of many books at Great White Software. This book can be purchased at http://greatwhite-software.com/rblibrary/ where many great Xojo resources are available. Happy programming! Eugene Eugene Dakin MBA, Ph.D., P.Chem., is an author of Xojo and Real Studio reference materials and has many years of experience in the programming industry. Another great reference book is I Wish I Knew How To Program Win32 Declares for Windows. ISBN: 978-1-927924-12-9 91