When working with interfaces, an ambiguity can arise if a class implements two interfaces and each has a method with the same name and signature. As an example, consider the following versions of the interfaces IAccount and IStatement . Each interface contains the method Show . Interface IAccount Sub Deposit(ByVal amount As Decimal) Sub Withdraw(ByVal amount As Decimal) ReadOnly Property Balance() As Decimal Sub Show() End Interface Interface IStatement ReadOnly Property Transactions() As Integer Sub Show() End Interface How can a class that implements both of these interfaces specify implementations for both of the Show methods that have the same name? The answer is to use an alias for each of the method implementations in the derived class. You can use the interface name to qualify the method, as illustrated in the program Ambiguous . [7] In the Ambiguous example, the IAccount version of Show is named IAccount_Show , and the IStatement version of Show is named IStatement_Show . These method names are typically not used explicitly by client code. Rather, the client code usually calls on the name of the method as it is defined in the interfaces. IAccount_Show implements IAccount.Show and displays only the balance. IStatement_Show implements IStatement.Show and displays both the number of transactions and the balance. [7] Actually, any distinct names will do, but using the interface name as part of the method name helps make it more recognizable and meaningful. // Account.vb (project "Ambiguous") ... Public Class Account Implements IAccount, IStatement Private m_balance As Decimal Private m_numXact As Integer = 0 Public Sub New(ByVal balance As Decimal) m_balance = balance End Sub Public Sub Deposit(ByVal amount As Decimal) _ Implements IAccount.Deposit m_balance += amount m_numXact += 1 End Sub Public Sub Withdraw(ByVal amount As Decimal) _ Implements IAccount.Withdraw m_balance -= amount m_numXact += 1 End Sub Public ReadOnly Property Balance() As Decimal _ Implements IAccount.Balance Get Return m_balance End Get End Property Public Sub IAccount_Show() Implements IAccount.Show Console.WriteLine("balance = {0}", Balance) End Sub Public ReadOnly Property Transactions() As Integer _ Implements IStatement.Transactions Get Return m_numXact End Get End Property Public Sub IStatement_Show() Implements IStatement.Show Console.WriteLine(_ "{0} transactions, balance = {1}", _ m_numXact, Balance) End Sub End Class Here is the client code that exercises the IAccount and IStatement interfaces. For comparison, we show making the call through both an interface reference and an object reference. ' Ambiguous.vb Imports System Module Ambiguous Sub Main() Dim acc As Account = New Account(100) Dim iacc As IAccount = acc Dim istat As IStatement = acc iacc.Show() 'calls IAccountShow istat.Show() 'calls IStatementShow acc.IAccount_Show() acc.IStatement_Show() End Sub End Module |