Note that there are some explanatory texts on larger screens.

plurals
  1. POOverride a Property with a Derived Type and Same Name C#
    primarykey
    data
    text
    <p>I'm trying to override a property in a base class with a different, but derived type with the same name. I think its possible by covarience or generics but am not sure how to do it?</p> <p>The following code gets the error:</p> <blockquote> <p>Error 1 'Sun.Cache': type must be 'OuterSpace.Cache' to match overridden member 'OuterSpace.Cache'</p> </blockquote> <pre><code>public class OuterSpace { public virtual OuterSpaceData Data {get; set;} public virtual OuterSpaceAnalysis Analysis {get; set;} public virtual OuterSpaceCache Cache {get; set;} public class OuterSpaceData { //Lots of basic Data Extraction routines eg public virtual GetData(); } public class OuterSpaceAnalysis { //Lots of Generic Analysis on Data routines eg public virtual GetMean(); } public class OuterSpaceCache { //Lots of Caches of Past Analysis Results: public Dictionary&lt;AnalysisType, List&lt;Result&gt;&gt; ResultCache; } } public class Sun : OuterSpace { public override SunData Data {get; set;} public override SunAnalysis Analysis {get; set;} public override SunCache Cache {get; set;} public SunData : OuterSpaceData { //Routines to specific get data from the sun eg public override GetData(); } public SunAnalysis : OuterSpaceAnalysis { //Routines specific to analyse the sun: eg public double ReadTemperature(); } public SunCache : OuterSpaceCache { //Any data cache's specific to Sun's Analysis public Dictionary&lt;AnalysisType, List&lt;Result&gt;&gt; TempCache; } } public class Moon : OuterSpace {} etc. </code></pre> <p>For the end result, when I address the "Data" Object of Sun I don't want there to be two Data Objects (Inherited &amp; Base Class) but when I try override the property it requires the Sun variables to be the same type as base class. Eg:</p> <pre><code>Sun EarthSun = new Sun() EarthSun.Analyse() //The OuterSpace Analysis Saves results to Sun Cache: //Now try use the result: EarthSun.Cache[0]... </code></pre> <p>Very similar to this but with derived type instead of string-array: <a href="https://stackoverflow.com/questions/1112458/c-member-variable-overrides-used-by-base-class-method">C# Member variable overrides used by base class method</a></p> <p>And this answer didn't make much sense to me: <a href="https://stackoverflow.com/questions/1123975/how-to-override-member-of-base-class-after-inheritance-in-c">How to override member of base class after inheritance in C++</a></p> <p>Or perhaps this means its just not possible? <a href="https://stackoverflow.com/questions/157119/c-can-i-override-with-derived-types">Can I Override with derived types?</a></p> <p>Help! :) Any work around?</p>
    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.
 

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