JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Using Multiple Interfaces

As discussed in the In Depth section of this chapter, Visual Basic does not support multiple inheritance, where you inherit from multiple base classes at the same time. Like Java, however, Visual Basic lets you implement multiple interfaces at the same time, which is a dressed-down version of multiple inheritance. We saw this in the Interfaces example in the In Depth section of this chapter; in this example, we constructed two interfaces, person and executive:

Public Interface person
    Sub SetName(ByVal PersonName As String)
    Function GetName() As String
End Interface

Public Interface executive
    Sub SetTitle(ByVal PersonName As String)
    Function GetTitle() As String
    Sub SetName(ByVal ExecutiveTitle As String)
    Function GetName() As String
End Interface

Then I implemented these interfaces in a class named vicepresident-note that one method can implement multiple interface methods:

Public Class vicepresident
    Implements person, executive
    Dim Name As String
    Dim Title As String
    Sub SetTitle(ByVal ExecutiveTitle As String) Implements _
        executive.SetTitle
        Title = ExecutiveTitle
    End Sub

    Function GetTitle() As String Implements executive.GetTitle
        Return Title
    End Function

    Sub SetName(ByVal PersonName As String) Implements _
        person.SetName, executive.SetName
        Name = PersonName
    End Sub

    Function GetName() As String Implements person.GetName, _
        executive.GetName
        Return Name
    End Function
End Class

I used this new vicepresident class in the Interfaces example, like this:

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
        Dim Sam As New vicepresident()
        Sam.SetName("Sam")
        Sam.SetTitle("vice president")
        TextBox1.Text = "You created " & Sam.GetName() & ", " & _
            Sam.GetTitle ()
    End Sub
End Class

You can see the results of this code in Figure 12.4.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor