You are on page 1of 7

OOP with VB

Constructors A constructor is a special member function whose task is to initialize the objects of it's class. This is the first method that is run when an instance of a type is created. A constructor is invoked whenever an object of it's associated class is created. If a class contains a constructor, then an object created by that class will be initialized automatically. We pass data to the constructor by enclosing it in the parentheses following the class name when creating an object. Constructors can never return a value, and can be overridden to provide custom intitialization functionality. In Visual Basic we create constructors by adding a Sub procedure named New to a class. The following code demonstrates the use of constructors in Visual Basic. Module Module1 Sub Main() Dim con As New Constructor(10) WriteLine(con.display()) 'storing a value in the constructor by passing a value(10) and calling it with the 'display method Read() End Sub End Module Public Class Constructor Public x As Integer Public Sub New(ByVal value As Integer) 'constructor x = value 'storing the value of x in constructor End Sub Public Function display() As Integer Return x 'returning the stored value End Function End Class Destructors A destructor, also know as finalizer, is the last method run by a class.

Within a destructor we can place code to clean up the object after it is used, which might include decrementing counters or releasing resources. We use Finalize method in Visual Basic for this and the Finalize method is called automatically when the .NET runtime determines that the object is no longer required. When working with destructors we need to use the overrides keyword with Finalize method as we will override the Finalize method built into the Object class. We normally use Finalize method to deallocate resources and inform other objects that the current object is going to be destroyed. Because of the nondeterministic nature of garbage collection, it is very hard to determine when a class's destructor will be called. The following code demonstrates the use of Finalize method. Module Module1 Sub Main() Dim obj As New Destructor() End Sub End Module Public Class Destructor Protected Overrides Sub Finalize() Write("hello") Read() End Sub End Class When you run the above code, the word and object, obj of class, destructor is created and "Hello" is displayed. When you close the DOS window, obj is destroyed.

Methods
A Method is a procedure built into the class. They are a series of statements that are executed when called. Methods allow us to handle code in a simple and organized fashion. There are two types of methods in VB .NET: those that return a value (Functions) and those that do not return a value (Sub Procedures). Both of them are discussed below. Sub Procedures

Sub procedures are methods which do not return a value. Each time when the procedure is called the statements within it are executed until the matching End is encountered. Sub Main(), the starting point of the program itself is a procedure. When the application starts execution, control is transferred to Main procedure automatically which is called by default. Example of a Sub Procedure Module Module1 Sub Main() 'sub procedure Main() is called by default Display() 'sub procedure display() which we are creating End Sub Sub Display() System.Console.WriteLine("Using Sub Procedures") 'executing sub procedure Display() End Sub End Module The image below displays output from above

Sub Sub sub Sub

code.

Functions Function is a method which returns a value. Functions are used to evaluate data, make calculations or to transform data. Declaring a Function is similar to declaring a Sub procedure. Functions are declared with the Function keyword. The following code is an example on Functions: Imports System.Console

Module Module1 Sub Main() Write("Sum is" & " " & Add()) 'calling the function End Sub Public Function Add() As Integer 'declaring a function add Dim i, j As Integer 'declaring two integers and assigning values to them i = 10 j = 20 Return (i + j) 'performing the sum of two integers and returning it's value End Function End Module The image below displays output from above code.

Calling Methods A method is not executed until it is called. A method is called by referencing it's name along with any required parameters. For example, the above code called the Add method in Sub main like this: Write("Sum is" & " " & Add()). Method Variables Variables declared within methods are called method variables. They have method scope which means that once the method is executed they are destroyed and their memory is reclaimed. For example, from the above code (Functions) the Add method

declared two integer variables i, j. Those two variables are accessible only within the method and not from outside the method. Parameters A parameter is an argument that is passed to the method by the method that calls it. Parameters are enclosed in parentheses after the method name in the method declaration. You must specify types for these parameters. The general form of a method with parameters looks like this: Public Function -----------Implementation -----------End Function Add(ByVal x1 as Integer, ByVal y1 as Integer)

Understanding Objects
Object-oriented programming has been a technical buzzword for quite some time, but as far as Visual Basic programmers are concerned, it became a reality only with Visual Basic .NET (no previous version of Visual Basic was a true OO language). Almost everywhere you lookthe Web, publications, booksyou read about objects. What exactly is an object? Strictly speaking, an object is a programming structure that encapsulates data and functionality as a single unit and for which the only public access is through the programming structure's interfaces (properties, methods, and events). In reality, the answer to this question can be somewhat ambiguous because there are so many types of objectsand the number grows almost daily. However, all objects share specific characteristics, such as properties and methods. The most commonly used objects in Visual Basic .NET are the form object and the control object. Earlier hours introduced you to working with forms and controls and even showed you how to set form and control properties. In your Picture Viewer project from Hour 1, for instance, you added a picture box and two buttons to a form. Both the PictureBox and the Button controls are control objects, but each is a specific type of control object. Another, less-technical example uses pets. Dogs and cats are definitely different entities (objects), but they both fit into the category of Pet objects. Similarly, text boxes and buttons are each a unique type of object, but they're both considered a control object. This small distinction is important.

COLLECTIONS Collection Classes

============================== i) ArrayList ii) HashTable iii)SortedList iv) Queue and Stack ArrayList: ============================== The ArrayList is a collection class that models a dynamic array, whose size increases as required when new objects are added to the array. Namespace : System.Collections Implementing Interfaces : ICollection, IList, ICloneable, IConvertible Characteristics: ============================== i) To store dynamic data use the arraylist class. So it is compact memory usage. ii) The search for an item in an arraylist is performed sequentially. So it is slow. iii)Type of Access is indexed. HashTable: ============================== A HashTable is a collection of key-value pairs implemented using a hash table algorithm. Namespace : System.Collections Implementing Interfaces : ICollection, IDictionary Characteristics: ============================== i) Search is very fast. ii) HashTables are big and fast. iii)High Memory usage. iv) Type of Access is using the hash of a key value. SortedList: ============================== A SortedList is a collection class that holds a set of key-value pairs. Namespace : System.Collections Implementing Interfaces : ICollection, IDictionary Characteristics: ============================== i) A Sorted List may not contain duplicate keys. A HashTable can have duplicate keys. ii) Search is fast. iii)Medium Memory usage. iv) Type of Access is using the index and key value. Queue and Stack: ============================== i) Queue uses a First-In-First-Out (FIFO) Algorithm. ii) Stack uses a Last-In-First-Out (LIFO) Algorithm. Methods of Queue: ============================== i) Enqueue (Add an item) ii)Dequeue (Remove the first item) Methods of Stack: ============================== i) Push (Add an item) ii)Pop (Remove the last item)

You might also like