VB Chapter 12
VB Chapter 12
Scrollable Controls
Topics
•Classes
•Abstract Data Types
•Objects, Properties, Methods
•Exceptions
•Collections
•Object Browser
•Scrollable Controls
Abstract Data Types
• An abstract data type (ADT) is a data type
created by a programmer
• ADTs are important in computer science and
object-oriented programming
• An abstraction is a model of something that
includes only its general characteristics
• Dog is an abstraction
– Defines a general type of animal but not a specific
breed, color, or size
– A dog is like a data type
– A specific dog is an instance of the data type
Classes
• A class is a program structure that defines an
abstract data type
– First create the class
– Then create instances of the class
• Class instances share common attributes
• VB forms and controls are classes
– Each control in the toolbox represents a class
– Placing a button
on a form creates
an instance, or
object, of the class
Class Properties, Methods, & Events
Sub CreateStudent()
Dim sophomore As Student
sophomore = New Student()
sophomore.FirstName = "Travis"
sophomore.LastName = "Barnes"
sophomore.IdNumber = "17H495"
sophomore.TestAverage = 94.7
g_studentVar = sophomore
End Sub
' Constructor
Public Sub New()
strFirstName = "(unknown)"
strLastName = "(unknown)"
strId = "(unknown)"
sngTestAvg = 0.0
End Sub
‘ The rest of this class is omitted.
End Class
Finalizers
• VB provides a class method named Finalize
• Called automatically just before garbage collector
removes an instance of a class
• Select Finalize from method name drop-down list to
let Visual Basic create the following template
Protected Overrides Sub
Finalize()
MyBase.Finalize()
End Sub
' Or alternatively
customers.Add(custData,,,3)
Add Method Exceptions
Dim s as Student
s = CType(studentCollection.Item("49812"), Student)
s.LastName = "Griffin"
For Each Loop with a Collection