+ All Categories
Home > Documents > 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

Date post: 30-Dec-2015
Category:
Upload: chad-burns
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
47
1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto
Transcript
Page 1: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

1

Visual Basic

Checkboxes

Objects and Classes

Chapt. 16 in Deitel, Deitel and Nieto

Page 2: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 3: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

3

Example: State Tax CheckBox

Page 4: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

4

Example: State Tax CheckBox

Page 5: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 6: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 7: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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.

Page 8: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 9: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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)

Page 10: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 11: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 12: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 13: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 14: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 15: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 16: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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.

Page 17: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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)

Page 18: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 19: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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)

Page 20: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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.

Page 21: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

21

Adding a class module

Page 22: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

22

Adding a class module

Page 23: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

23

Naming your class

Page 24: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

24

Adding lets and gets

Page 25: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

25

Writing lets and gets

Page 26: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

26

Writing lets and gets

The property is private(Dim not Public)

The method accessing the property is public

Page 27: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

27

Writing lets and gets

Change these from variant

Page 28: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

28

Writing lets and gets

Public Property Get ItemName() As String ItemName = mItemNameEnd Property

Public Property Let ItemName(ByVal _ vName As _ String)

mItemName = vNameEnd Property‘This is a simple let with no validation

Page 29: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

29

Writing sets and getsPublic Property Get LifeTime() As Integer LifeTime = mLifeTimeEnd 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 IfEnd Property

‘some validation

Page 30: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 31: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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 = vCurrentValueEnd Sub

Page 32: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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 = vCurrentValueEnd Sub

“Me” refers to the current object

This approach uses the lets which have the validation code

Page 33: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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 SubNew items have an age of 0

New items’ current values equal their original values

Page 34: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 35: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

35

ToString

Page 36: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

36

ToString Method

Public Function ToString() As String

ToString = mItemName & ": Orig. Val. = " _ & Format$(mOriginalValue, "Currency") _

& " Curr. Val. = " & _ Format$(mCurrentValue, "Currency")

End Function

Page 37: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 38: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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 + 1End Sub

Page 39: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 40: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

40

Instantiation!

Use the keyword “new” when instantiating

Page 41: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 42: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

42

Drop-down boxes

Page 43: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

43

Depreciation form

Page 44: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

44

Depreciation form

Page 45: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 46: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

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

Page 47: 1 Visual Basic Checkboxes Objects and Classes Chapt. 16 in Deitel, Deitel and Nieto.

47

References

• http://catalog.com/softinfo/objects.html


Recommended