Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to put common interface methods when dealing with partial classes, inheritance, and Visual Studio Generated Code
    primarykey
    data
    text
    <p>Consider this situation:</p> <p>We have two classes generated by Visual Studio, for example Typed Dataset Rows. These classes derive from a common base class which we cannot change. We cannot change the class these child classes derive from, but they are generated as partial classes so we can extend them.</p> <p>Now we decide to implement an interface for these two classes which define some common methods, but the methods are going to be implemented exactly the same way in both classes. Where is the best place to put these methods so that we are not writing the same code twice.</p> <p>I could the the code in some helper class or global class, but it seems like there should be a better way.</p> <p>Here is a quick code example:</p> <pre><code>public interface ICommonInterface { void SomeMethod(int x); } // we cannot change what is in the base class and we cannot derive Child1 or Child2 from // a different base class, because they are partial classes generated by Visual Studio // we can extend them, and create an interface though public partial class Child1: SomeBaseClass, ICommonInterface { public void SomeMethod(int x) { //this code is the same in both child classes //where is the best place to put this to avoid writing //the same code twice } } public partial class Child2: SomeBaseClass, ICommonInterface { public void SomeMethod( int x) { //this code is the same in both child classes //where is the best place to put this to avoid writing //the same code twice } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload