I was wondering what the best approach would be to the following problem. Say you have a class that can convert strings into different languages.
string MyString = "Hello".Translate(Languages.European.Italian);
From above you 开发者_运维知识库see that the language I want to translate to is Italian which is nested within the European type. Here's an example of something that "works".
public struct Languages
{
public struct Asian
{
public enum Chinese{ Cantonese, Mandarin };
}
public enum European { Italian, German };
}
What is the best way to store or organize these options?
EDIT: The intent of the example was poorly described. The acutal language translation would have just a simple value, like an int. My overall objective could be described as adding hierarchy to an enum. Hopefully the following example will help describe the problem better... Currently at my work we deal with the following structure to write notes pertaining to a particular person to a DB.
public struct NoteTypes
{
public struct Client
{
public static ID = 1;
}
public struct CustomerService
{
public static ID = 2;
}
}
WriteToDB(new Note("Hello World", NoteTypes.CustomerService.ID));
I was hoping there was a better way to do this, because this way just looks odd.
I've come across this article explaining how to create something similar to an enum. http://www.codeproject.com/KB/cs/EnhancedEnums.aspx
Sorry for the confusion!
I don't think you'll ever want to instantiate a Language struct. Take StringComparer and Encoding as an examples and follow their precedent.
Asian could be another type with its own objects that extend Language
public abstract class Language
{
public static readonly AsianLanguage Asian = AsianLanguage.Instance;
public static readonly EuropeanLanguage European = EuropeanLanguage.Instance;
}
public class AsianLanguage
{
public static readonly AsianLanguage Instance = new AsianLanguage ();
public Language Cantonese {get;private set};
public Language Mandarin {get;private set};
private AsianLanguage (){}
}
public class EuropeanLanguage
{
public static readonly EuropeanLanguage Instance = new EuropeanLanguage ();
public Language Italian {get;private set;}
public Language German {get;private set;}
private EuropeanLanguage (){}
}
Because enums does not support inheritance, I will use classes instead:
public class Language
{
public static readonly UnSpecified = new Laguage();
protected Language()
{ }
}
public class Asian : Language
{
public static readonly Language Cantonese = new Asian();
public static readonly Language Mandarin = new Asian();
protected Asian() : base()
{ }
}
public class European : Language
{
public static readonly Language Italian = new European();
public static readonly Language German = new European();
protected European() : base()
{ }
}
Regardless of whether the data structure is the best or most acceptable design, you need to be consistent. If any of the second level objects requires a struct, then you should keep all 2nd level objects a struct:
public struct Languages
{
public struct Asian
{
public enum Chinese{ Cantonese, Mandarin };
}
public struct European
{
public enum Latin { Italian, Spanish };
public enum Nordic { German };
}
}
That said, I don't think this has the correct feel to it and could get very nasty. You may want to take a look at how the static Encoding class is designed and take some cues from that. It isn't that you are breaking any sort of OOP practice by instancing the Language struct (enums are value types), this just looks like it would be difficult to maintain and not very scalable.
If you're hierarchy is only going to be three levels deep, like in your example
NoteTypes.CustomerService.ID
Would declaring enumerations in different namespaces (but in the same assembly solve your problem)?
namespace NoteTypes { public enum Client { ID = 1, Name = 2}
public enum CustomerService { ID = 1, Name = 2}}
精彩评论