Wrox Programmer Forums
|
BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003
This is the forum to discuss the Wrox book Professional VB.NET 2003 by Bill Evjen, Billy Hollis, Rockford Lhotka, Tim McCarthy, Jonathan Pinnock, Rama Ramachandran, Bill Sheldon; ISBN: 9780764559921
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 10th, 2004, 04:17 PM
Authorized User
 
Join Date: Jul 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question on chapter 10 - overriding

Hi all,

Just a quick question - I think I am right but want to confirm.

One page 353 the book mentions how you can still call the original method if you specifically call it from within the new method (overriding the original) by using Base.MethodName etc.

I understand this for 2 objects, but lets say I had 3 objects, and they were something like this:

Object 1
--->Object 2 (inheriting from Object 1)
------->Object 3 (inheriting from Object 2)

Now, if I wanted to do what the book is talking about - does Base.Methodname refer to the object that this object inherits from - or rather the 'first' object in the series of objects.

Ie, all 3 objects above were to have the same method (each doing something slightly different) - and each one overrides the other, if in Object 3 I then called a method using Base.MethodName etc - would I be correct in saying that it would take the MethodName from Object2 and not Object1?

Sorry if I've overly complication my question by trying to explain it



Regards

Rob
__________________
Regards

Rob
 
Old August 11th, 2004, 04:30 AM
Thearon's Avatar
Wrox Author
 
Join Date: Dec 2003
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

Rob,

You are correct, Object3 would only call the base method in the class that it is inheriting (Object2). However, Object2 could in turn call the base method in the class that it is inheriting (Object1) and return the results from that method.

As a side note, the book had a typo and it should be MyBase.MethodName instead of Base.MethodName.

Here's an example:

Public Class Class1
    Overridable Function ReturnString() As String
        Return "From Class1"
    End Function
End Class

Public Class Class2
    Inherits Class1

    Overrides Function ReturnString() As String
        Return MyBase.ReturnString & ", From Class2"
    End Function
End Class

Public Class Class3
    Inherits Class2

    Overrides Function ReturnString() As String
        Return MyBase.ReturnString & ", From Class3"
    End Function
End Class

In a form add this code for a Button click event:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objTest As New Class3
        Debug.WriteLine(objTest.ReturnString)
    End Sub

The results that you'll see are:

From Class1, From Class2, From Class3

I hope this helps.

Thearon
 
Old August 11th, 2004, 06:06 AM
Authorized User
 
Join Date: Jul 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Thearon,

Many thanks for your prompt and informative reply, and for the heads up on the typo - I'm sure I'd have spotted that if I wasn't up so late reading the book! :)

Incidentally, can I take it that you are 'Thearon' from the book, ie the chappy on the left? Just curious as its always nice to put a face to a name etc.

Thanks again


Regards

Rob
 
Old August 11th, 2004, 04:29 PM
Thearon's Avatar
Wrox Author
 
Join Date: Dec 2003
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

Rob,

You're welcome and yes that's me.

Thearon
 
Old August 12th, 2004, 11:56 AM
Authorized User
 
Join Date: Jul 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nice to meet you - even if virtually :)


Regards

Rob





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 10 gogeo BOOK: Beginning Access 2003 VBA 1 January 22nd, 2006 09:41 AM
Another chapter 10 question. czambran BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 4 April 3rd, 2005 09:12 PM
Chapter 10 columbiasmiles JSP Basics 0 May 17th, 2004 08:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.