Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design 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 November 7th, 2006, 06:19 AM
Authorized User
 
Join Date: Jun 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ratheesh_param
Default when implementing two interfaces

hi all

 Can anyone tell me .. when we implement a class with two interfaces which have same methods.. which reference the class will take?


public interface Interface
 function fun() as string
 end function
end interface

public interface Interface1
 function fun() as string
 end function
end interface

public class : implements interface,interface1

public function classInter()As String Implements interface1.fun,interface.fun
      /* implementation */
end function
end class

This is the case.... here the ClassInter implements the fun() from which interface?
regards
Ratheesh

 
Old November 8th, 2006, 11:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

In both VB.NET and C# you need to implement two functions/methods in the type implementing the interfaces with the duplicate method name, one funtion/method for each version of the duplicate method:

The C# compiler supports "interface-qualified names" for methods, but the VB.NET compiler doesn't. So the VB.NET version would need to look something like this:

Code:
Public Interface InterfaceA
   Function ReturnAString() As String
End Interface

Public Interface InterfaceB
   Function ReturnAString() As String
End Interface

Class Class1
   Implements InterfaceA
   Implements InterfaceB

   Public Function InterfaceAReturnAString() As String _
         Implements InterfaceA.ReturnAString
      Return "InterfaceA implementation"
   End Function

   Public Function InterfaceBReturnAString() As String _
         Implements InterfaceB.ReturnAString
      Return "InterfaceB implementation"
   End Function
End Class

Module Module1

   Sub Main()
      Dim TheClass As Class1 = New Class1
      Dim TheString As String
      TheString = TheClass.InterfaceAReturnAString()
      TheString = TheClass.InterfaceBReturnAString()
   End Sub

End Module
In C# you would use interface-qualified method names in the type implementing the interfaces, then cast the type to the interface whose version of the method you want to execute. So the C# version would need to look something like this:

Code:
using System;

public interface InterfaceA {
    string ReturnAString();
}

public interface InterfaceB {
    string ReturnAString();
}    

class Class1 : InterfaceA, InterfaceB      {

    // Notice no access modifier. Method considered private.
    // Can't be called by a variable referencing the type
    // because the interface method is implemented using
    // an "ïnterface-qualified name".
    string InterfaceA.ReturnAString() {
        return "InterfaceA implementation";
    }

    string InterfaceB.ReturnAString() {
        return "InterfaceB implementation";
    }

    static void Main() {
        Class1 TheClass = new Class1();

        string TheString;
 
        // Return InterfaceA's implementation. Cast the reference
        // to TheClass to an InterfaceA, and only the method defined
        // by interfaceA is callable.
        TheString = ((InterfaceA)TheClass).ReturnAString();

        // Return InterfaceB's implementation.
        TheString = ((InterfaceB)TheClass).ReturnAString();        
    }
}



HTH,

Bob

 
Old November 9th, 2006, 12:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

In VB.NET you might want to use method names like:

Public Function InterfaceA_ReturnAString() As String _
         Implements InterfaceA.ReturnAString
      Return "InterfaceA implementation"
End Function

The underscore character helps clarify the type.member relationship in Class View, Object Browser, Intellisense, etc. by mimicing an interface-qualified name.

Bob

 
Old November 9th, 2006, 11:48 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by ratheesh_param
 hi all

Can anyone tell me .. when we implement a class with two interfaces which have same methods.. which reference the class will take?


public interface Interface
function fun() as string
end function
end interface

public interface Interface1
function fun() as string
end function
end interface

public class : implements interface,interface1

public function classInter()As String Implements interface1.fun,interface.fun
     /* implementation */
end function
end class

This is the case.... here the ClassInter implements the fun() from which interface?
regards
Ratheesh
Your classInter method is implementing both methods. If you were to call this from an object that is declared as Interface1 or Interface2 you would get the same results. If you need different results you can follow the code that Bob Bedell provided.

However, there is one aspect to his code that should be clarified. In VB.NET you are still able to call either method using its interface name (just like C#) as long as the instance is cast or declared as the type of the interface you are wanting to use, which of course is one of the main typical uses of implementing interfaces.

Dim a As New Class1
Debug.WriteLine(CType(a, InterfaceA).ReturnAString)
Debug.WriteLine(CType(a, InterfaceB).ReturnAString)

Woody Z http://www.learntoprogramnow.com
 
Old November 9th, 2006, 10:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Thanks Woody Z. And to carry your point a little further, I've declared InterfaceAReturnAString and InterfaceBReturnAString with Public scope, which allows calling them through either a variable referencing the type, or a variable referencing the type cast to the interface.

Using (CType(a, InterfaceA).ReturnAString) allows the two methods to be declared with Private scope. The remaining difference then between the VB.NET version and the C# version is that C#'s interface-qualified method names automatically enforce private scope on the method declarations.

Bob







 
Old November 10th, 2006, 01:52 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It would also be good to point out that you can not make Interface members static

Allowed:
string ReturnAString();

Disallowed:
static string ReturnAString();

And, if memory serves me, they must be declared with AT LEAST protected scope.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature





Similar Threads
Thread Thread Starter Forum Replies Last Post
returning interfaces mike72 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 April 17th, 2008 05:53 AM
interfaces malli_kv2 Java Basics 1 April 23rd, 2007 05:21 AM
Instantiating Interfaces simsdagr8 C# 2005 4 November 30th, 2006 07:37 AM
[CH04] Interfaces Norm 2782 BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 3 June 16th, 2005 04:56 AM
Polymorphism with Interfaces digby_dog VB.NET 2002/2003 Basics 2 May 11th, 2005 12:50 PM





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