Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Leverage VB.NET's Object-Oriented Features
Write elegant and easily maintainable code by taking advantage of inheritance and other object-oriented capabilities.
by Francesco Balena

February 2003 Issue

Technology Toolbox: VB.NET

ADVERTISEMENT

One of the most compelling reasons for switching from VB6 to VB.NET is VB.NET's full support for object-oriented programming (OOP). However, you need to do more than learn some new keywords to take advantage of this capability. The many options you now have can be puzzling. I'll show how you can use object-oriented features in your applications. I won't explain every new feature in depth (a task this entire magazine couldn't accommodate), and I'll provide code examples that contain remarks in lieu of executable code to draw your attention to general concepts. You might be unfamiliar with some words I use, so I've provided a glossary of the most common OOP terms (see the sidebar, "An Object-Oriented Glossary").

The first new object-oriented language feature you're likely to use extensively in your apps is method overloading. VB.NET lets you define more than one method or property with a given name, provided the versions differ in their argument signatures; that is, the number or type of their arguments must be different. For example, a class can define a GetItem method that takes either a numeric index or the string key you associate with the element to be returned:

Function GetItem(ByVal index As _
   Integer) As Object
   ' return an element by its index
End Property

Function GetItem(ByVal key As String) _
   As Object
   ' return an element by its key
End Property

The compiler generates the call to the correct version by looking at the argument's type:

res = obj.GetItem(1)     ' numeric key
res = obj.GetItem("Joe") ' string key

Method overriding is especially useful when you have a highly generic method that can take any data type—for example, a Log method that appends the argument's value to a text file. You might be tempted to define a single version that takes an Object argument, because you want to pass any type of data to this method:

Sub Log(ByVal value As Object)
   ' TW is a TextWriter object
   tw.Write("LOG:" & value.ToString())
End Sub

However, you force a hidden boxing operation if you pass a value type argument—a number, a date/time, a Boolean, or a structure—to an Object argument. The .NET runtime must wrap an object around the value—an operation that allocates memory from the managed heap and wastes precious CPU cycles.

Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home