Introduction to VB.NET Object-Oriented Features
Pages: 1, 2, 3
Interfaces
There are times when you do not want to code the implementation within a class. Interfaces provide a powerful mechanism to separate the definitions of objects from actual implementation. Listing 9 shows three interfaces. Note that only the definitions of methods and properties are present, not the implementation codes.
Listing 9. Vehicle Interface
Interface Vehicle
Sub Start()
Sub Brake()
Sub TurnLeft()
Sub TurnRight()
Sub Reverse()
Sub Accelerate()
Function GetSpeed()
Property wheels() As Integer
End Interface
Interface Car
Inherits Vehicle
End Interface
Interface Dimension
Property length() As Single
Property breadth() As Single
End Interface
Listing 10 shows the SportsCar class implementing (using the Implements keyword) the Car and Dimension interfaces. A class that implements an interface must provide implementations for that interface.
Listing 10. SportsCar Class
Public Class SportsCar
Implements Car
Implements Dimension
Public Sub start() Implements Car.Start
' implementation codes here
End Sub
Public Sub Brake() Implements Car.Brake
' implementation codes here
End Sub
Public Sub turnleft() Implements Car.TurnLeft
' implementation codes here
End Sub
Public Sub turnright() Implements Car.TurnRight
' implementation codes here
End Sub
Public Sub reverse() Implements Car.Reverse
' implementation codes here
End Sub
Public Sub accelerate() Implements Car.Accelerate
' implementation codes here
End Sub
Public Function getspeed() Implements Car.GetSpeed
' implementation codes here
End Function
Property wheels() As Integer Implements Car.wheels
Get
' implementation codes here
End Get
Set(ByVal Value As Integer)
' implementation codes here
End Set
End Property
Property breadth() As Single Implements Dimension.breadth
Get
' implementation codes here
End Get
Set(ByVal Value As Single)
' implementation codes here
End Set
End Property
Property length() As Single Implements Dimension.length
Get
' implementation codes here
End Get
Set(ByVal Value As Single)
' implementation codes here
End Set
End Property
End Class
Listing 10 also shows that all of the methods and properties defined by the two interfaces are defined by the SportsCar class. Unlike class inheritance, multiple inheritance in interfaces is allowed, like the following:
Interface Truck
Inherits Car
Inherits Dimension
End Interface
Namespaces
The .NET framework uses a dot syntax to group class libraries into easily recognized groups. This naming convention is known as a namespace. For example, the System.Xml.Xsl namespace contains classes that perform XSL transformation. Likewise, in your application you may have many classes, each performing different functions. You may run into name collision, where you have one or more classes with the same name. In this case, you can organize your classes using Namespace:
Namespace Graph
Public Class Point
' Members declaration here
End Class
End Namespace
Namespace Direction
Public Class Point
' Members declaration here
End Class
End Namespace
To use the classes with namespaces, you should specify the full namespace:
Dim ptX As New Graph.Point()
Dim goNorth As New Direction.Point()
Note: you can also reduce the amount of typing by using the Imports statement:
Imports MyApplication.Graph
Dim ptX As New Point()
I will talk more about namespaces in a future article.
The Set Keyword
In Visual Basic 6.0, object assignment requires the use of the Set keyword. This has created some confusion for developers who sometimes are just not sure whether to use the Set keyword.
' Visual Basic 6.0
Dim cmdButton As CommandButton
Set cmdButton = Command1
In VB.NET, Microsoft has removed this confusion, and the Set keyword is no longer supported (do not confuse this Set with the one in Property).
' Visual Basic .NET
Dim cmdButton As Button
cmdButton = Button1
Summary
I hope you now have a good grounding in OO concepts in VB.NET. The best way to learn OO concepts is through examples and some experimentation. Go ahead, try it out yourself, and you will very soon discover that you have learnt much more than you have expected.
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to ONDotnet.com

