Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Explicit Interface and overriding

4: Adsense

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   

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.


#2 Paradigm Shifter   Members   

5832
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   

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.