15
Object-Based Programming in VB.NET

Object-Based Programming in VB.NET

Embed Size (px)

DESCRIPTION

Object-Based Programming in VB.NET. Must Understand Following:. Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable v. value variable, Object, Method (function or subroutine), Property, …. Create Class. Add new class file with appropriate name - PowerPoint PPT Presentation

Citation preview

Page 1: Object-Based  Programming  in VB.NET

Object-Based Programming

in VB.NET

Page 2: Object-Based  Programming  in VB.NET

Must Understand Following:

• Encapsulation

• Information hiding• Abstract Data Type• Class, Instance, Reference Variable v. value

variable, Object, Method (function or subroutine), Property, …

Page 3: Object-Based  Programming  in VB.NET

Create Class• Add new class file with appropriate name• Add appropriate methods, class-level variables,

and properties• Public Class Person

public firstName as stringpublic MI as charpublic lastName as stringpublic function fullName()

return firstname.trim() & “ “ & MI.trim() & _lastName.trim()

end functionend class

Page 4: Object-Based  Programming  in VB.NET

Create Reference Variable and Class Instance

• Dim x as Person ‘Note first char Cap‘pointer on stack

x = new Person ‘this creates instance on heap

x.firstname = “fred”x.MI = “F”x.lastName = “farfinkle”msgbox(x.fullname())

Page 5: Object-Based  Programming  in VB.NET

Class Scope• Class's Scope

– Instance variables and methods• Class’s members

– Class members that are visible can be accessed only through a “handle” (ReferenceVariable.memberName)

• Variables within methods– Only methods can access that variable

• Keyword Me– A hidden instance variable can be accessed in a method by

preceding its name with the keyword Me and dot operator

Page 6: Object-Based  Programming  in VB.NET

Controlling Access to Members• Public versus Private

– Control access to a class’s instance variables and methods

• Public– Serve primarily to present interfaces of a class

• Private– Holds clients private data safely

– Get and set functions• Have ability to access private data

Page 7: Object-Based  Programming  in VB.NET

Initializing Class Objects: Constructors

• Initializing Constructors– False for Booleans and Nothing for references

• If an instance variable is not initialized the compiler will assign a default value

– Form of declarations• Dim ObjectReference As New ClassName(arguments)

• Programs without default constructor are provided with one by the compiler

Page 8: Object-Based  Programming  in VB.NET

Using Overloaded Constructors

• Overloaded Constructors– Must have different numbers and/or types

and/or orders of parameters(See example of Person2)

Page 9: Object-Based  Programming  in VB.NET

Properties• Private and Public

– Get accessor • In Visual Basic instance variables as private does not

guarantee

data integrity

– Set accessor• Cannot return values indicating a failed attempt to assign

invalid data to objects of the class

• Control the setting of instance variables to valid values

– Get and Set accessors are not required• A property with only Get accessor is called ReadOnly

• A property with only Set accessor is called WriteOnly

Page 10: Object-Based  Programming  in VB.NET

Composition: Objects as Instance Variables of Other Classes

• Composition: Objects as Instance Variables of Other Classes– Note “spouse” reference Variable pointing to

second instance of Person2 contained inside first instance of Person2.

• DANGER!!!!!!! – NOTE WHERE OBJECT IS INSTANCIATED!!!!!

• – what happens if we had:– Dim spouse as new Person2 rather than

dim spouse as person2

Page 11: Object-Based  Programming  in VB.NET

Using the Me Reference

• Me Reference– Every object can access a reference to itself

using a Me reference. • Me explicitly

• Me implicitly – The explicit use of the Me reference can increase program

clarity where Me is optional

Page 12: Object-Based  Programming  in VB.NET

Garbage Collection• Garbage collector

– Resource leaks• Objects must have an efficient way to return memory and

release resources when the program no longer uses those objects

– Memory leaks• In Visual Basic memory is reclaimed automatically, hence it

experiences rare memory leaks as compared to C and C++ – Reference counting!!

– Finalization• Finalizer method performs termination housekeeping on that

object just before the garbage collector reclaims the object's memory. Not the same as deconstructor in C++

Page 13: Object-Based  Programming  in VB.NET

Shared Class Members

• Shared Class Variable– Contains only one copy of this variable in

memory• When a single copy of the data will suffice, use

Shared class variables to save storage.

• Shared class variables are not the same as global variables because Shared class variables have class scope

• Shared method has no Me reference

Page 14: Object-Based  Programming  in VB.NET

Const and ReadOnly Members

• Const or ReadOnly – Const

• A data member must be initialized in its declaration

• Cannot be modified once initialized

– ReadOnly• A data member can be initialized either in the class

structure or in its declaration

• Cannot be modified once initialized

Page 15: Object-Based  Programming  in VB.NET

Namespaces and Assemblies

• Framework Class Library– .NET Framework:

• Must be imported to a Visual Basic program by including a reference to those libraries

– Namespaces: • Namespaces help minimize naming collisions by

proving a convention for unique class names