Re: No Equals on interfaces

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Michi Henning (michi_at_zeroc.com)
Date: 09/06/04


Date: Mon, 06 Sep 2004 11:02:37 +1000
To: "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at>

Herfried K. Wagner [MVP] wrote:

> * Michi Henning <michi@zeroc.com> scripsit:
>
>>the following code won't compile, but I believe it should:
>>
>>Interface X
>>End Interface
>>
>>Module M
>> Sub Main(ByVal args As String())
>> Dim iref As X
>> iref.Equals(iref) ' 'Equals' is not a member of 'vb.I'.
>> End Sub
>>End Module
>>
>>The compiler complains that X does not have a member Equals.
>>(The same complaint is issued for the other methods of System.Object:
>>GetHashCode, GetType, and ToString.)
>>
>>This seems wrong, given that C# compiles the equivalent code
>>without complaint.
>
>
> That's simply different behavior in C# and VB.NET, and I think that the
> behavior shown by VB.NET is more correct. Interfaces in .NET do /not/
> have a base type if they don't inherit from another interface.

Nonsense. From the C# language specification for predefined types:

object: The ultimate base type of all other types

> In C#, the compiler doesn't complain for some reason, but the code you
> listed above is rather useless because it will throw a
> 'NullReferenceException'.

Oh, *please*. The point is that the expression iref.Equals(iref) will not
compile, not what happens at run time.

> None of the methods you can access from C#
> are useful because they are all instance members and interfaces cannot
> be instantiated.

Again, nonsense. I can pass a reference to a class that is derived from I
to another method. Are you *really* suggesting that this method then should
not be allowed to invoke Equals on the reference?

Moreover, you ignored the following:

>>Also, the following works fine:
>>
>>Dim iref As X
>>Dim oref As Object = iref
>>oref.Equals(oref)
>>
>>Clearly, if there is an implicit conversion from X to Object,
>>then X must have the members of Object (otherwise, implicit
>>conversion would have to be illegal.)

According to what you say, the implicit conversion to object (which
is allowed by the compiler) would then lead to me holding a reference
that allows me to call Equals on it, yet does not support the Equals
method. Again, that's simply nonsense. *All* reference types are derived
from object. Whether the formal type of a references is for an interface
or to a class is irrelevant. The VB compiler is simply broken in this
respect, period.

>>Does anyone know whether this problem is fixed for Whidbey?
>
>
> There is no problem in VB.NET. It's IMO a C# problem.

Please read the language specification. This isn't a C# problem,
it's a bug in VB.

Cheers,

Michi.



Relevant Pages

  • Re: 7.0 wishlist?
    ... The "auto-implement" is intended mainly for the odd situation where an existing class you can't edit fits some interface and you're willing to take responsibility for it if it turns out not to actually adhere to the contract, and try using it where that interface type is expected. ... If reference declarations started showing up with the odd asterisk, bang, or other punctuation mark on it, but never primitive declarations, people would probably be able to guess what was going on, on the basis of "what other binary flag might be set on references but not primitives and would be really useful besides can be/cannot be null?" ... the compiler cannot prove by static analysis that the RHS isn't null might be a good idea. ... Object foo, bar; ...
    (comp.lang.java.programmer)
  • Re: Using early-bound interface on a late-bound object
    ... > the compiler determines which interfaces to use, ... > supports the declared interface. ... >> help if someone can point me to some authoritative document or reference ... within customer shops when they mirrored this 'division' to keep their ...
    (microsoft.public.vb.general.discussion)
  • Re: Using early-bound interface on a late-bound object
    ... If the assignment of an object to such a variable is successful, ... the compiler knows all it needs to know to call ... supports the declared interface. ... > help if someone can point me to some authoritative document or reference ...
    (microsoft.public.vb.general.discussion)
  • Re: Passing a form reference gives error
    ... Define an Interface with two methods: ... then the form reference implements the ... member called lstFormControls. ... compiler and shouldn't matter at runtime. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: No Equals on interfaces
    ... >> have a base type if they don't inherit from another interface. ... I can pass a reference to a class that is derived from I ... > that allows me to call Equals on it, yet does not support the Equals ...
    (microsoft.public.dotnet.languages.vb)