Download presentation
Presentation is loading. Please wait.
Published byJuliana Patrick Modified over 9 years ago
1
CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011
2
Objectives In this chapter, you will –Learn some examples of classes and objects Constructors Initializers Properties Shared class members 2
3
Recall: The Syntax for a Class Public Class Test '-----Variables '-----Methods '-----Properties '-----Events End Class 3
4
Classes Constructor Destructor Properties Methods 4
5
Constructor A constructor is a special member function whose task is to initialize the objects of its class A constructor call is required for every object that is created 5
6
Constructor (cont'd) Constructor must be named New Constructor are subroutines (Sub), as they cannot return values Default constructor –If you do not specify any constructor, the compiler provides one –The default constructor has no parameters –If you provide any constructors for a class, then the compiler will not provide a default constructor 6
7
Example of Constructor Public Sub New (ByVal initialBalance As Decimal) Public Sub New (Optional ByVal initialBalance As Decimal = 0D) 7
8
Example 9.2: Account.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.html Constructor –Exception Throw New ArgumentOutOfRangeException(“...”) 8
9
Property Values Get and Set Get allows you read the current property value Set allows you to change the property value Property without a "ReadOnly" or "WriteOnly" specifier must provide both a "Get" and a "Set" 9
10
Example 9.11: Time.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_20 10/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_htp_20 10/codeexamples.html Properties –Public Property Hour() As Integer –Set accessor can check the value of input –"Hour" can be directly used Methods –Overloaded constructors / functions –E.g. ToString() 10
11
Overloaded Constructors Dim time1 As New Time() Dim time2 As New Time(2) Dim time3 As New Time(21, 34) Dim time4 As New Time(12, 25, 42) Dim time5 As New Time(time4) 'copy another Time object 11
12
Example 9.11: Time.vb (cont'd) When one object of a class has a reference to another object of the same class –The first object can access all of the second object’s data, method, and properties, including those that are Private –E.g., In the constructor New(ByVal t As Time): SetTime(t.hourValue, t.minuteValue, t.secondValue) hourValue, minuteValue, and secondValue are private member of class Time 12
13
With Statement With statement –Make multiple references to the same object in a concise manner –E.g., an object called time time.Hour, time.Minute, time.Second With time outputText1.Text=Convert.ToString(.Hour) outputText2.Text=Convert.ToString(.Minute) outputText3.Text=Convert.ToString(.Second) End With 13
14
Object Initializer Keyword: With Dim timeObject1 As New Time() With {.Minute = 33,.Second = 12} 14
15
Auto-Implemented Properties Public Property Hour As Integer –Visual Basic 2010 only (new feature) –The complier would generate a Private instance variable of type Integer named _Hour Public Property Hour As Integer Get Return _Hour End Get Set(ByVal value As Integer) _Hour = value End Set End Property 15
16
Using Me to Access the Current Object Me reference –The instance variable is sometimes "hidden" by some parameters or local variables with the same name –To refer instance variable, use Me Me.hour = hour Me.minute = minute 16
17
Garbage Collection Using … End Using statement –Using fileChooser As New SaveFileDialog() result=fileChooser.ShowDialog() fileName=fileChooser.Filename End Using 17
18
Example 9.13: Shared Member Class All objects of the class share the same value –E.g., counter in Employee object –Private Shared counter As Integer 'Employee objects Shared properties –No need to create an Employee object to call the Get method of a Shared property 18
19
Exercises Properties can contain both ______accessors. –1. Return and Value 2. Get and Value 3. Get and Set 4. Set and Return Keyword _____introduces a class definition. –1. NewClass 2. ClassDef 3. VBClass 4. Class The_____is used to retrieve the value of an instance variable –1. Get accessor of a property 2. retrieve method of a class 3. Client method of a class 4. Set accessor of a property A class can yield many _____, just as a primitive data type can yield many values –1. names 2. objects (instances) 3. values 4. types 19
20
Exercises (cont'd) Instance variables declared Private are not accessible_____. –1. outside the class 2. by other methods of the same class 3. inside the same class 4. All of the above. A class definition ends with the keyword(s)__. –1. Class End 2. End Class 3. EndClass 4. End Variables can be initialized ____. –1. when they are declared 2. to their default values 3. in a constructor 4. All of the above A(n) ____can ensure that a value is appropriate for a data member before the data member is assigned that value. –1. Get accessor 2. Access accessor 3. Modify accessor 4. Set accessor 20
21
21
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.