|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
Contents
IntroductionVB.NET is completely object oriented. This article uncovers some basic Object Oriented Programming features of Visual Basic. NET. The whole article is divided into ten lessons. The source code for these lessons is provided with the article. This tutorial is designed with the following objectives:
Go through this tutorial and you will start making sense of almost any .NET code. Also, Java/CPP programmers can use this to understand OOPs in VB.NET. Using the codeThe source code for each lesson is available as a .vb source code file. You need Microsoft .NET framework SDK installed in your system to compile and execute the exercises in this article. You can download it from the Microsoft website. The VB.NET compiler (vbc.exe) normally resides in your FrameworkSDK\bin folder. To manually compile a source code file, you may use the command prompt to type: vbc filename.vb /out:"filename.exe" /r:"System.Windows.Forms.dll","System.dll" Lesson 1: Namespaces, Classes & Objects, Modules
Lesson 2: Access TypesThe major access types are Import the Imports System
Namespace Animals
Public Class Dog
'A public variable
Public AgeOfDog as Integer
Public Function Bark()
Console.Writeline ("Dog is barking")
End Function
Private Function Walk()
Console.Writeline ("Dog is walking")
End Function
End Class
End Namespace
Our Public Module modMain
Execution will start from the Sub Main()
'Call our function. See below
OurFunction()
End sub
'OurFunction: Called from Main()
Function OurFunction()
Dim Jimmy as Animals.Dog
Jimmy=new Animals.Dog()
'This will work, because Bark & Ageofdog are public
Jimmy.Bark
Jimmy.AgeOfDog=10
'Calling the Walk function will not work here, because
'Walk() is outside the class Dog
'So this is wrong. Uncomment this and try to compile, it will
'cause an error.
'Jimmy.Walk
End Function
End Module
Additional Notes:
Lesson 3: Shared FunctionsThe shared members in a class (both functions and variables) can be used without creating objects of a class as shown. The Import the Imports System
Namespace Animals
Class Dog
Public Shared Function Bark()
Console.Writeline ("Dog is barking")
End Function
Public Function Walk()
Console.Writeline ("Dog is walking")
End Function
End Class
End Namespace
Our Public Module modMain
Execution will start from the Sub Main()
'We can call the Bark() function directly,
'with out creating an object of type Dog -
'because it is shared.
Animals.Dog.Bark()
'We can call the Walk() function only
'after creating an object, because
'it is not shared.
Dim Jimmy as Animals.Dog
Jimmy=new Animals.Dog()
Jimmy.Walk()
'Now Guess? The WriteLine() function we used so far
'is a shared function in class Console :)
'Also, we can write the Main() function itself as a shared
'function in a class. i.e Shared Sub Main(). Try
'moving Main() from this module to the above class
End sub
End Module
Lesson 4: OverloadingOverloading is a simple technique, to enable a single function name to accept parameters of different type. Let us see a simple Imports System
Class Adder
Here, we have two Overloads Public Sub Add(A as Integer, B as Integer)
Console.Writeline ("Adding Integers: " + Convert.ToString(a + b))
End Sub
This one adds two strings. Overloads Public Sub Add(A as String, B as String)
Console.Writeline ("Adding Strings: " + a + b)
End Sub
'And both have the same name. This is possible because, we used the
'Overloads keyword, to overload them.
'Here, we have the Main Function with in this class. When you write.
'your main function inside the class, it should be a shared function.
Shared Sub Main()
Dim AdderObj as Adder
'Create the object
AdderObj=new Adder
'This will invoke first function
AdderObj.Add(10,20)
'This will invoke second function
AdderObj.Add("hello"," how are you")
End Sub
End Class
Lesson 5: InheritanceInheritance is the property in which, a derived class acquires the attributes of its base class. In simple terms, you can create or 'inherit' your own class (derived class), using an existing class (base class). You can use the Let us see a simple example. Import the Imports System
Our simple base class: Class Human
'This is something that all humans do
Public Sub Walk()
Console.Writeline ("Walking")
End Sub
End Class
Now, let us derive a class from A Class Programmer
Inherits Human
'We already have the above Walk() function
'This is something that all programmers do ;)
Public Sub StealCode()
Console.Writeline ("Stealing code")
End Sub
End Class
Just a Class MainClass
'Our main function
Shared Sub Main()
Dim Tom as Programmer
Tom=new Programmer
'This call is okie because programmer got this function
'from its base class
Tom.Walk()
'This is also correct because Tom is a programmer
Tom.StealCode()
End Sub
End Class
Additional Notes:
Lesson 6: OverridingBy default, a derived class Import the Imports System
Our simple base class: Class Human
'Speak() is declared Overridable
Overridable Public Sub Speak()
Console.Writeline ("Speaking")
End Sub
End Class
Now, let us derive a class from An Class Indian
Inherits Human
'Let us make Indian speak Hindi, the National Language
'in India
'Speak() is overriding Speak() in its base class (Human)
Overrides Public Sub Speak()
Console.Writeline ("Speaking Hindi")
'Important: As you expect, any call to Speak() inside this class
'will invoke the Speak() in this class. If you need to
'call Speak() in base class, you can use MyBase keyword.
'Like this
'Mybase.Speak()
End Sub
End Class
Just a class to put our Class MainClass
'Our main function
Shared Sub Main()
'Tom is a generic Human
Dim Tom as Human
Tom=new Human
'Tony is a human and an Indian
Dim Tony as Indian
Tony=new Indian
'This call will invoke the Speak() function
'in class Human
Tom.Speak()
'This call will invoke the Speak() function
'in class Indian
Tony.Speak()
End Sub
End Class
Lesson 7: PolymorphismPolymorphism is the property in which a single object can take more than one form. For example, if you have a base class named Import the Imports System
This example is exactly the same as the one we saw in the previous lesson. The only difference is in the Our simple base class: Class Human
'Speak() is declared Overridable
Overridable Public Sub Speak()
Console.Writeline ("Speaking")
End Sub
End Class
Now, let us derive a class from An Class Indian
Inherits Human
'Let us make Indian speak Hindi, the National Language
'in India
'Speak() is overriding Speak() in its base class (Human)
Overrides Public Sub Speak()
Console.Writeline ("Speaking Hindi")
'Important: As you expect, any call to Speak() inside this class
'will invoke the Speak() in this class. If you need to
'call Speak() in base class, you can use MyBase keyword.
'Like this
'Mybase.Speak()
End Sub
End Class
Carefully examine the code in Class MainClass
'Our main function
Shared Sub Main()
'Let us define Tom as a human (base class)
Dim Tom as Human
'Now, I am assiging an Indian (derived class)
Tom=new Indian
'The above assignment is legal, because
'Indian IS_A human.
'Now, let me call Speak as
Tom.Speak()
'Which Speak() will work? The Speak() in Indian, or the
'Speak() in human?
'The question arises because, Tom is declared as a Human,
'but an object of type Indian is assigned to Tom.
'The Answer is, the Speak() in Indian will work. This is because,
'most object oriented languages like Vb.net can automatically
'detect the type of the object assigned to a base class variable.
'This is called Polymorphism
End Sub
End Class
Lesson 8: Constructors & DestructorsImport the Imports System
A Constructor is a special function which is called automatically when a class is created. In VB.NET, you should use
Class Dog
'The age variable
Private Age as integer
The default constructor: Public Sub New()
Console.Writeline ("Dog is Created With Age Zero")
Age=0
End Sub
The parameterized constructor: Public Sub New(val as Integer)
Console.Writeline ("Dog is Created With Age " + Convert.ToString(val))
Age=val
End Sub
This is the destructor: Overrides Protected Sub Finalize()
Console.Writeline ("Dog is Destroyed")
End Sub
'The Main Function
Shared Sub Main()
Dim Jimmy, Jacky as Dog
'Create the objects
'This will call the default constructor
Jimmy=new Dog
'This will call the parameterized constructor
Jacky=new Dog(10)
End Sub
'The Destruction will be done automatically, when
'the program ends. This is done by the Garbage
'Collector.
End Class
Lesson 9: Property RoutinesYou can use both properties and fields to store information in an object. While fields are simply Imports System
Public Class Dog
'A private variable to hold the value
Private mAgeOfDog as Integer
This is our property routine: Public Property Age() As Integer
'Called when someone tries to retreive the value
Get
Console.Writeline ("Getting Property")
Return mAgeOfdog
End Get
Set(ByVal Value As Integer)
'Called when someone tries to assign a value
Console.Writeline ("Setting Property")
mAgeOfDog=Value
End Set
End Property
End Class
Another class: Class MainClass
'Our main function. Execution starts here.
Shared Sub Main()
'Let us create an object.
Dim Jimmy as Dog
Jimmy=new Dog
'We can't access mAgeofDog directly, so we should
'use Age() property routine.
'Set it. The Age Set routine will work
Jimmy.Age=30
'Get it back. The Age GEt routine will work
Dim curAge=Jimmy.Age()
End Sub
End Class
Lesson 10: A simple programLet us analyze a simple program. First, let us import the required namespaces: Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
'We are inheriting a class named SimpleForm, from the
'class System.Windows.Forms.Form
'
'i.e, Windows is a namespace in system, Forms is a
'namespace in Windows, and Form is a class in Forms.
Public Class SimpleForm
Inherits System.Windows.Forms.Form
'Our constructor
Public Sub New()
'This will invoke the constructor of the base
'class
MyBase.New()
Set the Me.Text = "Hello, How Are You?"
End Sub
End ClassPublic Class MainClass
Shared Sub Main()
'Create an object from our SimpleForm class
Dim sf as SimpleForm
sf=new SimpleForm
'Pass this object to the Run() function to start
System.Windows.Forms.Application.Run(sf)
End Sub
End Class
That is it. Now you can atleast read and understand most of those VB.NET source code, and probably implement more OOP features in your VB.NET programs. Now, in my next article, I'll try to cover the patterns and practices in VB.NET. History
| ||||||||||||||||||||