Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding method from library

There is a virtual method in a library that my C# project references. How can I override this method in another class within my application?

Example:

namespace TheLibary
{
   class SomeClass
   {
      public virtual void TheMethod()
      {
           //Do Stuff
      }
   }
}

Then in my project:

using theLibary;
namespace TheProject
{
   class SomeClass
   {
       public override <Help>
   }
}

Edit: Confusion and totally forgetting that this class didn't inherit from the libraries class messed me up, it was late :)

like image 758
Cyral Avatar asked Nov 24 '25 03:11

Cyral


1 Answers

You should study a bit about OOP (and inheritance in particular) before getting down to any serious coding. For quick reference, here’s an example of how to override a method:

namespace TheDll
{
    public class SomeClass
    {
        public virtual void TheMethod()
        { }
    }
}

namespace TheProject
{
    public class DerivedClass : SomeClass
    {
        public override void TheMethod()
        { }
    }
}

You should observe that the signature of the overriding method (including its name) must be the same. The derived class, on the other hand, may (and typically should, for clarity) be named differently.

like image 183
Douglas Avatar answered Nov 26 '25 17:11

Douglas