akiaz Posted May 4, 2004 Posted May 4, 2004 I have a question regarding implementing multiple interfaces in vb.net versus c#. This code works good in c#: interface IDatabaseUser { void LookupPassword(); } interface ITestUser { void LookupPassword(); } public class testcode : IDatabaseUser, ITestUser { public void LookupPassword() { // } void ITestUser.LookupPassword() { // } } but the "equivalent" code (so I think) in vb.net: Public Interface IDatabaseUser Sub LookupPassword() End Interface Public Interface ITestUser Sub LookupPassword() End Interface Public Class User Implements IDatabaseUser, ITestUser Public Sub LookupPassword() ' End Sub Public Sub LookupPassword() Implements ITestUser.LookupPassword ' End Sub gives an error stating that I must implement 'Sub LookupPassword()', so when I change the method declaration to Public Sub LookupPassword() Implements IDatabaseUser.LookupPassword I still get the same error, but now I also get an error: 'LookupPassword' cannot implement 'LookupPassword' because there is no matching sub on interface 'IDatabaseUser' Is there a fundamental difference in implementing multiple interfaces between c# and vb.net or am I doing something wrong here that is simple to correct? Quote
akiaz Posted May 4, 2004 Author Posted May 4, 2004 Also, in the vb.net code, I get the error: Method 'LookupPassword' has multiple definitions with identical signatures. Which I do not get in c#. I thought they have to be the same when specifying the interface that is implemented for the method otherwise this makes no sense. Quote
Administrators PlausiblyDamp Posted May 4, 2004 Administrators Posted May 4, 2004 Public Interface IDatabaseUser Sub LookupPassword() End Interface Public Interface ITestUser Sub LookupPassword() End Interface Public Class User Implements IDatabaseUser, ITestUser Public Sub LookupPassword2() Implements IDatabaseUser.LookupPassword ' End Sub Public Sub LookupPassword() Implements ITestUser.LookupPassword ' End Sub End Class Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
akiaz Posted May 4, 2004 Author Posted May 4, 2004 I see. So when I create a User object and then make a reference to it like this: Dim guy As New User Dim dbguy As IDatabaseUser = guy I use b.LookupPassword and not b.LookupPassword2. Actually, now I'm curious as to why vb.net is this way and c# is not. Is there some other limitation or advantage in doing interfaces like this between languages? Quote
akiaz Posted May 4, 2004 Author Posted May 4, 2004 oo limitation? VB is a dog! lol :p , well it's actually kinda annoying more than anything but I can sort of get around it by naming class members like this instead: Public Sub LookupPassword_DatabaseUser Implements IDatabaseUser.LookupPassword ' and Public Sub LookupPassword_TestUser Implements ITestUser.LookupPassword but that makes the lines long which is why I prefer the c# method. Doesn't really matter though to me as long as they both work the same. Where I work, our coders use vb.net and/or c# so that's why it's important they work the same. An alternate thing that I found that I do not like is that when an interface inherits from another interface, the implementing class can only have one member that implements the base interface member (sounds like a tongue twister :cool: ). So I can't do something like this: Interface IStandardUser Sub LookupPassword() End Interface Interface IDatabaseUser Inherits IStandardUser End Interface Interface ITestUser Inherits IStandardUser End Interface Public Class User Implements IDatabaseUser, ITestUser Public Sub LookupPassword_DatabaseUser() Implements IDatabaseUser.LookupPassword ' End Sub Public Sub LookupPassword_TestUser() Implements ITestUser.LookupPassword ' End Sub End Class Because I get the error: 'IStandardUser.LookupPassword' cannot be implemented more than once. So I cannot have a base interface, and the same is true for c#. I wonder if this is a .NET limitation (or design standard) or if this is pretty much standard across other object oriented languages. Or maybe my approach is alltogether wrong ;) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.