Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why not have a <a href="http://msdn.microsoft.com/en-us/library/sf985hc5%28v=vs.71%29.aspx" rel="nofollow">base class</a> that has <code>Output</code> defined. Then return the base.</p> <pre><code>public abstract class BaseType { public abstract void Output(); } </code></pre> <p>Both <code>Articles</code> and <code>Questionaire</code> should inherit this <code>BaseType</code>.</p> <pre><code>public class Articles : BaseType { // Output method here } public class Questionaire : BaseType { // Output method here } </code></pre> <p>Then you can do:</p> <pre><code>public static BaseType Choose(int x, string name) { if (x == 1) { Articles art = new Articles(name); return art; } if (x == 2) { Questionnaire ques = new Questionnaire(name); return ques; } } </code></pre> <p>You could also achieve this via an <a href="http://msdn.microsoft.com/en-us/library/87d83y5b%28v=vs.110%29.aspx" rel="nofollow"><code>interface</code></a>.</p> <pre><code>public interface IInterface { void Output(); } public class Articles : IInterface { // Output method here } public class Questionaire : IInterface { // Output method here } </code></pre> <p>You would then have to modify the Choose method to return <code>IInterface</code> rather than <code>BaseType</code>. Whichever you choose is up to you. </p> <p>Note: even if you can't change original classes you can still use these approaches before resorting to <code>dynamic</code> by providing wrapper classes that implement the interface and either inherits original or forwards calls to corresponding method:</p> <pre><code>public class ArticlesProxy : Articles, IInterface { public ArticlesProxy(string name) : base(name){} } public class QuestionaireProxy : Questionaire, IInterface { Questionaire inner; public QuestionaireProxy(string name) { inner = new Questionaire(name); } public void Output() { inner.Output();} } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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