Download presentation
Presentation is loading. Please wait.
Published byChad Burns Modified over 9 years ago
1
1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto
2
2 CheckBox Allows multiple choices which are not mutually exclusive as opposed to OptionButtons for which making one choice excludes the others There are associated constants called vbChecked and vbUnchecked
3
3 Example: State Tax CheckBox
4
4
5
5 Example: State Tax CheckBox (Code) Private Sub cmdOK_Click() Dim Amount As Currency Amount = txtSubtotal.Text If chkPA.value = vbChecked Then ‘note the `vbChecked constant txtTotal.Text = 1.06 * Amount Else txtTotal.Text = Amount End If End Sub
6
6 Motivation for objects Recall our motivation for writing subroutines and functions was that they represented a convenient unit of programming. One could take the “divide and conquer” approach, that is, break the problem into pieces and solve each piece Objects represent another unit of programming, one that is higher up in the hierarchy
7
7 An object is … An object is a collection of associated variables (called the properties) and subroutines/functions (called the methods) An employee object might have properties –Name, Soc_Sec_Num, Hourly_Wage, etc. An employee method might have methods –Hire, Give_Raise, Calculate_Weekly_Salary, etc.
8
8 Code Maintenance and Reuse Code maintenance: a program will consist of various objects, but only a select few will be involved in any single update Code reuse: a well chosen object may be useful in many programs or in many aspects of a large program –For example, the VB controls are objects that we are always reusing
9
9 Information Hiding “The process of hiding details of an object or function. Information hiding is a powerful programming technique because it reduces complexity …. The programmer can then focus on the new object without worrying about the hidden details.” “Information hiding is also used to prevent programmers from changing — intentionally or unintentionally — parts of a program.” (http://www.webopedia.com)
10
10 Black box The corresponding idea in electronics (hardware) is known as a “black box” “any unit that forms part of an electronic circuit and that has its function, but not its components, specified.” (Webster’s New Universal Unabridged Dictionary, 1996) The box is “black”, that is, while one can see what goes into it and what comes out of it, one cannot see what is going on inside of it
11
11 Multiple programmers Most code is written by teams of coders One should be able to use an object without detailed knowledge of how it works (its implementation) –For example, we do not know the details of a ListBox’s AddItem method, but we have used it –If the code had been available one might have tried to “fix” it when it was actually something else that was broken in a program
12
12 Accessing properties and methods The controls in VB are objects, so we have already seen how to access the properties and methods of objects Properties: –lstExercises.Text, lstExercises.ListCount, lstExercises.Left Methods –lstExercises.SetFocus, lstExercises.Clear, lstExercises.AddItem
13
13 A class of our own Now we want to learn to write and use our own objects Actually one does not write the code for an object, one writes the code for a class and then “instantiates” the object
14
14 Classes and Objects A class is an abstract category of or template for objects It collects the characteristics (properties) and actions (methods) common to the objects that belong to it An object is a specific member of the class When a specific object is made from the abstract class template, it is said to be an instantiation of the class Dragging a CommandButton icon onto a form instantiates a CommanButton object
15
15 Example Dog would be a class –It has properties, like breed, height, weight, etc. –It has methods, like barks, eats, runs, etc. Lassie is an object –Lassie has specific properties Her breed is Collie; her height is 36 inches; her weight is 90 pounds; etc. –The collection of specific properties is referred to as the state of the object
16
16 Property A property is like a variable; however, altering or accessing its value is usually done indirectly using a “guard” method (gets and lets) The CommandButton object has –Height –Width –Position (left and top) –Caption –Etc.
17
17 Private Property The properties are declared as “private,” e.g. Dim mWage As Double as opposed to “public” Public Wage As Double One then accesses the properties through lets (for changing the value, a.k.a. setting the value) and gets (for obtaining the value)
18
18 Lets and gets One can include some validation in a let method to prevent the user of your class from assigning a bad value to a property –(Remember if you write a useful class, you will not be its sole user) Some properties may be totally internal, then there’s no reason to have a let or get for such a function
19
19 Method Just as properties are variables tied to an object, methods are functions or subroutines tied to an object Methods are executed when an object receives a message (a “call” plus any arguments) The code for the class will be in a separate module from the one in which the object is instantiated, therefore methods must be public (unless they are totally internal methods the outside world doesn’t need to see)
20
20 Depreciation Example Our depreciation programs from early on may just be part of an accounting package Let’s turn the code for depreciating an item into an object It will have properties such as –Name, current_value, original_value, etc It will have methods like –Depreciate, Print_Info, etc.
21
21 Adding a class module
22
22 Adding a class module
23
23 Naming your class
24
24 Adding lets and gets
25
25 Writing lets and gets
26
26 Writing lets and gets The property is private (Dim not Public) The method accessing the property is public
27
27 Writing lets and gets Change these from variant
28
28 Writing lets and gets Public Property Get ItemName() As String ItemName = mItemName End Property Public Property Let ItemName(ByVal _ vName As _ String) mItemName = vName End Property ‘This is a simple let with no validation
29
29 Writing sets and gets Public Property Get LifeTime() As Integer LifeTime = mLifeTime End Property Public Property Let LifeTime(ByVal vLifeTime As Integer) If vLifeTime > 0 Then mLifeTime = vLifeTime Else MsgBox ("The lifetime must be positive.") mLifeTime = 100 'some default value for lifetime End If End Property ‘some validation
30
30 The constructor A common method to write for a class is called the constructor Instead of setting properties individually through the let methods, one sets a number of properties at once
31
31 Constructor Public Sub ConstructAccountingItem(ByVal vName As _ String, ByVal vLifeTime As Integer, ByVal vAge As _ Integer, ByVal vOriginalValue As Double, ByVal _ vCurrentValue As Double, ByVal vDepMethod As _ String) mItemName = vName mLifeTime = vLifeTime mItemAge = vAge mDepreciationMethod = vDepMethod mOriginalValue = vOriginalValue mCurrentValue = vCurrentValue End Sub
32
32 Constructor (another approach) Public Sub ConstructAccountingItem(ByVal vName As _ String, ByVal vLifeTime As Integer, ByVal vAge As _ Integer, ByVal vOriginalValue As Double, ByVal _ vCurrentValue As Double, ByVal vDepMethod As _ String) Me.ItemName = vName Me.LifeTime = vLifeTime Me.ItemAge = vAge Me.DepreciationMethod = vDepMethod Me.OriginalValue = vOriginalValue Me.CurrentValue = vCurrentValue End Sub “Me” refers to the current object This approach uses the lets which have the validation code
33
33 Another Constructor Public Sub ContructNewAccountingItem(ByVal _ vName As String, ByVal vLifeTime As Integer, _ ByVal vOriginalValue As Double, ByVal _ vDepMethod As String) Call ConstructAccountingItem(vName, vLifeTime, _ 0, vOriginalValue, vOriginalValue, vDepMethod) End Sub New items have an age of 0 New items’ current values equal their original values
34
34 The ToString method Another common method to write for a method is the ToString method It returns a string with information about the “state” of the object A user could utilize the get methods and construct his or her own string, but it’s more convenient if the object’s programmer provides a ToString method
35
35 ToString
36
36 ToString Method Public Function ToString() As String ToString = mItemName & ": Orig. Val. = " _ & Format$(mOriginalValue, "Currency") _ & " Curr. Val. = " & _ Format$(mCurrentValue, "Currency") End Function
37
37 The Depreciation Method Public Sub Depreciate() If mItemAge < mLifeTime Then Select Case LCase(mDepreciationMethod) Case "straight line" mCurrentValue = mCurrentValue - _ mOriginalValue / mLifeTime Case "double declining" mCurrentValue = mCurrentValue - _ mCurrentValue * 2 / mLifeTime
38
38 The Depreciation Method (Cont.) Case "sum of the years' digits" mCurrentValue = mCurrentValue - _ mOriginalValue * (mLifeTime - _ mItemAge) / (mLifeTime * _ (mLifeTime + 1) / 2) Case Else MsgBox ("Error in Depreciation Method") End Select End If mItemAge = mItemAge + 1 End Sub
39
39 Using a class The first step toward using this Accounting_Item code is to instantiate an object of the class Use the keyword “new” when declaring the object
40
40 Instantiation! Use the keyword “new” when instantiating
41
41 Using the methods Just as with the VB controls, to access a property or method, one types the name of the object and then a dot (.) And just as with VB controls a drop-down list of properties and methods appears If the list does not appear something is wrong
42
42 Drop-down boxes
43
43 Depreciation form
44
44 Depreciation form
45
45 Depreciation form (Code) Private Sub cmdCalculate_Click() Dim myItem As New AccountingItem Dim i As Integer Call myItem.ContructNewAccountingItem( _ txtItemName.Text, txtLifeTime.Text, _ txtOriginalValue.Text, myDepMethod) lstDepResults.Clear instantiate Call the constructor
46
46 Depreciation form (Code) For i = 1 To myItem.LifeTime Call myItem.Depreciate lstDepResults.AddItem (i & vbTab _ & myItem.ToString) Next i End Sub The lifetime property, implicitly uses the get method
47
47 References http://catalog.com/softinfo/objects.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.