Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
I want Polymorphic enums
Message
De
08/03/2006 13:58:10
 
 
À
03/03/2006 19:12:58
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01100503
Message ID:
01102562
Vues:
15
You could inplement the abstract property as an int and then cast it to the enum in the derived class, but that is a bit sloppy and causes a lot of additional work that you need to do for error checking.

When you are casting an array of base class ints to the subclass' overridden enum, you have no way of checking if the int value is valid or not.

If you have an enum like,
	public enum BreedEnum : int
	{
		Collie = 0,
		Golden = 1,
		Lab = 2
	}
and do something like,
BreedEnum bogus = (BreedEnum)12;
This causes problems if you want to one subclass' overridden enum to have 10 members and another to have 8. You don't get a compiler or run-time error. For me, typically, I would end up throwing a custom error in the default: of a switch statement if I didn't check Enum.IsDefined for the value at assignment time.

You can get the number of values in an enum, their string and int equivalents by fussing with Enum.GetNames, but I wish that this functionality was built-in to enum so I didn't have to implement it. Maybe I will have to play with subclassing Enum and see what the compiler will accept.

I could implement a custom class using an ArrayList of strings that I could use it like an enum in my switch statements. The index would map to the int equivalent, I could tell how many members there were, and if specific values were included. I wouldn't get the Intellisense autocompletion when I typed BreedEnum. and I like that a lot. If I wanted to work really hard, I could implement all the members as properties, having a complete class for every enum for the sake of Intellisense and even implement it in dynamically generated classes, but that seems a bit ridiculous and a poor use of coding resources. C# could be extended to do what I want, but if there isn't widespread interest or my example doesn't seem compelling to other programmers, I am out of luck.

I have a background in math and I guess I like thinking in terms of sets. Thanks for being my sounding board. I will pursue my grieving process in my own way.

>Enums are typically int. Any abstract method that needs to take BreedEnum as a parameter would just have an int as the parameter. The child class would be able to implement this. I guess I'm still not following you ... and I don't get your example.
>
>>Furthermore, convenient polymorphism, like defining a concrete, inherited method in the parent class to set the breed for each instance would not be possible.<
>
>OK, I follow you on the above statement, although I'm trying to come up with a reason that this kind of method would be implemented in the parent class and not need to be overridden in the child classes.
>
>I'm not trying to be argumentative (although, unfortunately, I'm told that I like to argue for sport <g>), I'm just trying to understand your examples and looking for alternatives. Because of the nature of Enums, I doubt if you're ever going to see C# support polymorphic enums because, by definition, they're static.
>
>~~Bonnie
>
>
>
>
>>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
Répondre
Fil
Voir

Click here to load this message in the networking platform