Re: interface mapping

From: Chris A. R. (sothryn_at_hotmail.com)
Date: 03/29/04


Date: Mon, 29 Mar 2004 08:37:54 -0500

Remove the interface from DerivedClass:
    class DerivedClass: BaseClass

And then try this code:
    DerivedClass derived = new DerivedClass();
    derived.Foo = derived.Foo;

You'll see that it fails, because it cannot be assigned to, since you
haven't defined a 'set'. That's why, when you use the interface, it
complains.

if you use
    ((BaseClass)derived).Foo = ((BaseClass)derived).Foo;
it'll compile just fine.

So, everything works as defined and as expected.
When you create a "new" member, it really is NEW. derived.Foo and
((BaseClass)derived).Foo do not refer to the same property. You can
manipulate the values totally independent of each other, if you so desire
to.

It's that simple.

Chris A.R.

"pn" <anonymous@discussions.microsoft.com> wrote in message
news:B0253CA8-6959-4476-9E31-2314C79F7D58@microsoft.com...
> I don't think it's as simple as that. Hiding an inherited member doesn't
make it inaccessible:
>
> DerivedClass derived = new DerivedClass();
> int i = ((BaseClass)derived).Foo; // invokes BaseClass.Foo
>
> By the way, the new modifier doesn't have any effect on the semantics of a
member declaration. It's just a way to explicitely state that a newly
introduced member hides an inherited member. If you leave it out, you'll get
a compiler warning, but the program will behave the same.
>
>
>
> ----- Chris A. R. wrote: -----
>
> What's surprising about this? You're using "new" in
> public new int Foo
> which hides the BaseClass Foo. Therefore, your derived class does
not have
> access to your set in Foo.
>
> Chris A.R.
>
> "pn" <anonymous@discussions.microsoft.com> wrote in message
> news:1B42DC5C-8F8E-4D33-92FA-AF592DB39CDE@microsoft.com...
> > Hi all,
> >> I wonder if the Microsoft C# compiler performs interface mapping
according
> to the C# language specification (ECMA-334, 2nd edition December
2002).
> > Take a look at the following example:
> >> interface I
> > {
> > int Foo { get; set; }
> > }
> >> class BaseClass
> > {
> > public int Foo
> > {
> > get { return 0; }
> > set {}
> > }
> > }
> >> class DerivedClass: BaseClass, I
> > {
> > public new int Foo
> > {
> > get { return 0; }
> > }
> > }
> >> Now let us locate the implementation for I.Foo. According to the
language
> specification (section 20.4.2 Interface mapping), DerivedClass.Foo
doesn't
> match I.Foo because DerivedClass.Foo doesn't have a set accessor. So
the
> search for a matching implementation continues in BaseClass. Because
> BaseClass.Foo matches I.Foo, we are done.
> >> But to my surprise, running the compiler gives the following
error:
> > 'DerivedClass' does not implement interface member 'I.Foo.set'
> > (Note: if DerivedClass.Foo is removed, the program compiles without
> errors.)
> >> I've found other similar issues, but first I'd like to get your
input on
> this one.
> > Any comments would be greatly appreciated!
> >> Regards,
> > pn
>
>
>



Relevant Pages

  • Re: Protected Member
    ... | Then I had a class called CustomerList that was a List, ... Private visibility means that a member of a class can only ever be accessed ... class DerivedClass: BaseClass ...
    (microsoft.public.dotnet.general)
  • Re: need interface methods to be virtual-able
    ... class DerivedClass: BaseClass ... public override void IFoo.Print ... What I see is that the "CompareTo" coming from IComparable should be ...
    (microsoft.public.dotnet.general)
  • Re: interface mapping
    ... the point was about member hiding, ... Your example still shows that DerivedClass can't ... "Member hiding is not the reason because the base class should fulfill ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: need interface methods to be virtual-able
    ... "Greg Young" wrote: ... class DerivedClass: BaseClass ... public override void IFoo.Print ...
    (microsoft.public.dotnet.general)
  • Re: need interface methods to be virtual-able
    ... void IFoo.Print ... class DerivedClass: BaseClass ... DerivedClass d = new DerivedClass; ... What I see is that the "CompareTo" coming from IComparable should be ...
    (microsoft.public.dotnet.general)