CIS 338: Classes and Modules Dr. Ralph D. Westfall May, 2011.

Slides:



Advertisements
Similar presentations
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Advertisements

Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
OOP-Creating Object-Oriented Programs
VBA Modules, Functions, Variables, and Constants
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.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
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.
CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Multiple Forms, Standard Modules, And Menus
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Why to Create a Procedure
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
Introduction to Visual Basic.NET Chapter 2 Introduction to Controls, Events.
Introduction to Visual Basic.NET Your First Visual Basic.NET Application.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
CIS 338: Using Queries in Access as a RecordSource Dr. Ralph D. Westfall May, 2011.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Applications Development
CSCI 3327 Visual Basic Chapter 3: Classes and Objects UTPA – Fall 2011.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
3.2 VB.NET Events An Event Procedure Properties and Event Procedures of the Form Tab Order of Controls Exercises.
1 Advanced Computer Programming Lab Calculator Project.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET Part I VB.NET Controls VB.NET Events.
CIS 234: Project 1 Issues Dr. Ralph D. Westfall April, 2010.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
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.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
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.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
1 Introduction to Object Oriented Programming Chapter 10.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
CIS 338: Events Dr. Ralph D. Westfall April, 2011.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
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.
Chapter 8 Multiple Forms, Modules, and Menus. Introduction This chapter demonstrates how to: – Add multiple forms to a project – Create a module to hold.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
Stored Procedures Dr. Ralph D. Westfall May, 2011.
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Apply Procedures to Develop Message, Input, and Dialog Boxes
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Chapter 11 – Object-Oriented Programming
Visual Basic..
CIS16 Application Development Programming with Visual Basic
CIS16 Application Development and Programming using Visual Basic.net
Tonga Institute of Higher Education
Fundamentals of Programming in VB.Net
CIS16 Application Development and Programming using Visual Basic.net
Chapter 8 - Functions and Functionality
CIS 338: Images on Forms Dr. Ralph D. Westfall May, 2009.
GUI Programming in Visual Studio .NET
Presentation transcript:

CIS 338: Classes and Modules Dr. Ralph D. Westfall May, 2011

Classes and Objects classes are fundamental to object oriented programming (OOP) a class is a template or pattern an object is an instance or a specific example of a class Dim [object name] as New [class name] Dim rect as Polygon 'Polygon = previously created class 'rect = object that is being instantiated

Class Methods methods = Functions or Subprocedures of the class methods connected to objects via "dot notation" [object name].[method]() rect.Calc()

Creating a Class create the class create a New Project>Windows Application in VB.NET make sure to put project into a directory that you can find later Project>Add Class

Creating a Function in a Class declare as Public so can be used by other code (default is Private) Public Class Calc Public Function CalcArea(ByVal ht As _ String, ByVal wide As String) As String CalcArea = CStr((CDbl(ht) * _ CDbl(wide))) ' _ = line continuation End Function End Class ' function name here works like a variable ' name in the code where it is called

Using a Class Function to use, need to declare/instantiate object and then attach to Function Dim rect As New Polygon() Private Function btnCalc_Click(etc. txtArea.Text = rect.CalcArea _ (txtHt.Text, txtWide.Text) End Sub 'notes

Class Properties class properties = values related to it properties also use "dot notation" '[objname].[property] rect.Area properties don't use naming convention prefixes rect.Area, NOT rect. dbl Area

Class Properties - 2 could make properties be public variables in general declarations of class module Public Area As Double 'in class code myCls.Area = 10.5 'in form code disadvantages of this approach 1. doesn't hide data (no encapsulation) 2. no validation or other code runs when a value is assigned

Adding a Property Procedure start the declaration: Public Property [name]( ) as [type] then hit the Enter key Visual Studio adds Get and Set methods Get … End Get, Set … End Set also adds the End Property statement

Property Procedure Example Private dArea As Double Public Property Area() As Double Get Return dArea End Get Set(ByVal value As Double) dArea = value End Set End Property

Property Procedures written like functions or subprocedures behave (somewhat) like variables advantage: run code every time used Get – function line(s) that runs whenever reading a value Set – procedure line(s) that runs whenever writing a value additional code can do other things also

Property Procedures - Writing Dim side as Integer = 22 Dim rect as New Polygon() rect.Width = side writing side value to the Width property of the rect object therefore the Set procedure runs in the Property procedure

Property Procedures - Reading horizontal = rect.Width horizontal is reading Width property of rect object instantiated from Polygon class (in previous slide) therefore the Get function runs in the Property procedure

Property Procedures - Getter Get function (reading) Private nWide As Integer'declared variable Public Property Width() as Integer Get Width = nWide End Get 'Set …… End Set End Property note pattern:  Public Property X() As [type]  X = [the variable that returns the value]

Property Procedures - Setter simple Set procedure (writing) 'nWide declaration on previous slide Public Property Width() as Integer 'Get …… End Get Set (ByVal value as [type]) nWide = value 'value by convention End Set End Property note pattern:  Public Property X() As [type]  [variable declared outside function] = value

ReadOnly and WriteOnly

Reading/Running Other Code Get function – returning (reading) a string from data stored as an integer Private nColor as Integer Public Property Color() as String Get Select Case nColor Case 1 Color = "red" 'more Select Case statements End Select End Get

Writing/Running Other Code Set procedure – data entered as string, written as integer (for error control?) Private nColor As Integer Public Property Color As String Set(ByVal value As String) Select Case Left(value.ToLower), 4) Case "gree" 'using 1 st 4 letters nColor = 2 'gre = grey? 'rest of Case, Set and Property code 'notes

Constructors for Classes use word New instead of class name Public Sub New([arg list]) [variable]=[arg]'initialize etc.

Declaring and Using Objects create object in one line Dim hexagon as New Polygon([args]) create object in (separated) lines Dim hexagon as Polygon 'no () hexagon = New Polygon(sides, etc.)

Disposing of Objects getting rid of an object octagon = Nothing generates a Finalize event that causes class module to destroy the object frees resources  too many objects can use up lot of memory

Class Object Events each class object has at least these 2 events: New – has some similarities to a form's Load event Finalize – when class is destroyed

Method Events some objects can have methods created by VB.NET that automatically code event handling e.g., double click on a button on a form Private Sub btnResults_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResults.Click

Adding Events to Objects objects can have events for example, form object has Load event can add events to objects you create declare the event in the class module to show that the problem can happen with that class Public Event MissingData(ByVal sMessage _ As String)

Adding Events to Objects - 2 add code in class module to trigger event if the problem occurs Set(ByVal value As String) If value = Nothing Then RaiseEvent MissingData("no value") Else colr = value End If

Adding Events to Objects - 3 WithEvents adds events to object declaration in user interface events NOT handled in the class file Dim WithEvents rect as New Polygon 'Dim +"WithEvents" +rest of declaration 'outside of any Sub or Function Private Sub rect_MissingData(ByVal sMsg _ as String) Handles rect.MissingData MsgBox(sMsg, vbExclamation) End Sub

Demonstration download nations.zipnations.zip unzip it onto C:\ (root directory) view code note that this approach can be used to bring data from DataTier to BizTier to a ListView on the user interface form