Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, there's no sense to <code>override</code> these values. Actually, the advantages of override is that you can call a method of a derived class without knowing the derived class itself.</p> <p>For example :</p> <pre><code>static void Main() { NaturalFood food = GetSomeFood(); // At this point, we don't know the actual type food.SomeMethodInBaseClass(); // ok } static NaturalFood GetSomeFood() { if(somecondition) { return new Fruits(); } else{ return new Vegetables(); } } public abstract class NaturalFood { public abstract void SomeMethodInBaseClass(); } public class Fruits : NaturalFood { public override void SomeMethodInBaseClass(){ Console.WriteLine("I'm a fruit"); } } public class Vegetables : NaturalFood { public override void SomeMethodInBaseClass(){ Console.WriteLine("I'm a vegetable"); } } </code></pre> <p>No imagine what you wanted to do. In the main method, try to call the AllList :</p> <pre><code>static void Main() { NaturalFood food = GetSomeFood(); // At this point, we don't know the actual type food.SomeMethodInBaseClass(); // ok food.AllList.XXXX; // What? it won't compile } </code></pre> <p>This won't compile. The compiler has no way to know the actual derived class to infer the available enumeration values.</p> <p>However, if you remove the enumeration from the base type, this will works :</p> <pre><code>static void Main() { NaturalFood food = GetSomeFood(); // At this point, we don't know the actual type food.SomeMethodInBaseClass(); // ok Fruits f = new Fruits(); Console.WriteLine( f.AllList.Apple); // Ok Vegetable v = new Vegetable (); Console.WriteLine( v.AllList.Potatoe); // Ok } </code></pre> <p>But as you can see, you have to know explicitly the actual type, and thus, make the polymorphic useless.</p> <p><strong>[Edit]</strong> It's hard to answer to your second edit. Actually there are many many ways to validate such constraint. Without more context it may be difficult to answer. The most simple way I think, is to add to each derived class a overriden property that describe what kind of enumeration is accepted.</p> <pre><code>public enum NaturalFoodType { Unknown = 0, Apple= 1, Banana = 2, Potatoe = 3, Cucumber = 4 } public abstract class NaturalFood { public abstract void SomeMethodInBaseClass(); public abstract IEnumerable&lt;NaturalFoodType&gt; AcceptedFoodType { get; } public bool IsValid(NaturalFoodType type){ return AcceptedFootType.Contains(type); } } public class Fruits : NaturalFood { public override void SomeMethodInBaseClass(){ Console.WriteLine("I'm a fruit"); } public override NaturalFoodType { get { yield return NaturalFoodType.Apple; yield return NaturalFoodType.Banana; } } } public class Vegetables : NaturalFood { public override void SomeMethodInBaseClass(){ Console.WriteLine("I'm a vegetable"); } public override NaturalFoodType { get { yield return NaturalFoodType.Potatoe; yield return NaturalFoodType.Cucumber; } } } </code></pre> <p>But honestly, it start to add a lot of plumbing code, that become quite unreadable. You should consider the problem at a higher scope to find an acceptable solution.</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.
    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