Object Oriented Programming for VB.NET - Part 1
Pages: 1, 2, 3
Static Members
Looking at the Employee class in Listings 4, 5 and 6 again, you may wonder why we can use the Write method of the System.Console class without first instantiating a System.Console object. This is because in OOP languages there is a special type of member called a static member. The term shared is also used in VB.NET.
Static means the member is available without having to instantiate an object. For example, the class called SalaryLevel provides static fields only in Listing 7.
Listing 7: Static members in a class
Class SalaryLevel
Public Shared Level1 As Decimal = 35000
Public Shared Level2 As Decimal = 40000
Public Shared Level3 As Decimal = 45000
Public Shared Level4 As Decimal = 50000
Public Shared Level5 As Decimal = 55000
Public Shared Level6 As Decimal = 60000
Public Shared Level7 As Decimal = 65000
Public Shared Level8 As Decimal = 70000
Public Shared Level9 As Decimal = 75000
Public Shared Level10 As Decimal = 80000
End Class
You can then use the class in your program, as illustrated in Listing 8.
Listing 8: Using a static member of a class
Imports System
Class SalaryLevel
Public Shared Level1 As Decimal = 35000
Public Shared Level2 As Decimal = 40000
Public Shared Level3 As Decimal = 45000
Public Shared Level4 As Decimal = 50000
Public Shared Level5 As Decimal = 55000
Public Shared Level6 As Decimal = 60000
Public Shared Level7 As Decimal = 65000
Public Shared Level8 As Decimal = 70000
Public Shared Level9 As Decimal = 75000
Public Shared Level10 As Decimal = 80000
End Class
Class Employee
Dim yearlyBonus As Decimal = 4000
Public Sub PrintSalary()
' print the salary to the Console, use the static field of SalaryLevel
Console.Write(SalaryLevel.Level4)
End Sub
Public Shared Sub Main()
Dim employee As Employee
employee = New Employee()
employee.PrintSalary()
End Sub
End Class
In the PrintSalary method of the Employee class, we can use the Level4 static field in the SalaryLevel class without first creating an object of type SalaryLevel. Members that are not static are called instance members.
Constructors
A constructor is a special method that must be present in a class for the class to get instantiated. In VB.NET, this method is called New. But, as you have seen, there is no New method in the classes in the previous code. That's right. When there is no constructor present, VB.NET will create one for you. When you instantiate an object by using the New keyword, the class's constructor is called. You can provide initialization code that is guaranteed to run when the object is instantiated.
If you write a constructor explicitly in your class, VB.NET won't create it anymore.
Inheritance
Inheritance is a feature that allows you to extend a class. If you need some functionality, you can create a brand new class. If part of the functionality you need has been provided in a class written by someone else, however, you can then write a new class that extends the original class. Your class is called the child class or derived class, and the original class is called the parent class or the base class. The process of extending a class is called extension. Sometimes, the term subclass or inherit is used to describe the act of extending a class. In VB.NET a class can only extend one parent class. Multiple class inheritance is not allowed in VB.NET.
Syntactically, extending a class is done using a semicolon after the class name, followed by the word Inherits and the parent class name. For example, Listing 9 shows how we extend the class Employee to create a new class called Manager.
Listing 9: Extending a class
Imports System
Class Employee
Dim salary As Decimal = 40000
Dim yearlyBonus As Decimal = 4000
Public Sub PrintSalary()
' print the salary to the Console
Console.Write(salary)
End Sub
End Class
Class Manager: Inherits Employee
End Class
If the word Inherits appears on the next line, you don't need the semicolon.
Class Manager
Inherits Employee
End Class
Now, you can instantiate a Manager object and use members from the Employee class.
The code is given in Listing 10.
//////QUESTION: IMPORTS SYSTEM WAS STUCK IN THE ABOVE GRAF. IT PROBABLY BELONGS AS TEH FIRST LINE OF CODE. CHECK WITH AU.//////////
Listing 10: Instantiating a Manager object
Imports System
Class Employee
Public salary As Decimal = 40000
Public yearlyBonus As Decimal = 4000
Public Sub PrintSalary()
' print the salary to the Console
Console.Write(salary)
End Sub
End Class
Class Manager: Inherits Employee
End Class
Module Module1
Public Sub Main()
Dim manager As Manager
manager = New Manager()
manager.PrintSalary()
End Sub
End Module
The example in Listing 11 shows how we can extend the Manager class by writing a new method called PrintBonus.
Listing 11: Adding a method to the derived class
Class Manager: Inherits Employee
Public Sub PrintBonus()
Console.Write(yearlyBonus)
End Sub
End Class
Note that member accessibility restriction applies. For example, you can't make the yearlyBonus field private, since this field is not accessible from the Manager class. Therefore, trying to compile the code will result in an error.
Inheritance is a common practice. In fact, the .NET Framework class library consist of hierarchies of classes that are derived from other classes. For example, the Button class in the Windows.Forms namespace is a child class of ButtonBase, which itself is a child of Control. All classes will eventually have the System.Object class as its root. System.Object is called the root or the superclass in the .NET Framework class library.
The power of inheritance can be demonstrated by the code in Listing 12.
Listing 12: Extending System.Windows.Forms.Form
Public Class MyForm : Inherits System.Windows.Forms.Form
End Class
This blank class declaration, when compiled and run, will display a Windows form. You can create a form without a single line of code. This is because MyForm inherits System.WIndows.Forms.Form. It inherits the functionality of the Form class.
NotInheritable Classes
You can prevent classes from being inherited by using the NotInheritable keyword. For example, the code in Listing 13 is a class called Calculator that cannot be inherited.
Listing 13: A non-inheritable class
NotInheritable Class Calculator
End Class
Trying to extend this class will raise a compile error. Why would you want to make your class non- inheritable? In some cases, you may not want other people to be able to extend your class. Another reason is that an inheritable class produces faster code. You should use inheritable classes with caution, however, because noninheritance defeats the purpose of OOP. Only use it if you are 100% sure that you will not extend the class. This type of class is also called a final class in some other OOP languages.
Part 2 of this series can be found here
Return to the .NET DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 26 of 26.
-
about this article
2004-04-23 00:08:43 bvijay [Reply | View]
This article is very useful for beginers who is learning vb.net. thanks
-
MS version of Java?
2003-11-05 10:24:51 anonymous2 [Reply | View]
This article makes me feel like seeing the Microsoft version of Java. It is helpful of learning about concepts of Visual Basic.NET.
-
OOP
2003-08-27 20:07:07 anonymous2 [Reply | View]
I have read alot about Class, but still can not figure out real world usage,
-
oop explained in 3 pages...wow
2003-03-31 14:03:21 anonymous2 [Reply | View]
Everything ive ever needed to know about computers ive learned throuhg O'rielly.
-
No, what is the point?
2003-03-17 22:16:47 anonymous2 [Reply | View]
It appears that VB OOP is making programming more difficult just for its own sake. It does not save development time. If you wanted an OOP language where you spend enormous amounts of time trying to accomplish trivial things, there is already a language for that, it is called C++.
-
I LOVE YOU
2003-01-29 02:08:58 anonymous2 [Reply | View]
MY NAME IS ZAHOOR AHAMD
I HOPE THAT YOU R FINE
-
I LOVE YOU
2003-01-29 02:08:35 anonymous2 [Reply | View]
MY NAME IS ZAHOOR AHAMD
I HOPE THAT YOU R FINE
-
I LOVE YOU
2003-01-29 02:08:19 anonymous2 [Reply | View]
MY NAME IS ZAHOOR AHAMD
I HOPE THAT YOU R FINE
-
Still confused
2003-01-26 11:52:09 anonymous2 [Reply | View]
I bought Learning VB.NET from O'Reilly...got 1/2 way through the book and realized I understood nothing. While this site made 1 or 2 things a little clearer, I'm still lost.
I believe code samples should be hi-lited or commented...in excess.
Example:
' this is the constructor
Blah new Blah Blah
' this is the end of the constructor
I think I understand the concept of OOP, but once there's code involved I get totally lost. That's why I believe hi-liting and/or commenting as much as possible, perhaps more than what's necessary, is important.
The article states something along the lines of "...there was no constructor defined in the class, so VB.NET created one automatically..."
Since I have no idea what or where a constructor would go, I'd never notice that it was missing from the Class definition.
I believe there are a couple of typos in the article, too. It states that you should use a semicolon before the Inherits statement, but all the code samples show a colon, not a semicolon.
It also asks "why would you want to make a class UNinheritable?" One of the answers is "because INheritable code compiles faster." That makes no sense (to an OOP noob wanna-be like me, anyway). If inheritable is faster, why would I want to slow things down by making a class uninheritable? Isn't faster code better?
I'll keep reading, and re-reading until I get it though.
Thank you
-
Good Start for Oops
2003-01-22 23:02:49 anonymous2 [Reply | View]
Hi,
This really acts as a good start for Oops understanding.
Thnaks
-
Inheritance
2003-01-11 03:53:50 anonymous2 [Reply | View]
From my testing it seems that multiple class inheritance IS allowed in VB.NET.
-
VB.Net OOP
2002-11-26 13:24:27 anonymous2 [Reply | View]
Kudos!!
Article excellent. Examples simple and right-on.
Sorry, but don't understand "friend" explanation.
Thanks
andrews_gary_w@solarturbines.com
-
OOP and VB
2002-11-24 15:26:56 anonymous2 [Reply | View]
Finaly VB has evolved into a language with some real functionality. I am a C++ developer and used to stay away from VB for the simple rason that classes were so cumbersome to create. I will now consider using VB once again(only for the simpler Syntax, no ; and {} to worry about -
caculator
2007-09-20 00:04:47 Ashoaib [Reply | View]
I want to make a simple calculator in vb.net2005
could any one help me I just want the code to for add button and equals button and also for four more button that do the simple operation and also how and where I define the variables






VB .NET seems to implement all the OOP functionality in a pragmatic and functional way. It seems much more rubust and much more simple than JAVA. Together with Visual Studio it seems to be the ideal rapid prototyping environment. Too bad Visual Studio doesn't allow patching programs on the fly like VBA does (you can break directly in the program, modify a line or few and keep on executing).
As for the article. Anyone who has understood some of the aspect of OOP gan grasp what is said straight away. But is it thanks to the BASIC heritage of VB or is it thanks to the author? Difficult to say.