Inheritance and polymorphism in VS 2005

axnst2
axnst2 used Ask the Experts™
on
Hi Experts,

     I have a kind of complicated question.  I wrote a couple of plugins which are stored in a single C# DLL. I access these two plugins through an interface (another C# DLL)  The two plugins are two seperate classes inside the same DLL.  The second plugin inherits from the first one, so all of the first plugin's functions are automatically inherited by the second.  What I am trying to do now, is override some of the first plugin's functions in the second.  However, I get a compile errors.  I've never had to deal with plugins or interfaces before, so maybe it's just not bossible to override a function of an inherited base class in this case.  Here's a little bit of the code I am trying to use:

// First plugin class
class PluginOne : IPluginOne
{
    virtual double IPluginOne.Add(double x, double y)
    {
         //Add two doubles together
    }
}

// Second plugin class
class PluginTwo : PluginOne, IPluginTwo
{
    double IPluginTwo.Subtract(intx, int y)
    {
    }
    override int IPluginOne.Add(int x, int y)
    {
         //Add two integers together
    }
}

I get the following two errors:
"The modifier 'virtual' is not valid for this item"
"The modifier 'override' is not valid for this item"

What am I doing wrong?  Any help would be great...

Thanks,
axnst2
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
When you define a virtual method, you don't define a body (braces {}), since the deriving class is responsible for providing the concrete method.

Bob
axnst2IT Manager

Author

Commented:
OK...what if I don't want to make it virtual, but a still want to override it in inherited classes?

Commented:
First, think about that interface is only rules how at least you MUST implement inherited class.
So if you have :
interface IPluginOne
{
    double Add (double x, double y);
}

then you must implement method add
class PluginOne : IPluginOne
{
    public double Add(double x, double y)
    {
         //Add two doubles together
        return 0;
    }
}

Whe you did all what compiler expected from you it is possible to change declaration slightly

interface IPluginOneVirtual
{
    double Add (double x, double y);
}

interface IPluginOne
{
    double Add (double x, double y);
}

interface IPluginTwo
{
    double Subtract (int x, int y);
}

class PluginOne : IPluginOne
{
    public virtual double Add(double x, double y)
    {
         //Add two doubles together
        return 0;
    }
}

// Second plugin class
class PluginTwo : PluginOne, IPluginTwo
{
    public double Subtract(int x, int y)
    {
        return 0;
    }

    public override double Add (double x, double y)
    {
        //Add two doubles together
        return 0;
    }

    protected int Add(int x, int y)
    {
         //Add two integers together
        return 0;
    }
}
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Commented:
You musn't use "IPluginOne."/"IPluginTwo." before function name und you need public access to interface member.
Most Valuable Expert 2012
Top Expert 2008

Commented:
Then you replace the virtual keyword with abstract, and then the deriving class will implement the concrete method.

Bob
axnst2IT Manager

Author

Commented:
I am inheriting the interfaces without implementing its functions and I am running great.  I also am using the IPluginOne names before the function names, and all the functions that I am trying to override are public...

It puzzles me...

Commented:
I'm sorry, I was not quite right.
You could use interface name before function name in implementaion class.
With some restriction
- the explicit implementation method cannot have an access modifier /it is always public/
- a function can't be declared with the abstract, virtual, override, or new keywords
- you cannot access the explicitly implemented method through the object itself.

Commented:
>I am inheriting the interfaces without implementing its functions
Could you give us a short sample, please.

if you only change interface i.e. add new test function
interface IPluginOne
{
    double Add (double x, double y);
    double AddTest (double x, double y);
}

Compiler give you errror like :
'PluginOne' does not implement interface member 'IPluginOne.AddTest(double, double)'      
axnst2IT Manager

Author

Commented:
Interfaces:

    public interface IDBPluginV010000
    {
        string Name { get;}
        string VersionString { get;}
        string Author { get;}
    };

    public interface IDBPluginV010001 : IDBPluginV010000
    {
        double Add(double a, double b);
        double Subtract(double a, double b);
        double Multiply(double a, double b);
        double Divide(double a, double b);
    };

Their Implementation:

namespace DBPlugin
{
    public class DBPluginV010000 : IDBPluginV010000
    {
        private string m_Name="Name";
        private string m_Version = "1.0.0";
        private string m_Author = "GreatOne";

        string IDBPluginV010000.Name
        {
            get
            {
                return m_Name;
            }
        }

        string IDBPluginV010000.VersionString
        {
            get
            {
                return m_Version;
            }
        }

        string IDBPluginV010000.Author
        {
            get
            {
                return m_Author;
            }
        }
    }

   class DBPluginV010001 : DBPluginV010000, IDBPluginV010001
    {// This class inherits from the <DBPluginV010000> class and implements the
     // <IDBPluginV010001> interface.  However, it does not implement any of DBPluginV010000' functions

          double IDBPluginV010001.Add(double a, double b)
        {
            return a + b;
         }

        double IDBPluginV010001.Subtract(double a, double b)
        {
            return a - b;
        }

        double IDBPluginV010001.Multiply(double a, double b)
        {
            return a * b;
        }

        double IDBPluginV010001.Divide(double a, double b)
        {
            return a / b;
        }
    }
}

This runs and works fine.  As soon as I try to overwrite the VersionString method in the second plugin is where I run into problems.

Commented:
>However, it does not implement any of DBPluginV010000' functions
You already did it. Try to change
from
class DBPluginV010001 : DBPluginV010000, IDBPluginV010001
to
class DBPluginV010001 : IDBPluginV010001
and you see the difference

The second try
add this class
    public interface IDBPluginV010002
    {
        string Name { get;}
        string VersionString { get;}
        string Author { get;}
    };

and change this line
public interface IDBPluginV010001 : IDBPluginV010002
axnst2IT Manager

Author

Commented:
OK Alex..let me try that..
axnst2IT Manager

Author

Commented:
Thanks Alex, with minor modifications that worked great!

For the next poor soul that has the same question, here's the entire thing:

In the interface DLL:

    public interface IDBPluginV010000
    {
        string Name { get;}
        string VersionString { get;}
        string Author { get;}
        double Subtract(double a, double b)
    };

    public interface IDBPluginV010001 : IDBPluginV010000
    {
        new string Name { get;}
        new string VersionString { get;}
        new string Author { get;}
        double IDBPluginV010001.Add(double a, double b);
    };

In the Plugins:

Plugin 1:

public class DBPluginV010000 : IDBPluginV010000
    {
        private string m_Name="Name";
        private string m_Version = "1.0.0";
        private string m_Author = "Akos \"Kosher\" Nagy";

       string IDBPluginV010000.Name
        {
            get
            {
                return m_Name;
            }
        }

        string IDBPluginV010000.VersionString
        {
            get
            {
                return m_Version;
            }
        }

        string IDBPluginV010000.Author
        {
            get
            {
                return m_Author;
            }
        }

       double IDBPluginV010000.Subtract(double a, double b)
        {
            return a - b;
            //throw new Exception("The method or operation is not implemented.");
        }
}

Plugin 2:

public class DBPluginV010001 : DBPluginV010000, IDBPluginV010001
    {
        private string m_Name="Name";
        private string m_Version = "1.0.1";
        private string m_Author = "Author";

       string IDBPluginV010001.Name
        {
            get
            {
                return m_Name;
            }
        }

        string IDBPluginV010001.VersionString
        {
            get
            {
                return m_Version;
            }
        }

        string IDBPluginV010001.Author
        {
            get
            {
                return m_Author;
            }
        }

        double IDBPluginV010001.Add(double a, double b)
        {
            return a + b;
        }
}

I DID NOT have to implement all of Plugin 1's functions, yet I am inheriting all of them and overriding some of them.  Just what I wanted...

Thanks,
axnst2
axnst2IT Manager

Author

Commented:
Correction, this:

public interface IDBPluginV010001 : IDBPluginV010000
    {
        new string Name { get;}
        new string VersionString { get;}
        new string Author { get;}
        double IDBPluginV010001.Add(double a, double b);
    };

Should be:

public interface IDBPluginV010001 : IDBPluginV010000
    {
        new string Name { get;}
        new string VersionString { get;}
        new string Author { get;}
        double Add(double a, double b);
    };

The code really does work....

Commented:
I'm happy that you get what you want but I want to tell you that we thought in different way.
What you marked for solution, was only my try to show you that you already implement all functions, at least from compiler sight.
Thank you too for me it was the most interesting question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial