Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > ASP.NET 1.x and 2.0 Application Design
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 7th, 2006, 06:19 AM
Authorized User
 
Join Date: Jun 2006
Location: , , .
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

  #2 (permalink)  
Old November 8th, 2006, 11:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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

  #3 (permalink)  
Old November 9th, 2006, 12:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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

  #4 (permalink)  
Old November 9th, 2006, 11:48 AM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
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
  #5 (permalink)  
Old November 9th, 2006, 10:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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







  #6 (permalink)  
Old November 10th, 2006, 01:52 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:17 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.