Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Explicit Interface and overriding


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Memories are Better   Prime Members   -  Reputation: 769

Like
0Likes
Like

Posted 31 May 2013 - 08:05 AM

Code Example:

 

    public interface ITest
    {
        void SomeMethod();
    }

    public abstract class TestBase : ITest
    {
        void ITest.SomeMethod()
        { 
            // Stuff
        }
    }

    /// <summary>
    /// Compiler Error: -
    ///     Error	1	'AnotherTest.ITest.SomeMethod()': containing type does not implement interface 'ITest'
    /// 
    /// Need to implement ITest to avoid this error
    /// </summary>
    public class AnotherTest : TestBase, ITest
    {
        /// <summary>
        /// Did I just override this?
        /// What if I wanted it sealed
        /// </summary>
        void ITest.SomeMethod()
        {
            // Stuff
        }
    }

 

When you explicitly implement an interface, you cant use the modifier abstract or virtual on its members you want to be called explicitly (in this cast ITest.SomeMethod()), so my question is, in the class AnotherTest did I override ITest.SomeMethod()? and why do I get a compiler error if I don't inherit ITest in AnotherTest despite it already being inherited in the base class?

 

I am not familiar with CIL or how this is treated internally, but would this be treated as a virtual method being overridden or a completely new method?


Edited by Dynamo_Maestro, 31 May 2013 - 08:09 AM.


Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5432

Like
0Likes
Like

Posted 31 May 2013 - 08:09 AM

TestBase already inherits ITest, so you don't want to inherit ITest in AnotherTest surely?


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 Memories are Better   Prime Members   -  Reputation: 769

Like
0Likes
Like

Posted 31 May 2013 - 08:19 AM

TestBase already inherits ITest, so you don't want to inherit ITest in AnotherTest surely?

 

Yeh that's what I thought, but it throws an error. Ofc if I don't re-implement ITest.SomeMethod() in AnotherTest, I don't have to inherit it and I get no error, I only get a compiler error (as shown in summary) IF I re-implement it.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS