4 ms·
(EDIT: Funny, I made the same mistake as kyrra, thinking it was Java. At least my last line still applies...) The true solution, that doesn't involve using a n
by gordaco 12y ago
(EDIT: Funny, I made the same mistake as kyrra, thinking it was Java. At least my last line still applies...)
The true solution, that doesn't involve using a new language, is to include the data for the switch inside each of each element of the enum, instead of treating it like C enums. That forces you to define all you need for "German" and give it to a constructor; otherwise the compiler complains about it. Come on, it's one of the very few things from Java that I miss when I do C++.
By the way, in Spanish it's "cero" not "zero" :).
- JackMorgan 12y agoSure, but something still has to link the enum to the concrete class, then conditionally instantiate the concrete clas, right? Do you have an example of what you are talking about? As to cero, yep, whoops!
- gordaco 12y agoOk, let's see... this is a draft and I don't have a compiler here, so I may write something wrong, but it would be something like this. Assuming that you don't want to use simply a translation table, and you really need different code for each element from the enum, you would first create an interface: public interface ILanguage { String convert(int num); } Then you would subclass for each language: public class SpanishLanguage implements ILanguage { public String convert(int num) { String ret; switch (num) { case 0: ret="cero";break; case 1: ret="uno";break; default: ret="~~~"; } return ret; } } And the same for English, German or whatever you want. Yes, it gets too verbose, that's why I'd prefer just a translation table... in any case, you can abstract common code for languages that only implement translation for 1, 2 and others, and create more complex subclasses for different behaviours. But I digress... Now that you have the interface, you would declare the enum as follows: public enum Language { SPANISH(new SpanishLanguage()), ENGLISH(new EnglishLanguage()), GERMAN(new GermanLanguage()); // This is a member instance. private ILanguage translationImpl; // And this is the one and only constructor. public Language(ILanguage translationImpl) { this.translationImpl=translationImpl; } public String convert(int number) { return translationImpl.convert(number); } } If you do this, you need to supply the implementation of the language to the constructor, otherwise your code fails to compile. So you can't add FRENCH or JAPANESE after GERMAN(new GermanLanguage()); you need FRENCH(new FrenchLanguage()), JAPANESE(new JapaneseLanguage()) and so on. Of course you can do JAPANESE(null), but it's immediately clear that you're doing it wrong. I'm completely aware that there is a lot of Java-ness (in the bad sense) in this code, so maybe in C# you can do it more elegantly. I haven't touched C# for a good 10 years... EDIT: formatting.
- agency 12y agoAt least in Java, Enums can implement interfaces. The following is valid: public interface ILanguage { String convert(int number); } public enum Language implements ILanguage { English { String convert(int number) { switch(number) { case 0: return "zero"; case 1: return "one"; default: return "..."; } } }, Spanish { String convert(int number) { switch(number) { case 0: return "zero"; case 1: return "uno"; default: return "~~~"; } } } }