meljun cortes vb.net classes and objects

16
Visual Basic .Net Programming Classes and Objects Define Class Procedures for Defining a Class Using Access Modifiers Declaring Methods Declaring Properties Define Objects Supporting Events with Objects Classes and Objects * Property of STI Page 1 of 16 Supporting Events with Objects Comparing Objects OOP with Structures and Modules

Upload: meljun-cortes

Post on 19-Jan-2017

12 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

Classes and Objects

� Define Class

� Procedures for Defining a Class

� Using Access Modifiers

� Declaring Methods

� Declaring Properties

� Define Objects

Supporting Events with Objects

Classes and Objects * Property of STIPage 1 of 16

� Supporting Events with Objects

� Comparing Objects

� OOP with Structures and Modules

Page 2: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

Class

� defines a new type of thing

� defines the common characteristics of

every object of that new type

� in defining a new class, the characteristics

of all objects of that class, as well as their

behaviors are defined

Classes and Objects * Property of STIPage 2 of 16

behaviors are defined

� declaring class:

Class NewClass

‘ statement

End Class

Page 3: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

Procedure forDefining a Class

� To define a class

� Add a class to the project

� Provide an appropriate name of the class

� Create constructors as needed

� Create a destructor if appropriate

Classes and Objects * Property of STIPage 3 of 16

� Create a destructor if appropriate

� Declare properties

� Declare methods and events

Page 4: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

UsingAccess Modifiers

� Access Modifiers

� specify accessibility of variables and

procedures

Keyword Definition

Public Accessible everywhere

Private Accessible only within

the type itself.

Classes and Objects * Property of STIPage 4 of 16

the type itself.

Friend Accessible only within

the type itself and all

namespaces and code

with the same assembly.

Protected Only for use on class

members.

Protected Friend The union of Protected

and Friend.

Page 5: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

DeclaringMethods

� Methods

� procedures that contain application logic

� Syntax:

Public Sub TestIt(ByVal x As

Integer) …

End Sub

Classes and Objects * Property of STIPage 5 of 16

End Sub

Public Function GetIt( ) As

Integer

End Function

Page 6: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

DeclaringProperties

� Properties

� used to store variables in a class

� Syntax

Public Property MyData( ) As

Integer

Get

Classes and Objects * Property of STIPage 6 of 16

Get

Return intMyData

End Get

Set (ByVal Value As Integer)

intMyData = Value

End Set

End Property

Page 7: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

DeclaringProperties

� ReadOnly Properties

� used to get the value of the property

� can be created by using ReadOnly keyword

� cannot be used in an assignment statement

� Example:

Classes and Objects * Property of STIPage 7 of 16

Public ReadOnly Property MyData( )

As Integer

Get

Return intMyData

End Get

End Property

Page 8: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

DeclaringProperties

� Write-Only properties

� used to set the value of the property

� can be created by using WriteOnly keyword

� cannot be used to retrieve the value of the

property

� Example:

Classes and Objects * Property of STIPage 8 of 16

Public WriteOnly Property

MyData( ) As Integer

Set (ByVal Value As Integer)

intMyData = Value

End Set

End Property

Page 9: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

DeclaringProperties

� Default property

� can be created by using the Default keyword

� must be coded to take at least one argument

� public, protected, or friend access must be specified

� property array a property that is indexed

Classes and Objects * Property of STIPage 9 of 16

� a property that is indexed

� Example:

[Default Public Property Item(ByVal index As Integer) As Boolean

Get

Return myArray(index)

End Get

Set (ByVal Value As Boolean)

myArray(index) = Value

End Set

End Property

Page 10: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

Objects

� an individual instance of a class

� a usable example of the thing the class

represents

Classes and Objects * Property of STIPage 10 of 16

Page 11: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

Supporting Events with Objects

� To define and support events using Event

statement:

[<attrlist>][ Public | Private |

Protected | Friend | Protected

Friend]

[ Shadows ] Event

Classes and Objects * Property of STIPage 11 of 16

[ Shadows ] Event

eventname[(arglist)]

[Implements

interfacename.interfaceeventnam]

Page 12: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

Supporting Events with Objects

� Example:

Public Class TripleClicker

Public Event trpClick(ByVal Text As

String)

Public Sub Click()

Classes and Objects * Property of STIPage 12 of 16

Public Sub Click()

Static intCount As integer = 0

intCount += 1

If intCount >= 3 Then

intCount = 0

RaiseEvent trpClick_

(“The button was triple_

clicked.”)

EndIf

End Sub

End Class

Page 13: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

Supporting Events with Objects

� Example (con’t)

Public Class Form1

Dim WithEvents Click3 As New TripleClicker

Private Sub Click3_trpClick(ByVal text As

String) Handles Click3.trpClick

TextBox1.Text = text

End Sub

Classes and Objects * Property of STIPage 13 of 16

End Sub

Private Sub Button1_Click(ByVal sender As

System.Object, ByVal e As

System.EventArgs) Handles Button1.Click

Click3.Click()

End Sub

End Class

Page 14: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

Comparing Objects

� Is keyword

� a special keyword that can be used to compare objects in If statements

� Example:

Public Sub Button_Click(ByVal sender

As_ System.Object, ByVAl e As

Classes and Objects * Property of STIPage 14 of 16

As_ System.Object, ByVAl e As

System.EventArgs)

If sender Is Button1 Then

TextBox1.Text = “Button 1_

was clicked.”

Else

TextBox1.Text = “Button 2_

was clicked.”

EndIf

End Sub

Page 15: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

OOP with Structures and Modules

� Structures

� similar to classes but without methods

� Differences between Structures and

Classes

� structure members cannot be declared as

Protected

� structures cannot inherit from other

Classes and Objects * Property of STIPage 15 of 16

� structures cannot inherit from other

structures

� cannot use the Finalize method

� structure cannot include initializers, the

New keyword, or set initial sizes of arrays

� cannot define a constructor that takes no

arguments for a structure

� default access of data members in

structures is public if structures were

declared with Dim

� when a data item created from a structure

to procedure was passed, the entire

structure was copied

Page 16: MELJUN CORTES Vb.net classes and objects

Visual Basic .Net Programming

OOP with Structures and Modules

� Modules

� holds code, but can also support members,

like classes

� has exactly one instance and does not need

to be created or assigned to a variable

� do not support inheritance or implement

interfaces

Classes and Objects * Property of STIPage 16 of 16

� Differences between Modules and Classes

� members of a module are implicitly shared

� modules can never be instantiated

� modules do not support inheritance

� modules cannot implement interfaces

� modules cannot be nested in other types