1 / 15

Object-Based Programming in VB.NET

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

Download Presentation

Object-Based Programming in VB.NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Based Programming in VB.NET

  2. Must Understand Following: • Encapsulation • Information hiding • Abstract Data Type • Class, Instance, Reference Variable v. value variable, Object, Method (function or subroutine), Property, …

  3. 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

  4. 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())

  5. 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

  6. 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

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

  8. Using Overloaded Constructors • Overloaded Constructors • Must have different numbers and/or types and/or orders of parameters(See example of Person2)

  9. 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

  10. 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

  11. 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

  12. 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++

  13. 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

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

  15. 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

More Related