Summary of text to be covered Create your own class Create and use objects of your own class Control access to object instance variables Use keyword private.

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VBA Modules, Functions, Variables, and Constants
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Collections and classes. Projects in this ppt Collection interface practice The microwave oven simulator Bank account.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 15 – Digital Clock Application: Building Your.
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.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Variables and Constants
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
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)
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Why to Create a Procedure
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 8: Writing Graphical User Interfaces
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Teacher: Ms. Olifer MICROSOFT VISUAL STUDIO 2010: PROPERTIES OF WINDOWS FORM OBJECT.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 12: How Long Can This Go On?
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
CIS 338: Classes and Modules Dr. Ralph D. Westfall May, 2011.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Microwave Oven Application Building Your Own Classes and Objects.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CSC 157 (Blum)1 Hello World. CSC 157 (Blum)2 Start/Programs/Microsoft Visual Studio.NET 2003/Microsoft Visual Studio.NET 2003.
1.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET Part I VB.NET Controls VB.NET Events.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Decisions with Select Case and Strings Chapter 4 Part 2.
Controlling Program Flow with Decision Structures.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Object-Oriented Programming: Classes and Objects.
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
IS 350 Application Structure
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Classes and Objects
VISUAL BASIC.
Visual Basic..
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
CIS16 Application Development Programming with Visual Basic
Classes & Objects A deeper Look Chapter 10 & 11
GUI Programming in Visual Studio .NET
Presentation transcript:

Summary of text to be covered Create your own class Create and use objects of your own class Control access to object instance variables Use keyword private Create your own properties Use panel control Use string methods padLeft and Substrings

Key points Classes: Classes contain methods or variables and called members of the class e.g. in class random there is a method next which you have accessed using random.next() Each GUI control, toolbox control that you add is an instance of (an object) of that class. In other words your application is a client of that class. Keyword class and end class means start and end of the class

Chapter 19: Building your own classes and objects So far you have used classes defined in Framework class library (FCL) e.g. GUI objects, Classes String, Random, and Date When you add the GUI control to the form, an instance of that class is created Now we will create our own classes called programmer-defined classes. Classes can be reused

Microwave application A user enters the time for the microwave to cook food. To handle the time data we will create a class called Time. This class will store the number of minutes and seconds. (This will be used by the microwave application to keep track of remaining cooking time.

Microwave App Add a panel to the form. A panel is different from group box as it does not have a caption Rename the panel to windowPanel View the code. You will notice that the code already includes events for numeric buttons.

Microwave App Add a new class by clicking project  add class and rename to time.vb Any methods or variables created in this class will be called members of this class Keyword class and end class means start and end of the class Comments are very important to describe the purpose of the class. Purpose of this class is to represent time data and its properties.

Microwave App Declare 2 instance variables: Dim minuteValue As integer Dim secondValue as Integer Value of minute will be stored in minuteValue and seconds will be stored in secondValue Each class contains instance variables and methods.

Microwave App Instance variables are variables defined in a class and are copied each time an object of that class is created. Methods in class perform certain activities e.g. random.next or string.format. Next and format are methods defined in random and string class Constructor is a special type of methods which gets called first and initializes the classes instance variables

Microwave App A constructor is a sub procedure type of a method so it cannot return a value ' Time constructor, minute and second supplied Public Sub New(ByVal mm As Integer, ByVal ss As Integer) Minute=mm Second=ss End Sub So when creating an object of this class the statement will look like –timeObject= New Time (5,3)

Microwave App Now back in the main application declare an object of the class time under timeIs variable Dim timeObject As Time Classes also have properties which allow manipulating the instance variables e.g. to manipulate the value of variable minuteValue and secondValue, we will create two properties: minute and second.

Microwave App The property consists of two accesors –One to modify and return a value (Get accessor) e.g. minuteValue=timeObject.minute –One to assign a value (Set accessor) e.g. timeObject.minute=35. This can also be used to validate the values e.g. 0<min<59.

Microwave App Declare the property function Public Property Minute () As Integer The rest of the code will be declared automatically. Now the get accessor returns the value stored in minute so return minuteValue Set validates the minute and stores the value so check if 0<min<60 is met and assign the value.

Microwave App Public Property minute() As Integer Get Return minuteValue End Get 'set minute value Set(ByVal value As Integer) If (value < 60) Then minuteValue = value Else minuteValue = 0 End If End Set End Property Public Property second() As Integer Get Return secondValue End Get Set(ByVal value As Integer) If (value < 60) Then secondValue = value Else secondValue = 0 End If End Set End Property

Microwave App Now that your class is ready, create the start button event. Dim second as Integer Dim minute as Integer timeIs=timeIs.PadLeft(4, convert.ToChar(“0”)) Padleft ensures that the string is atleast 4 characters long so 3:25 displays as 03:25 Now the first two digits represent minutes and last two represent seconds

Microwave App. Now to break the string and get minutes and seconds add the following code: Second=Convert.ToInt32(timeIs.Substring(2)) Substring(2) means start at the second position and go till the end. So 0345 will start at 4 and give 45 Minute=convert.ToInt32(timeIs.Substring(0,2)) Substring(0,2) means start at position 0 and get 2 characters. So 0345 will start at 0 and give 03.

Microwave App Create the time object timeObject = New Time(minute, second) New allocates the memory in which the time object will be stored. The constructor in class time initializes the instance variables. displayLabel.Text = String.Format("{0:d2}:{1:d2}", timeObject.minute, timeObject.second) timeIs = "" clockTimer.Enabled = True windowPanel.BackColor = Color.Yellow

Microwave App. Define the clear button event displayLabel.Text = " Microwave Owen" timeIs = "" timeObject = New Time(0, 0) clockTimer.Enabled = False windowPanel.BackColor = Control.DefaultBackColor

Display time sub ' method to display formatted time in timer window Sub DisplayTime() Dim second As Integer Dim minute As Integer Dim display As String If timeIs.Length > 4 Then timeIs = timeIs.Substring(0, 4) End If display = timeIs.PadLeft(4, Convert.ToChar("0")) second = Convert.ToInt32(display.Substring(2)) minute = Convert.ToInt32(display.Substring(0, 2)) displayLabel.Text = String.Format("{0:d2}:{1:d2}", minute, second) End Sub ' DisplayTime

Only thing left is the countdown If timeObject.second > 0 Then timeObject.second -= 1 ElseIf timeObject.minute > 0 Then timeObject.minute -= 1 timeObject.second = 59 Else clockTimer.Enabled = False Beep() displayLabel.Text = "Done" windowPanel.BackColor = Control.DefaultBackColor Return ‘ returns the control back to the application without executing any further End If displayLabel.Text = String.Format("{0:d2}:{1:d2}", timeObject.minute, timeObject.second)

Summary Declare a class Declare instance variables Declare a constructor (sub procedure that is called when class object is created). Used to initialize instance variables Declare properties of the class: Get/Set Create objects of the class and use them using dot operator

Controlling access to members Keywords public and private are called member-access modifiers. Public class objects are available to any Time object. Private makes it available to only methods and properties of the class. Instance variables are usually declared private whereas methods and properties are declared public.

Inclass group exercise 19:13 – The local bank wants you to create an application that will allow it to view clients information. The interface is given below. You need to implement the client class. Once the application is complete, the manager should be able to click the next or previous button to run through each clients information. The information is stored in arrays. Download the template of the application from here.