Click here to Skip to main content
14,865,088 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
Hello everybody.

I have this piece of code to explain my problem:

C#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    public interface Interface1
    {
        void Test();
    }
    public class BaseClass : Interface1
    {
        public void Test()
        {
            throw new NotImplementedException(); //Execution jumps here always. I need it to execute this line only if Class1 doesn't have a Test function.
        }
    }
    public class Class1 : BaseClass
    {
        public void Test()
        {
            return; //Why not here?
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class1 a = new Class1();
            Object obj = a as Object;
            Interface1 itf = a as Interface1;
            itf.Test(); // <-- Why it calls BaseClass.Test() ?
        }
    }
}


When the program execution reaches the itf.Test() in the Main function I expect it would call the Class1.Test() function instead of the BaseClass.Test(). I need to execute the BaseClass.Test() method only if Class1.Test() function is not implemented. In that case the program would fire a NotImplementedException.
Anyone could help me please? Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Feb-11 15:37pm
   
Oh gosh! This is because you're not slow enough :-) Read the manual first!
--SA

Change your code as follows:

C#
public class BaseClass : Interface1
{
    public virtual void Test()
    {
        // some code here
    }
}

public class Class1 : BaseClass
{
    public override void Test()
    {
        // some other code here
    }
}
   
v3
Comments
#realJSOP 17-Feb-11 13:17pm
   
Proposed as answer
Sergey Alexandrovich Kryukov 17-Feb-11 15:42pm
   
My "4", this time, so far. You answer would be nearly perfect if you suggested using "abstract" instead of NonImplementedException and explanation why it's better.
How about adding this small fix?
--SA
Nish Nishant 17-Feb-11 16:29pm
   
I was just using the OP's example - he was just showing placeholder code. I don't think he wants to make the base implementation abstract.

Anyway I've updated my answer to make that more obvious :-)

Thanks SA.
Sergey Alexandrovich Kryukov 17-Feb-11 19:53pm
   
Whatever, but now it's better. I vote 5 now :-)
--SA
Nish Nishant 17-Feb-11 20:07pm
   
:-)
Espen Harlinn 17-Feb-11 18:25pm
   
Good answer, straight to the point, my 5
Nish Nishant 17-Feb-11 19:22pm
   
Thank you.
Previous answer is correct. You use virtual to indicate that the method can be overriden. And you use override when you are re-writing that method in a child class.

non virtual method:
* it can't be overriden
* it's faster than virtual method (early binding)

virtual method:
* it can be overriden
* it's slower than non virtual method because the binding is at runtime (late binding).
   
v3
Comments
Sergey Alexandrovich Kryukov 17-Feb-11 15:39pm
   
Well, not much slower. You could also explain hiding a method in a derived class, modifier "new" and the functional difference. My 4.
--SA
Espen Harlinn 17-Feb-11 18:30pm
   
I'm abstaining from voting, but you should probably note that virtual/non-virtual and late-binding/early-binding are usually not understood as dealing with the same issue.
fgoldenstein 17-Feb-11 18:32pm
   
They're not the same because if you have a concrete class with a virtual method and you call that method there's no late binding. Late binding occurs when you have inheritance and you are calling an abstract or a virtual method with overriden method.
Do you agree?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900