Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
I want Polymorphic enums
Message
De
02/03/2006 13:51:16
 
 
À
02/03/2006 01:07:04
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01100503
Message ID:
01101017
Vues:
15
I want C# to support polymorphic enums to save me time and the need to remember.

If a programmer decided to derive a third class called Rabbit and didn't remember to implement an enum called BreedEnum, there would be no compiler error that said,

'Rabbit' does not implement inherited abstract enum 'BreedEnum'."

If abstract method is defined in the PureBreed class that takes BreedEnum as a parameter, all derived classes can't implement it using their own BreedEnum values. Values from child class BreedEnums can't be assigned to an inherited parent property.

These classes could be used to create a pet expo directory of booths by breed. Iterating through an array containing all the cat, dog, and rabbit instance objects would be convenient if polymorphic enums existed. Assuming there was a concrete get property in the abstract class called public string BoothNumber, a 2×n array of strings could be created using,
public string[][] createExpoIndex(string[] entrants)
{
	int entrantCount = entrants.Length;

	string[][] listing = new string[entrantCount][];

	for(int index = 0; index < entrantCount; index++)
	{
		string breed = entrants[index].getBreed();
		string booth = entrants[index].BoothNumber;
		listing[index] = new string[2]{breed, booth};
	}

	…Alphasort by breed…

	return listing;
}
Furthermore, convenient polymorphism, like defining a concrete, inherited method in the parent class to set the breed for each instance would not be possible.

Because Polymorphic enumerations aren't supported, if you want to implement an abstract or virtual class to include an enum whose members vary by subclass, a programmer who is unfamiliar with the inheritance structure can't simply create a class that inherits from the abstract class and have the IDE and compiler lay out everything that needs to be implemented.

I know that polymorphism can be faked in non-OOP languages, but it is inconvenient to remember to write a bunch of parallel code containing the same logic.

Does this make sense?

>I don't know why this would be necessary really. You'd need to have the appropriate enum in each sub-class anyway, what benefit do you get in having it as an abstract? This will work fine:
>
>public class PureBreed
>{
>	protected int Breed;
>}
>public class Dog : PureBreed
>{
>	public Dog()
>	{
>		this.Breed = (int)BreedEnum.Golden;
>	}
>
>	public enum BreedEnum : int
>	{
>		Collie,
>		Golden,
>		Lab
>	}
>}
>
>public class Cat : PureBreed
>{
>	public Cat()
>	{
>		this.Breed = (int)BreedEnum.Siamese;
>	}
>	public enum BreedEnum : int
>	{
>		Abyssinian,
>		RussianBlue,
>		Siamese
>	}
>}
>
>Or did I totally miss your point?
>
>~~Bonnie
>
>
>
>
>>This is an issue that has annoyed me for a while, but after reading Bill Caton's message #1098525, I was inspired to weigh in.
>>
>>I want the possibility to declare abstract or virtual enums.
>>
>>A simple example would be a class called Purebred with an abstract enum called Breed. I would subclass Cat and Dog classes. For dog, the breed enum would countain AfghanHound, Beagle, Collie, etc. and for cat it would be Abyssinian, Birman, CornishRex, etc. For a specific instance, a property would expose the value of a specific instance.
>>
>>An even more practical example might be an abstract enum called color, where each subclass would implement it to define the subset of all possible colors that it was allowed to be.
>>
>>Referring to Perry Forman's quote,
>>
>>"If you are finding you need to do something like this, the design of your program is clearly all wrong. Enums are there for hard coded groups of items that will never change during runtime. If for example you want people to add groups (e.g. add a username), you should use an alternative datasource (e.g. database, file)."
>>
>>Does this apply to what I would like to do?
>>
>>I could declare an abstract int object which Cat and Dog implemented that tied to some kind of DataTable that I pulled from a DataBase that associated ints with strings, or something equally awkward, but it isn't as clean.
>>
>>If someone can suggest a way to accomplish the same kind of pattern, I would appreciate it.
David S. Alexander
Kettley Publishing
20271 SW Birch Street, 2nd Floor
Newport Beach, CA 92660-1752
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform