210 likes | 229 Views
CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects. UTPA – Fall 2011. Objectives. In this chapter, you will Learn some examples of classes and objects Constructors Initializers Properties Shared class members. Recall: The Syntax for a Class.
E N D
CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011
Objectives • In this chapter, you will • Learn some examples of classes and objects • Constructors • Initializers • Properties • Shared class members
Recall: The Syntax for a Class • Public Class Test'-----Variables '-----Methods '-----Properties '-----EventsEnd Class
Classes • Constructor • Destructor • Properties • Methods
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
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
Example of Constructor • Public Sub New (ByVal initialBalance As Decimal) • Public Sub New (Optional ByVal initialBalance As Decimal = 0D)
Example 9.2: Account.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html • Constructor • Exception • Throw New ArgumentOutOfRangeException(“...”)
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"
Example 9.11: Time.vb • URL: • http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html • Properties • PublicProperty Hour() AsInteger • Set accessor can check the value of input • "Hour" can be directly used • Methods • Overloaded constructors / functions • E.g. ToString()
Overloaded Constructors • Dim time1 AsNew Time() • Dim time2 AsNew Time(2) • Dim time3 AsNew Time(21, 34) • Dim time4 AsNew Time(12, 25, 42) • Dim time5 AsNew Time(time4) 'copy another Time object
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
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
Object Initializer • Keyword: With • Dim timeObject1 AsNew Time() With {.Minute = 33, .Second = 12}
Auto-Implemented Properties • PublicProperty 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 IntegerGetReturn _HourEnd GetSet(ByVal value As Integer) _Hour = valueEnd SetEnd Property
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
Garbage Collection • Using … End Using statement • Using fileChooser AsNew SaveFileDialog() result=fileChooser.ShowDialog() fileName=fileChooser.Filename EndUsing
Example 9.13: Shared Member Class • All objects of the class share the same value • E.g., counter in Employee object • PrivateShared counter AsInteger 'Employee objects • Shared properties • No need to create an Employee object to call the Get method of a Shared property
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
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