Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Inheritance - Calling previous methods


Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2012, 07:44 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,272
Default Inheritance - Calling previous methods


Is this possible?

Say I have class ClassA...
Code:
Public Class ClassA

    Protected Overridable Sub DoSomething()
        messagebox.show("This is ClassA")
    End Sub
End Class
I then have ClassB that inherits ClassA...
Code:
Public Class ClassB
    Inherits ClassA

    Protected Overrides Sub DoSomething()
        messagebox.show("This is ClassB")
    End Sub
End Class
I then have ClassC that inherits from ClassB... BUT... I want DoSomething to do ClassA's DoSomething.
Code:
Public Class ClassC
    Inherits ClassB

    Protected Overrides Sub DoSomething()
        ' I want this to do ClassA's DoSomething
    End Sub
End Class
If I call MyBase.DoSomething in ClassC's DoSomething then it will do ClassB's DoSomething.

Is it possible to get back to ClassA's DoSomething from ClassC?

I wondered if casting to ClassA might be the way to go but the IDE's not happy because the method is protected so I couldn't get any further to find out if that would work.

Do I need to move the code from ClassA's DoSomething into another method which ClassC's DoSomething can call or is there a better solution?
__________________
There are no computers in heaven!
Reply With Quote
  #2  
Old 07-10-2012, 08:30 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

You can't do it without putting a method in A that can be called separately. I wouldn't recommend it from a philosophical standpoint: if a derived class needs to know intimate implementation details of its entire hierarchy you've found some coupling that needs to be resolved. Casting won't work because polymorphism guarantees you'll call the most specific method unless "Shadows" is in play. Calling your base implementation means you're saying, "I don't do anything to satisfy the contract but need to do some extra bookkeeping."

I'd like to hear more details about the specifics before delving into a solution, but my guess is the thing A is doing should be a separate function and is part of a larger logical responsibility that could be its own class depending on its complexity and importance.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 07-10-2012, 08:33 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is online now
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Wigan, UK
Posts: 1,676
Default

If Class C isn't meant to use Class B's implementation then either Class C really shouldn't be inheriting from Class B or the method override doesn't belong in Class B.

Is the DoSomething method something that makes sense to exist in all three classes and if so is the fact Class B overrides it also making sense as opposed to just being "convenient".

It is a bit difficult to give suggestions without an idea of what the classes and method in question really are however.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #4  
Old 07-10-2012, 09:00 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,272
Default

I'll try and explain what's going on as best I can, but it is kinda complicated and might take a lot of text to explain.

It's ultimately part of a stock control system. The part I'm dealing with here is the picking. Going from very simple to more complicated. The most simple, i.e. the base class's, implementation does not bother with quantities of parts. It is just cross reference. So when the user enters a part they want to pick it just shows the user the list of locations known to the system and they choose the one they want.

The next iteration of the system introduces quantities. This changes the picking because after entering a part they want to pick they have to say how many they want to pick. The user no longer chooses what location they want to pick from, the system does that for them (based on FIFO). So this second iteration overrides when a part number has been entered and goes about getting the quantity and working out the location before going back to the base class's methods for the rest of the pick.

The third iteration introduces users (for transaction logging, i.e. traceability) as well as being able to gather other information (such as a Transaction reference, just another form of traceability for the users). So it overrides bits of the second iteration to do its own things before returning to the base to record stuff.

What's happened now is that a customer kind of needs this third iteration to work without quantities. So, I need the functionality of the first iteration but with the extra bits that have been added for this third iteration.

So I saw that I had 3 choices. One was to go entering loads of code into the third iteration to try and cope with this, but the problem there is that I'd have to do loads of testing to make sure I hadn't broke the original functionality. That's something I like about inheritance. I don't have to touch what is already there so I know it's still going to work.

The other two choices are where I inherit from. I could inherit the first iteration's class for this but that means I'd have to add the extra functionality that the third iteration does (like the users, the transaction ref, etc.) and that seems like more work than is required.

So the third choice is to inherit the third iteration's but make it call the first iteration's method at the point where the second overrides it (doubt that makes sense).

Moving the code in the first iteration into a different method for the fourth iteration to call has worked, I just wasn't too sure if there was a better way.
__________________
There are no computers in heaven!

Last edited by DrPunk; 07-10-2012 at 09:06 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->