Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS 338: Classes and Modules Dr. Ralph D. Westfall May, 2011."— Presentation transcript:

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

2 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

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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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]

15 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

16 ReadOnly and WriteOnly

17 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

18 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

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

20 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.)

21 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

22 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

23 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

24 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)

25 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

26 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

27 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


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

Similar presentations


Ads by Google