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

Slides:



Advertisements
Similar presentations
General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual.
Advertisements

OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Classes & Objects Computer Science I Last updated 9/30/10.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Object-Oriented PHP (1)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
VBA Modules, Functions, Variables, and Constants
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
CSI 101 Elements of Computing Spring 2009 Lecture # 14 – Classes and Objects Wednesday, April 15th, 2009 and Monday, April 20th, 2009.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
An Object-Oriented Approach to Programming Logic and Design
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Visual Basic Starter Vocabulary. IDE Integrated Development Environment A setting in which programs are written, tested, debugged, etc. You could write.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Controlling Program Flow with Decision Structures.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
Object Oriented Paradigm OOP’s. Problems with Structured Programming As programs grow ever larger and more complex, even the structured programming approach.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Object-Oriented Programming: Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Classes A class is a blueprint of an object
Using Procedures and Exception Handling
Object-Oriented Programming: Classes and Objects
Defining Classes and Methods
Tonga Institute of Higher Education
If, Subroutines and Functions
Methods.
Presentation transcript:

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

3 Example: State Tax CheckBox

4

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 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 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 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 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.” (

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 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 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 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 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 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 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 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 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 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 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 Adding a class module

22 Adding a class module

23 Naming your class

24 Adding lets and gets

25 Writing lets and gets

26 Writing lets and gets The property is private (Dim not Public) The method accessing the property is public

27 Writing lets and gets Change these from variant

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 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 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 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 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 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 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 ToString

36 ToString Method Public Function ToString() As String ToString = mItemName & ": Orig. Val. = " _ & Format$(mOriginalValue, "Currency") _ & " Curr. Val. = " & _ Format$(mCurrentValue, "Currency") End Function

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 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 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 Instantiation! Use the keyword “new” when instantiating

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 Drop-down boxes

43 Depreciation form

44 Depreciation form

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 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 References