440 likes | 833 Views
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
E N D
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 • Add appropriate methods, class-level variables, and properties • Public Class Personpublic firstName as stringpublic MI as charpublic lastName as stringpublic function fullName() return firstname.trim() & “ “ & MI.trim() & _ lastName.trim()end functionend class
Create Reference Variable and Class Instance • Dim x as Person ‘Note first char Cap ‘pointer on stackx = new Person ‘this creates instance on heapx.firstname = “fred”x.MI = “F”x.lastName = “farfinkle”msgbox(x.fullname())
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
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
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
Using Overloaded Constructors • Overloaded Constructors • Must have different numbers and/or types and/or orders of parameters(See example of Person2)
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
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 thandim spouse as person2
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
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++
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
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
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