Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The question you posted is somewhat vague, but it sounds like you need to define an interface and then have each of the differen classes implement that interface. In your example, where you're referencing a "calculate" method, you would just define an interface like this: </p> <pre><code>public interface ICalculator { int NumberOne { get; set; } int NumberTwo { get; set; } int Calculate ( ); } </code></pre> <p>Then when you create your different classes, they would implement the interface like this:</p> <pre><code>public class TwoPlusTwoCalculator : ICalculator { private int _numberOne; private int _numberTwo; public int NumberOne { get { return this._numberOne; } set { this._numberOne = value; } } public int NumberTwo { get { return this._numberTwo; } set { this._numberTwo = value; } } public TwoPlusTwoCalculator ( ) { } public TwoPlusTwoCalculator ( int numOne, int numTwo ) { this.NumberOne = numOne; this.NumberTwo = numTwo; } public int Calculate ( ) { return this.NumberOne + this.NumberTwo; } } </code></pre> <p>Then in your code when you want to invoke the Calculate method you can cast an instance of the TwoPlusTwoCalculator, or OnePlusOneCalculator, or any other class that implements the interface as that interface:</p> <p>public void GetValue ( int numOne, int numTwo ) { TwoPlusTwoCalculator calculator = new TwoPlusTwoCalculator ( numOne, numTwo ); PerformCalculation ( calculator as ICalculator ); }</p> <pre><code> public int PerformCalculation ( ICalculator calculator ) { return calculator.Calculate ( ); } </code></pre> <p>Is that what you're trying to accomplish?</p>
 

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