Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 11th, 2005, 06:32 AM
Authorized User
 
Join Date: Oct 2004
Location: Bristol, Nth. Somerset, United Kingdom.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default Polymorphism with Interfaces

I'm going through a VB.NET book at the moment and I've come to a part about Interfaces. The book defines an Interface as follows:

Publice Interface IDriveable
    Sub GoForward(ByVal Speed As Integer)
    Sub Halt()
    Function DistanceTraveled() as Integer()
End Interface

It then goes on to show a normal use with:

Implements IDriveable

However it then shows under the title of "Polymorphism with Interfaces" the following:

Public Sub GoSomewhere(ByVal As IDriveable)
' implementation omitted
End Sub

Anthis is where I get stuck how whould you implement an Interface when its pasted as a parameter. I tried defining it as:

Dim x as IDriveable
x.FuelLevel = 10

And that produced a runtime error of 'SystemNullReferenceException'. I then tried to define it as:

Dim x = new IDriveable

That gave a design time error of not being able to use new with an Interface.

Any ideas?

But that gives a runtime error
Reply With Quote
  #2 (permalink)  
Old May 11th, 2005, 11:11 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can not create an instance of an Interface because it's an abstract concept.

The GoSomewhere method is not expecting an instance of an interface, but rather it is saying "Please pass me an instance of a class that implements IDrivable".

So if we have these classes:

Class Car
   Implements IDriveable
End Class

Class Boat
   Implements IDriveable
End Class

and this method:

Public Sub GoSomewhere(ByVal As IDriveable)
' implementation omitted
End Sub

We can do this:

Dim myTruck As New Car
Dim myShip As New Boat

GoSomewhere(myTruck)
GoSomewhere(myShip)

This works because Car and Boat each implement the interface that the method expects. Think of it like this: The abstract 'instance' of the interface is piggy-backed on top of the real instance of the class. This way you can do something that is related to the interface without caring what the class type is of the actual object instance.

A very practical example of this that you might not even be aware that you use all the time is IEnumerable. Whenever you use a ForEach, .NET is asking the object that you iterate thru for it's GetEnumerator method that is provided by the IEnumerable interface. ForEach doesn't care if it is working with an array or a collection as long as the type it's dealing with implement IEnumerable.

-Peter
Reply With Quote
  #3 (permalink)  
Old May 11th, 2005, 01:50 PM
Authorized User
 
Join Date: Oct 2004
Location: Bristol, Nth. Somerset, United Kingdom.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for that excellent reply.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
need of polymorphism? pavanonforums C++ Programming 4 February 22nd, 2018 03:36 AM
Need help with Polymorphism Piash Java Basics 1 August 8th, 2007 06:24 AM
interfaces malli_kv2 Java Basics 1 April 23rd, 2007 06:21 AM
What is Interface Polymorphism in C#? ramk_1978 C# 1 May 3rd, 2006 02:23 AM
[CH04] Interfaces Norm 2782 BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 3 June 16th, 2005 05:56 AM



All times are GMT -4. The time now is 01:58 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.