Download presentation
Presentation is loading. Please wait.
Published byHarjanti Susanto Modified over 5 years ago
1
CIS16 Application Development and Programming using Visual Basic.net
Chapter ten classes
2
Object-Oriented Programming Terminology
Object-oriented programming language Uses objects to accomplish a program’s goal an object is anything that can be seen, touched, or used. In other words, an object is nearly any thing Class A pattern or blueprint for an object Instance An object created from a class Instantiated The process of creating an object from a class Attributes Characteristics that describe an object
3
Object-Oriented Programming Terminology (cont.)
Behaviors Methods and events that define how the object will act or react Methods Operations (actions) that an object is capable of performing Events Actions to which an object can respond Encapsulation To enclose in a capsule A class encapsulates all attributes and behaviors of an object it instantiates
4
Creating a Class Two types of classes used in VB applications:
Built-in classes, such as the TextBox class Programmer-defined classes Class statement Used to define a class Defines attributes and behaviors of objects created from the class After a class has been defined, it can be used to instantiate objects
5
Creating a Class (cont.)
6
Creating a Class (cont.)
7
Class Attributes - Variables
Variables and properties both represent values that you can access Variables A variable corresponds directly to a memory location. When defined outside of a procedure are referred to as member variables Two types: PUBLIC & PRIVATE Private member variables will not be visible to other programs in the application Can only be used within the class they are defined in Public member variables will be visible to the application They will be exposed to it
8
Class Attributes - properties
A property is a data element defined in the class It has a code block, called a property procedure, written between a Property and End Property statement The code block contains a Get procedure, a Set procedure, or both In addition to retrieving or storing the property's value, a Property Procedure can also perform custom actions, such as updating an access counter.
10
Public Property Procedures
A Public Property procedure allows an application to refer to a Private member variable in a class. Property procedures contain blocks of code: Get block: Retrieves the contents of the Private variable Set block: Assigns a value to the Private variable Blocks may be used individually or together
11
Using Public Properties in the VB program
Using Getters: The Class property's Get procedure retrieves the value - do not explicitly call it by name Use the property name following the equal (=) sign in an assignment statement Dim ThisMoment As Date ThisMoment = TimeOfDay Using Setters: The Class property's Set procedure stores a value - do not explicitly call it by name. Use the property name on the left side of an assignment statement. TimeOfDay = #12:00:00 PM#
12
Class Constructors Constructor
A class method whose purpose is to initialize the class’s Private variables Processed each time an object is created Must be coded as a Sub procedure named New A class can have more than one constructor The names are the same, but the parameter Lists (signatures) must differ A parameterized constructor is simply a constructor that has parameters A parameterized constructor allows an application to specify the object’s initial values Default constructor A constructor without parameters
13
Class Constructors Parameterized constructors are called Overloaded Constructors
14
Overloaded Methods Overloaded methods
Methods with the same name but different parameters
15
Auto-Implemented Properties
Auto-implemented properties enables you to specify the property of a class in one line of code Visual Basic automatically creates a hidden Private variable that it associates with the property It also automatically creates hidden Get and Set blocks It provides a shorter syntax to use when creating a class
16
Auto-Implemented Properties (cont.)
Property Prop2 As String = "Empty" Private _Prop2 As String = "Empty“ Public Property Prop2 As String Get Return _Prop2 End Get Set(ByVal value As String) _Prop2 = value End Set End Property
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.