Chapter 12 Classes, Exceptions, Collections, and Scrollable Controls Starting Out with Visual Basic .NET 2nd Edition 12.1 Introduction Starting Out with Visual Basic .NET 2nd Edition Chapter 12 Topics • Classes • Abstract Data Types • Objects, Properties, Methods • • • • Exceptions Collections Object Browser Scrollable Controls Starting Out with Visual Basic .NET 2nd Edition 12.2 Classes and Objects Classes Are Program Structures That Define Abstract Data Types and That Are Used to Create Objects Starting Out with Visual Basic .NET 2nd Edition Abstract Data Types • An abstract data type (ADT), is a data type created by a programmer • ADTs are very important in computer science and especially significant in objectoriented programming • An abstraction is a general model of something—a definition that includes only the general characteristics of an object. Starting Out with Visual Basic .NET 2nd Edition Classes • A class is a program structure that defines an abstract data type • First create a class then create instances of the class • All class instances share common characteristics, for example: • Visual Basic .NET controls and forms are classes • In the toolbox, each icon represents a class • When you select the button tool from the toolbox and place it on a form you are creating an instance of the button class • An instance is also called an object Starting Out with Visual Basic .NET 2nd Edition Class Properties, Methods, and Event Procedures • Class properties example: Buttons have Location, Text, and Name properties • Class methods example: Buttons have a Focus method • Class event procedures: Buttons can have a click event procedure Starting Out with Visual Basic .NET 2nd Edition Object Oriented Design • This involves taking the program specifications and doing an analysis to determine what ADTs would be best to use to implement those specifications • In general, the classes will be the nouns Starting Out with Visual Basic .NET 2nd Edition Object Oriented Design Example Specifications: We need to keep a list of students that lets us track the courses they have completed. Each student has a transcript that contains all information about his or her completed courses. At the end of each semester, we will calculate the grade point average of each student . At times, users will search for a particular course taken by a student. Starting Out with Visual Basic .NET 2nd Edition OOD Class Characteristics Class Attributes(properties) Operations (methods) Student LastName, FirstName, IdNumber Display, Input StudentList AllStudents, Count Add, Remove, FindStudent Course Semester, Name, Grade,Credits Display, Input Transcript CourseList, Count Display, Search, CalcGradeAvg Starting Out with Visual Basic .NET 2nd Edition Interface and Implementation • The class interface is the portion of the class that is visible to the application programmer • The class implementation is the portion of a class that is hidden from client programs; it is created from private member variables, private properties, and private methods Starting Out with Visual Basic .NET 2nd Edition 12.4 Exceptions and Error Handling Exceptions Signal Errors or Unexpected Events That Occur While an Application Is Running Starting Out with Visual Basic .NET 2nd Edition Run-Time Errors • Examples: • Attempted division by zero • Attempting to open a non-existent file • Visual Basic .NET will display an error message Starting Out with Visual Basic .NET 2nd Edition Exceptions • Run-time errors are caused by exceptions • An exception is an event or condition that happens unexpectedly, and causes the application to halt • These may be caught by an error handler • The process of intercepting and responding to exceptions is called error trapping Starting Out with Visual Basic .NET 2nd Edition Handling an Exception Try TryBlock Catch CatchBlock End Try • The TryBlock is code that • is always executed • may cause an exception • The CatchBlock code is executed if there is an exception in the associated TryBlock Starting Out with Visual Basic .NET 2nd Edition Try/Catch Examples Try result = num1 / num2 Catch MessageBox.Show("An error has occurred.") End Try Dim inputFile As System.IO.StreamReader Try inputFile = System.IO.File.OpenText("names.txt") Catch MessageBox.Show("An error has occurred.") End Try Starting Out with Visual Basic .NET 2nd Edition Some Conditions That Cause Exceptions From the OpenText Method • The file is not found • The path does not exist • The application does not have permission to open the file • An empty string, or the value Nothing, was passed as an argument to the method Starting Out with Visual Basic .NET 2nd Edition Handling Specific Exceptions Try inputFile = System.IO.File.OpenText("globby.txt") Catch ex As FileNotFoundException MessageBox.Show("File not found.") Catch ex As DirectoryNotFoundException MessageBox.Show("Directory not found.") End Try Starting Out with Visual Basic .NET 2nd Edition Using System-Defined Error Messages Try inputFile = System.IO.File.OpenText("globby.txt") Catch ex As FileNotFoundException ' ex is the Exception Variable MessageBox.Show(ex.Message) Catch ex As DirectoryNotFoundException ' ex is the Exception Variable MessageBox.Show(ex.Message) End Try Starting Out with Visual Basic .NET 2nd Edition Generic Exception Type 'Exception' Usage Function OpenFileForReading(ByVal filename As String) _ As System.IO.StreamReader Dim fileObject As System.IO.StreamReader Try fileObject = System.IO.File.OpenText(filename) Catch ex As Exception ' Generic Exception MessageBox.Show(ex.Message, "Error") fileObject = Nothing End Try Return fileObject End Function Starting Out with Visual Basic .NET 2nd Edition The Try…Finally Statement Try TryBlock Catch [ExceptionVar As ExceptionType] CatchBlock Finally FinallyBlock End Try • The FinallyBlock is executed • After the Try block • After any Catch blocks have executed Starting Out with Visual Basic .NET 2nd Edition Try…Finally Example Sub ReadEntireFile(ByVal filename As String, _ ByRef fileContents As String) ' Reads the entire contents of a file ' into a string. Dim fileObject As System.IO.StreamReader fileObject = OpenFileForReading(filename) If Not (fileObject Is Nothing) Then Try fileContents = fileObject.ReadToEnd Catch ex As Exception MessageBox.Show(ex.Message) fileContents = Nothing Finally fileObject.Close() End Try End If End Sub Starting Out with Visual Basic .NET 2nd Edition 12.8 Scroll Bars and Track Bars The HScrollBar, VScrollBar, and TrackBar Controls Provide a Graphical Way to Adjust a Number Within a Range of Values Starting Out with Visual Basic .NET 2nd Edition Visual Appearance • The HScrollBar and VScrollBar look like normal scroll bars • The TrackBar has an arrow pointer as the slider and has tick marks along its length Starting Out with Visual Basic .NET 2nd Edition Bar Properties • • • • Minimum - bar's minimum value Maximum - bar's maximum value Value - bar's current value LargeChange - increment (decrement) for a mouse click on or near the slider • SmallChange - increment (decrement) for a mouse clicl on an arrow at the end • TickFrequency - (TrackBar only) distance between the tick marks Starting Out with Visual Basic .NET 2nd Edition