Lesson 8 Classes and Objects Microsoft Visual Basic
Lesson 8 — Classes and Objects Microsoft Visual Basic. NET, Introduction to Programming 1
Objectives l l 2 Use the Object Browser to explore classes and objects. Design and create classes. Use class modules to declare and use objects. Add methods, events, and properties to classes.
Vocabulary l l l 3 Business rules By. Val Data encapsulation data hiding Inheritance Object-oriented programming (OOP) l l l Operator overloading Platforms Polymorphism Reusable code Str. Conv()
Maximum Effect with Minimum Effort l l 4 Every programmer wants to be more productive. "Maximum effect with minimum effort" is an old saying endorsed by every programmer. Programmers want a programming environment that makes it easy to write code. Programmers want their programs to work on several different platforms. Working on different platforms means writing programs that work on different combinations of hardware and operating systems. Programmers want to write reusable code, which is code you can use in more than one program.
Object-Oriented Programming (OOP ) The foundation of OOP is data — data and the operations performed on the data. An object contains both data and the operations that can be performed on the data. For example, a list box is an object. It contains the data in its Items collection and the operations that can be performed on the data. The operations are called methods. 5
Data Encapsulation Data encapsulation means hiding information from the user and the programmer. How does a list box store information in memory? What code does it use to sort its contents? 6 In OOP, these questions are not concerns of the programmer. To communicate with an object, a programmer writes code that sends messages to the objects. The messages instruct the objects what operations to perform on the data they contain. Communication is restricted to the messages, making the internal working of the object hidden from the user and the programmer.
Data Hiding Data hiding allows a programmer to worry about problem solving rather than about writing code to handle the details of storing and operating on data. Objects take care of the details for you. 7
Inheritance When you create a new class based on a simpler existing class, the new class inherits all the properties of the original class, a principle called inheritance. 8 In this new class, you can replace or extend the properties of the original class. The new class may add new methods and properties to the functionality of the old class while retaining all the original class's functionality.
Polymorphism is redefining methods, properties, and operations to behave differently with different objects. 9
Operator Overloading 10 The lowly plus symbol (+) has many functions: It adds integers, decimals, and doubles, and it joins strings together. Each operation calls for different code to execute the operation within the context in which it occurs. Operator overloading is the term that refers to the ability of the existing operators to have more than one function depending on the context.
The Object Browser lets you look at classes and the properties of the objects created from the classes. 11
The Object Browser 12
The Find Symbol Dialog Box 13
Creating a Class The simplest classes are a lot like structures. You use structures to create new data types. You can use classes the same way, but more often, you use classes to create objects of user-defined data types along with the operations you need to process, store, or display the data. The simplest way to define a property in a class is a simple variable declaration. A Public variable declaration, like Public Name As String, in the class definition provides a Name property of type String for any object created with that class definition. 14
Property Procedure Property Name As Data. Type Get Code called when the property is read End Get Set Code called when the data are written to the property End Set End Property 15
Classes Defining public variables in a Class module is equivalent to adding properties to the objects created with the class. Adding Public procedures to a Class module is equivalent to adding methods to the objects created with the class. 16
Note Remember, a method is an action that an object can perform. For example, the list box object can execute the Clear method on its Items collection to clear its contents, or it can execute the Add method to add a new entry to the Items collection of the list box. 17
Computer Ethics Building business rules into class definitions makes it easier to enforce those rules. If every programmer in the shop is required to use the same unmodified class definitions, all are compelled to use the same business rules. 18
Adding an Event l l l 19 First, you must declare an event in the Class definition. Then you must write code in the Class definition to raise the event. Lastly, in the calling program, you must declare object variables using the With. Events keyword and write an event procedure to respond to the event when it occurs.
By. Val Keyword The By. Val keyword in a parameter list sends a parameter's value to the function. Because the function does not know the parameter's address, it cannot change the actual value of the parameter. The value of the parameter can be used in the function. The program can change the value of the variable that represents the parameter, but it cannot change the actual value of the parameter as defined in the calling program. The By. Ref keyword passes the parameter by address. This means the program can make permanent changes to the parameter sent to the procedure. 20
Summary l l l 21 Programmers want better programming environments, crossplatform applications, and reusable code modules. Visual Basic is the root language of many Microsoft products, including Microsoft Office. Object-oriented programming (OOP) focuses on creating objects that include data and the operations performed on the data. Data encapsulation, part of the OOP paradigm, is the ability of an object to hide data and methods from the user or even the programmer. This protects data from accidental alteration. Inheritance is the ability of an OOP class to inherit properties and methods from other classes.
Summary l l l 22 Polymorphism is the ability to define multiple uses for the same operators and methods. You create objects from the templates provided by classes. The Object Browser lets the programmer look at classes and the properties of the object created from the classes. You add class definitions to a project by selecting Project | Add Class from the menu bar. Public variables declared in the Declarations section of the class definition become properties of objects created with the new class.
Summary l l l 23 Private variables declared in the Declarations section of a class definition are available within the definition but cannot be accessed outside the definition. The program uses Property procedures to gather and provide properties to objects. The program also uses the procedures to modify or process the properties. The Str. Conv( ) function can be used to convert an input string to a string where the first character is capitalized and the other characters are lowercase. A new object created from a class is an instance, or an instantiation, of the class. You can save objects in text files by saving each part of the object as text in the file.
Summary l l 24 Public procedures and functions recorded in a class definition become the methods of the objects created with the class. A business rule is a rule of the workplace written into the code of a program. When put into class definitions, business rules are easy to manage and implement. The Selected. Index property of a list box contains the index value of the item currently selected in the list. To add an event to a class module, first declare the event in the Declarations section of the class definition, then write code to raise the event. The code initiates the event when some condition is satisfied (or not satisfied) in the class definition.
Summary l l 25 To use event definitions in a class definition, use the With. Events keyword in an application to declare the objects of that class. The By. Val keyword in a parameter list sends a parameter's value to a function, not the parameter's address. The program uses the Raise. Event statement in a Class definition to fire an event procedure. Class definitions are at their best when used to hide the details of program operations from a programmer and provide the programmer with an easy-to-use interface to the data in the objects created from the class definition.
- Slides: 25