developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Auto Insurance Quote
Logo Design
Memory Upgrades
Laptops
Online Shopping
Hurricane Shutters
Domain registration
Promotional Products
Server Racks
Find Software
Computer Deals
Disney World Tickets
Promotional Golf
Shop

 


Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

Implementing Interfaces in VB .NET
By Paul Kimmel

What difference a year or two can bring. Two and a half years ago I thought I'd be writing a book titled Visual Basic 7 Unleashed for Sams. Completing the final drafts of Sams Visual Basic .NET Unleashed reminds me that a lot has changed in Visual Basic.

To help you make the transition as thoroughly and as completely as possible myself and other authors and contributors are writing a lot about threading, reflection, assemblies, COM interop, and delegates. But, reviewing programming subjects with a friend recently, I was reminded that there are developers at all levels, not just the advanced level. To be as thorough as possible, then, I am exploring advanced topics as well as non-advanced topics. (I welcome queries from readers too, and sometimes write an article based on several queries.)

This article is to the point. In this article we will examine interfaces: how to define them and how to implement them.

The idea of an interface is not new to COM, but in Visual Basic 6 every class was a COM interface. In Visual Basic .NET every class is a class and an interface is an interface, but not a COM interface. Classes have existed for decades and interfaces are not the sole purview of COM. A class describes the fields, properties, events, and methods a type will have. An interface is more like a portal or an attachment. An interface says that a type will implement these specific methods, but does not define what a type is. For example, a house can have a Window but so can a car. A type that implements a method open could be defined as something that has a Window, but the types could define cars, boats, houses, or the soul.

Visual Basic 6 does not support inheritance or classes in the object-oriented sense of the construct. VB6 does support COM interfaces. VB .NET, on the other supports classes and interfaces, so a distinguishment had to be made between the two idioms.

Defining Classes and Interfaces in VB .NET

The class and interface idioms use a very similar syntax when you define them. The following example defines an empty class in VB .NET, followed by an empty interface.

Public Class AClass

End Class

Public Interface AnInterface

End Interface

Classes can contain fields, properties, events, and methods. These elements of a class, called members, can have modifiers indicating that they are public, private, protected, or friend. All members of an interface declaration are public and as a result do not need nor can they have access modifiers.

Classes contain code; interfaces do not. However, classes that implement an interface do contain code. Keep in mind that there are no instances of interfaces in VB .NET. Every instance is a type that implements an interface, but is itself not an instance of the interface. (From this point we will leave the discussion of classes for another time and focus only on interfaces.)

Implementing Interfaces

Assuming we have an interface named AnInterface, we can only add method declarations to that interface. Extending the interface from the previous section, we can add a method named WhoAmI. The result is shown next.

Public Interface AnInterface
  Function WhoAmI() As String
End Interface

All types that implement the AnInterface interface must implement every declared method in that interface. In this example we only need to implement the function WhoAmI. Suppose AClass implements AnInterface; we would need to implement WhoAmI. The result of implement AnInterface in AClass would yield the following code.

Public Class AClass
  Implements AnInterface

  Public Function WhoAmI() As String Implements AnInterface.WhoAmI
    Return "AClass"
  End Function

End Class

The first thing we have to do is indicate that we want to implement the interface by name. Implements AnInterface tells consumers that AClass will implement all of the methods described in AnInterface. (The Visual Studio .NET IDE reminds us that we have to do so too.)

The difference between VB6 and VB .NET is that we have to add the Implements clause to the function body as shown in the listing. The function is declared as normal, but the clause Implements AnInterface.WhoAmI completes the contract between the class and the interface.

Final Thoughts

Structures can implement interfaces in VB.NET too. Whether a class or a structure is implementing an interface, you will need the Implements statement as demonstrated, and you will need to implement every method defined in the interface using the Implements clause at the end of the procedure header to indicate that a particular method satisfies a particular interface method.

Interfaces can be very short or very long. Methods described by an interface can be subroutines or functions, and they can be as elaborate or as simple as you need them to be. One method can implement more than one interfaces method. Finally, keep in mind that interface methods can be called with a reference to the object or with a reference to the interface.

About the Author

Paul Kimmel is a freelance writer for Developer.com and CodeGuru.com. Look for cool Visual Basic .Net topics in his upcoming book Visual Basic .Net Unleashed available in January of 2002.

Paul founded Software Conceptions, Inc. in 1990. Contact Paul Kimmel at pkimmel@softconcepts.com for help building VB.NET applications or migrating VB6 applications to .NET.

# # #

Previous article: Random Numbers and Tic Tac Toe with Visual Basic .NET
Next article: Web Services in Visual Basic .NET


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Other Language Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Data Sheet: IBM Information Server Blade
Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.
Intel Go Parallel Portal: Translating Multicore Power into Application Performance
Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES