DEVELOPING OBJECT ORIENTED APPLICATIONS

Similar documents
Visual Basic 2008 Anne Boehm

Chapter 12. OOP: Creating Object- Oriented Programs. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

Chapter 2. Building Multitier Programs with Classes The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Advanced Programming Using Visual Basic 2008

Lecture 10 OOP and VB.Net

Learning VB.Net. Tutorial 19 Classes and Inheritance

CALIFORNIA STATE UNIVERSITY, SACRAMENTO College of Business Administration. MIS 15 Introduction to Business Programming. Programming Assignment 3 (P3)

Objectives. After completing this topic, the students will: Understand of the concept of polymorphism Know on How to implement 2 types of polymorphism

Microsoft Visual Basic 2005: Reloaded

IMS1906: Business Software Fundamentals Tutorial exercises Week 5: Variables and Constants

1. Create your First VB.Net Program Hello World

To enter the number in decimals Label 1 To show total. Text:...

I101/B100 Problem Solving with Computers

Tutorial 03 understanding controls : buttons, text boxes

CS708 Lecture Notes. Visual Basic.NET Object-Oriented Programming. Implementing Client/Server Architectures. Part (I of?) (Lecture Notes 5A)

In this tutorial we will create a simple calculator to Add/Subtract/Multiply and Divide two numbers and show a simple message box result.

Introduction. In this preliminary chapter, we introduce a couple of topics we ll be using DEVELOPING CLASSES

Chapter 2 Exploration of a Visual Basic.Net Application

Building Multitier Programs with Classes

Revision for Final Examination (Second Semester) Grade 9

Repetition Structures

Learning VB.Net. Tutorial 17 Classes

Public Class ClassName (naming: CPoint)

Visual Basic 2008 The programming part

Visual Studio.NET for AutoCAD Programmers

CIS Intro to Programming in C#

Unit 4. Lesson 4.1. Managing Data. Data types. Introduction. Data type. Visual Basic 2008 Data types

How to Validate DataGridView Input

A Second Visual BASIC Application : Greetings

Lab 4: Adding a Windows User-Interface

Chapter 12: How to Create and Use Classes

Unit Title: Objects in Visual Basic.NET. Software Development Unit 4. Objects in Visual Basic.NET

C4.3, 4 Lab: Conditionals - Select Statement and Additional Input Controls Solutions

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

Module 6: Fundamentals of Object- Oriented Programming

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Software Design and Analysis for Engineers

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

What is an algorithm?

Introduction to Programming Using Java (98-388)

Herefordshire College of Technology Centre Edexcel BTEC Level 3 Extended Diploma in Information Technology (Assignment 1 of 3)

Final Exam 7:00-10:00pm, April 14, 2008

VB FUNCTIONS AND OPERATORS

What is an algorithm?

Chapter 1 Getting Started

The New Brew-CQ Synchronous Sockets and Threading

VISUAL BASIC PROGRAMMING (44) Technical Task KEY. Regional 2013 TOTAL POINTS (485)

Level 3 Computing Year 2 Lecturer: Phil Smith

FOR 240 Homework Assignment 4 Using DBGridView and Other VB Controls to Manipulate Database Introduction to Computing in Natural Resources

7. Inheritance & Polymorphism. Not reinventing the wheel

IT3101 -Rapid Application Development Second Year- First Semester. Practical 01. Visual Basic.NET Environment.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 1: Object-Oriented Programming Using C++

This PDF was generated in real-time using DynamicPDF; Generator for.net.

Microsoft Visual Basic 2005: Reloaded

Form Adapter Example. DRAFT Document ID : Form_Adapter.PDF Author : Michele Harris Version : 1.1 Date :

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Thou shalt not define an has a relationship. Thou must not require too deep a class hierarchy. Thou must require code re-use from a base class

Copyright 2014 Pearson Education, Inc. Chapter 7. Multiple Forms, Modules, and Menus. Copyright 2014 Pearson Education, Inc.

Review. October 20, 2006

ISM 3253 Exam I Spring 2009

Procedures in Visual Basic

IF ELSE and the Loops like For..NEXT, While..END WHILE are the Control Statements. 5. Write a Simple program to explain the FOR EACH loop in VB.

CS313D: ADVANCED PROGRAMMING LANGUAGE

How to use data sources with databases (part 1)

Programming I. Course 9 Introduction to programming

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Design Of Human Computer Interfaces Assignment 1- Hello World. Compliance Report

What are the characteristics of Object Oriented programming language?

The name of this type library is LabelManager2 with the TK Labeling Interface reference.

Learning VB.Net. Tutorial 10 Collections

CS313D: ADVANCED PROGRAMMING LANGUAGE

by Pearson Education, Inc. All Rights Reserved. 2

Dot and Scope Resolution Operator

Uka Tarsadia University MCA ( 3rd Semester)/M.Sc.(CA) (1st Semester) Course : / Visual Programming Question Bank

Classes, interfaces, & documentation. Review of basic building blocks

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Connection Example. Document ID : Connection_Example.PDF Author : Michele Harris Version : 1.1 Date :

Compaq Interview Questions And Answers

PROGRAMMING ASSIGNMENT: MOVIE QUIZ

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Mr.Khaled Anwar ( )

Object Oriented Programming With Visual Basic.NET

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

1 Dept: CE.NET Programming ( ) Prof. Akash N. Siddhpura. Working with Form: properties, methods and events

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

DRAWING AND MOVING IMAGES

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

B16 Object Oriented Programming

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Visual Basic for Applications

MATFOR In Visual Basic

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

C# Language. CSE 409 Advanced Internet Technology

CS608 Lecture Notes. Visual Basic.NET Programming. Object-Oriented Programming Creating Custom Classes & Objects. (Part I) (Lecture Notes 2A)

Interacting with External Applications

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

Transcription:

DEVELOPING OBJECT ORIENTED APPLICATIONS By now, everybody should be comfortable using form controls, their properties, along with methods and events of the form class. In this unit, we discuss creating classes and how to use the properties and methods of the objects defined by those classes. Review of class and object concepts An object is a self-contained unit that has properties (data) and methods (operations). A class is the code that defines the properties and methods of an object. An object is an instance of a class, and the process of creating an object from a class is called instantiation. If a class is based on an existing class called a base class, the new class inherits the properties and methods of the base class. To illustrate the concepts today, we will develop an invoice application that is implemented with two user defined classes: An invoice class used to define invoice object A form class used to implement the user interface How to develop the business classes Identify the business objects that the application requires. Design the business classes for the business objects by identifying all the properties and methods that any project will require. Code and test the business classes so they re ready for use by any projects in the application. Invoice Object Properties CustomerName As String Read-Write OrderTotal As Decimal Read only (form cannot change) DiscountAmount As Decimal Read only (form cannot change) InvoiceTotal As Decimal Read only (form cannot change) Invoice Object Methods AddItem(UnitPrice as Decimal, Quantity As Integer) Adds an item to the invoice and recalculates the Order Total, Discount Amount, and Invoice Total. WriteInvoice( ) Writes current Invoice data to a file or database. 1

If we instantiate two or more instances of the same class, they will have all of the same properties and methods. The properties in each instance may have different values, though. These two Invoice objects have been instantiated from the Invoice class Invoice1 CustomerName = Mark Adams OrderTotal = 139.65 DiscountAmount = 13.96 InvoiceTotal = 125.69 Invoice2 CustomerName = Bill Jones OrderTotal = 100.00 DiscountAmount = 10.00 InvoiceTotal = 90.00 Three Types of Classes for Business Applications A commercial application often requires three distinct types of classes: User interface classes to perform all of the processing related to forms Business logic classes to perform the actual business operations Database classes to save and retrieve data for the business objects. Separating the user interface and the data retrieval from the business logic greatly simplifies the design and maintenance of the application. 2

How the Invoice Application is Implemented This application is implemented by a form class and the Invoice class. The form class is a user interface class that defines the form and its controls, accepts the user entries, and responds to any user actions. The Invoice class is a business class that defines the properties and methods of each Invoice object. To begin an invoice, the user enters a customer name and the unit price and quantity for the first item, then clicks the Add Item button. The invoice will multiply the unit price and quantity to determine the item total It adds the item total to the order total, calculates the discount, and determines the invoice total. To add another item to the invoice, the user enters a new unit price and quantity, and then clicks the Add Item button again. Finally, the user will click the New Invoice button. A message box with the totals appears, and the form is cleared (we are not going to design a database class for this example). 3

The Code for the Form Class Here is the form class that uses the properties and methods that we discussed earlier Public Class Form1 Inherits System.Windows.Forms.Form Dim Invoice As Invoice Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Invoice = New Invoice() txtcustomername.focus() Private Sub btnadditem_click(byval sender As System.Object, _ ByVal e As System.EventArgs) Handles btnadditem.click If ValidData.PositiveNumber(txtUnitPrice, "Unit Price") Then If ValidData.PositiveInteger(txtQuantity, "Quantity") Then Invoice.AddItem(txtUnitPrice.Text, txtquantity.text) lblordertotal.text = FormatNumber(Invoice.OrderTotal) lbldiscountamount.text = FormatNumber(Invoice.DiscountAmount) lblinvoicetotal.text = FormatNumber(Invoice.InvoiceTotal) txtunitprice.text = "" txtquantity.text = "" txtunitprice.focus() End If End If Private Sub btnok_click(byval sender As System.Object, _ ByVal e As System.EventArgs) Handles btnnewinvoice.click Invoice.CustomerName = txtcustomername.text MessageBox.Show("Invoice number " & Invoice.NextInvoiceNumber & _ " dated " & Invoice.InvoiceDate & " for " & _ Invoice.CustomerName & " totaling $" _ & Invoice.InvoiceTotal & ".") Invoice = New Invoice() lblordertotal.text = "" lbldiscountamount.text = "" lblinvoicetotal.text = "" txtcustomername.text = "" txtunitprice.text = "" txtquantity.text = "" txtcustomername.focus() Private Sub btnexit_click(byval sender As System.Object, _ ByVal e As System.EventArgs) Handles btnexit.click Me.Close() End Class 4

Object variable declaration We have declared an object variable of type Invoice. This variable is necessary to refer to the data for the Invoice objects. This is a form level or module level variable Form1 Load Event Procedure An instance of the Invoice class is created and assigned to the object variable. That is, we create an Invoice object. The focus is moved to the text box for the customer name and the New Invoice button is disabled to prevent users from creating another invoice object. AddItem Click Event Procedure The AddItem method of the Invoice object is invoked and sent the UnitPrice and Quantity entered by the user. This method does all of the required processing. The next three statements get the read-only properties of the Invoice object and update the appropriate labels on the form. Finally, the text boxes are cleared, the focus is moved to UnitPrice, and the New Invoice button is enabled. NewInvoice Click Event Procedure The customer name is assigned to the CustomerName property of the Invoice object and a MessageBox is displayed showing the four properties of the Invoice object. Typically, we would invoke a WriteInvoice method at this point. To keep things simple for this example, we just create a new Invoice object, assign it to the Invoice variable, and clear the text boxes and labels. Note that all the data for the previous Invoice object are lost in this example. But, by looking at this form class, we can see how we access the properties and methods of a business class. It is quite similar to the way that we access the properties and methods of the.net classes. We don t have to know how they are coded, we just have to know how to use them. This is one of the big advantages of encapsulation. 5

The Code for The Invoice Class Now, let s look at the code for this particular business class, the Invoice class Public Class Invoice Private scustomername As String Private dordertotal As Decimal Private dinvoicetotal As Decimal Private ddiscountamount As Decimal Private Shared inextinvoicenumber As Integer = 1000 Private Shared dtminvoicedate As Date = Today() Public Shared ReadOnly Property NextInvoiceNumber() As Integer inextinvoicenumber += 1 Return inextinvoicenumber End Public Shared ReadOnly Property InvoiceDate() As Date Return dtminvoicedate End Public Property CustomerName() As String Dim i As Integer Return scustomername End Set(ByVal Value As String) scustomername = Value End Set Public ReadOnly Property OrderTotal() As Decimal Return dordertotal End Public ReadOnly Property DiscountAmount() As Decimal Return ddiscountamount End Public ReadOnly Property InvoiceTotal() As Decimal Return dinvoicetotal End 6

Public Sub AddItem(ByVal UnitPrice As Decimal, ByVal Quantity As Integer) dordertotal += UnitPrice * Quantity ddiscountamount = Discount(dOrderTotal) dinvoicetotal = dordertotal - ddiscountamount Private Function Discount(ByVal OrderTotal As Decimal) As Decimal Dim ddiscountpct As Decimal Select Case dordertotal Case Is >= 500 ddiscountpct = 0.3 Case Is >= 200 ddiscountpct = 0.2 Case Is >= 100 ddiscountpct = 0.1 Case Else '<100 ddiscountpct = 0 End Select Return OrderTotal * ddiscountpct End Function End Class Included in the code for the Invoice class are: Private instance variable declarations Procedures to define the four properties The AddItem Method A private procedure to calculate the discount At the top are declarations for four private variables called instance variables. They are created when an objected is instantiated from this class and are used to store the data for the properties of each object. Instance variables are normally declared as private variables so that they may not be referred to from outside the class. Next are the four property procedures. They provide access to the four instance variables from outside the class. They each have a procedure that is used to retrieve the value of the property from the corresponding instance variable. The CustomerName property procedure also has a Set procedure that is used for setting the value of the property. There is a public subprocedure that implements the AddItem method. AddItem accepts two arguments, UnitPrice and Quantity, then multiplies the values of the two arguments and adds the result to the order total. AddItem then calls Discount, a private function, which returns the discount amount, which is subtracted from the order total to get the invoice total. 7

Note that AddItem may be referred to from other objects in the application, Discount may not. The Public keyword is used to identify the properties and methods available from outside the class. The Private keyword is used to identify instance variables, functions, and subprocedures that may only be accessed internally. This illustrates the concept of encapsulation. The programmer can hide, or encapsulate, some data operations of a class while exposing others. Private variables and procedures are hidden. Public variables, public function and sub procedures provide the exposed interface to the data and operations of the class. Encapsulation lets the user of a class think of it as a black box that provides useful properties and methods. Adding a class To create a user defined class, we start by adding a class file to the application. Click on Project Add Class and we get a Add New Item dialog box. We type in the name we want to use for the class, then click Open. This adds a class file to the project, which appears in the solution explorer with the.vb extension. The Class and End Class statements are automatically added to the class. We may then ender the code for the class between those two statements. 8

The syntax for the Property statement Public [ReadOnly WriteOnly] Property name As type [ [statements] {name = propertyvalue} {Return propertyvalue} End ] [Set(ByVal varname [As datatype]) [statements] propertyvalue = newvalue End Set] We use the Property statement to code a property procedure for each property that the class defines. We can create a property that is read-only, write-only, or read/write. One or both of the two accessors, and Set will appear, which provide access to the property values. The statement is used to service a request to retrieve the property value. The Set statement is used when a request is made to set the property value. A procedure that defines a read/write property Public Property CustomerName() As String Return scustomername End Set(ByVal Value As String) scustomername = Value End Set When you start the property statement without the ReadOnly or WriteOnly keywords, /End and Set/End Set pairs are automatically generated. You need to add the code in the middle. Since a returns a value, it is coded similarly to a function by using the Return keyword, or setting the name of the property to the return value. This example will respond to requests for the value of the CustomerName property by returning the value of the instance variable scustomername. Or, it can be used to set the property value by assigning the value of the Value argument to scustomername. 9

A procedure that defines a read-only property Public ReadOnly Property OrderTotal() As Decimal OrderTotal = dordertotal End This example will retrieve the value of the OrderTotal property by using an assignment statement to assign the value of the instance variable to the property identifier. When you start a Property statement and use the ReadOnly keyword, only a /End pair is generated. A procedure that defines a write-only property Public WriteOnly Property InterestRate() As Decimal Set(ByVal Value As Decimal) dinterestrate = Value End Set This example shows the code used to set the InterestRate property. It may not be used to retrieve the value of this property. The instance variable sinterestrate is set to the value of the passed argument Value. When you start a Property statement and use the WriteOnly keyword, only a Set/End Set pair is generated. 10

A method that does not return a value Public Sub AddItem(ByVal UnitPrice As Decimal, _ ByVal Quantity As Integer) dordertotal += UnitPrice * Quantity ddiscountamount = Discount(dOrderTotal) dinvoicetotal = dordertotal - ddiscountamount Methods are nothing more than Public sub procedures and functions. This example shows a method the accepts two arguments, but does not return a value. It is used to set the instance variables. That is, it will set the values for the class properties. A method that returns a value Public Function InvoiceTotal( _ ByVal UnitPrice As Decimal, _ ByVal Quantity As Integer) As Decimal dordertotal += UnitPrice * Quantity ddiscountamount = Discount(dOrderTotal) dinvoicetotal = dordertotal ddiscountamount Return dinvoicetotal This example will accept two arguments and returns the value of dinvoicetotal. A function declaration is used. Note that in these methods, the Public keyword is used. This is often not the case in ordinary sub and function procedures. 11

Defining an object variable and instantiating an object In one statement: Dim Invoice As New Invoice() In two statements: Dim Invoice As Invoice() Invoice = New Invoice() Before we can instantiate a class, we have to create an object variable to hold the object. The Dim statement is used to define the object variable and names the class from which the object will be created. If we include the New keyword, then a new object will be created. If we do not include the New keyword, then an object will not be created right then. We can also create an object from the class by including the New keyword in an assignment statement. Referring to object properties and methods Invoice.CustomerName = txtcustomername.text lblordertotal.text = Invoice.OrderTotal Invoice.AddItem(dUnitPrice, iquantity) We can refer to the properties and methods of our user-defined classes in exactly the same way as we refer to properties and methods in the.net classes. We type the object name, followed by the dot operator, followed by the name of the property or method. The first example assigns the text entered by a user to the CustomerName property of the Invoice object. The second example writes the value of the OrderTotal property of the Invoice object to a label control. The last example invokes the AddItem method of the Invoice object and passes two parameters. Dereferencing an object Invoice = Nothing When we finish with an object, we do not normally need to do anything with it. The memory will be deallocated when the application completes. If we want to deallocate memory sooner, there is a way to dereference an object, by setting its object variable to nothing. This will release the memory and the runtime environment s garbage collection routine will take it from there. 12