Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,
I want to define a (super)class that hides the code of several (sub)classes, which all provides the same methods, but with different code.

Simplified example:
In an application "Form1" I instance the (Super)class "Printer" with the argument "PrinterType" (enum). This "Printer" class provides the "Print()" method. The code for printing on different hardware should be coded in separate classes. So the "Printer" class has to use/inherit one of the specific (sub)classes.

The only way I found is to declare the Print() method in the (sub)classes as shared. But this is for me as defining a Sub() in a public module.
I'm sure there is a better way of programming as my workaround.

What I have tried:

VB.NET
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim oPrinter As New Printer(Printer.PrinterType.HP)
        oPrinter.Print()
    End Sub
End Class


Public Class Printer

    Public Enum PrinterType
        HP
        Epson
    End Enum

    Public ActivePrinter As PrinterType

    Public Sub New(ByVal mPrinterType As PrinterType)
        ActivePrinter = mPrinterType
    End Sub

    Public Sub Print()
        Select Case ActivePrinter
            Case PrinterType.HP
                HP_Printer.Print()
            Case PrinterType.Epson
                Epson_Printer.Print()
        End Select
    End Sub

End Class


Public Class HP_Printer
    Public Shared Sub Print()
        Beep()
    End Sub
End Class


Public Class Epson_Printer
    Public Shared Sub Print()
        Beep()
    End Sub
End Class
Posted
Updated 27-Nov-16 3:10am
Comments
Richard MacCutchan 27-Nov-16 7:27am    
Why would you need different versions of the Print code, when it all goes to the same place (Windows spooler) before being handed off to the device driver?

If I understand the question correctly, you're looking for Inherits Statement[^]

Few tutorials you might find useful
- Inheritance from a Base Class in Microsoft .NET[^]
- Inheritance Basics (Visual Basic)[^]
- VB.Net - Classes & Objects[^]

[ADDED]
Based on your comment in Solution 3 I'm not sure if you're after multiple inheritance. If that is the case, then it's not supported in .Net

I'm not sure if you actually mean generics. This would help you to have common functionality while still operating with different classes. Have a look at Generic Types in Visual Basic (Visual Basic)[^]
 
Share this answer
 
v2
Derive your classes HP_Printer and Epson_Printer from your base Printer class, and they can override the base class implementation to provide model-specific Print functions.

Inheritance from a Base Class in Microsoft .NET[^] and Inheritance Basics (Visual Basic)[^] may help.
 
Share this answer
 
Dear Experts,

Richard
of course this is only a easy understandable example, the problem in my application has nothing to do with Windows printers.

Mika, OriginalGriff
I think I understand the Inherits statement, it means I create a new class which uses code of the base (parent) class. But to instance this new class I have to create an object with the type of the new class. I need the opposite way.
I look for a way that the base class can inherit methods from different classes depending on an argument of the NEW() method.

Please try to correct my lines of code in that way.
 
Share this answer
 
Comments
Wendelius 27-Nov-16 13:27pm    
Instead of posting a new solution always use the "Have a Question or Comment?" to comment on given solutions. This way the author of the solution will be notified.

Having that said, have a look at updated solution 1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900