I have the following partial
class created by running xsd /c
against an XML schema file:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="example.com")]
public partial class FRUITQueryType {
private string fruitTypeField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string FruitType {
get {
return this.fruitTypeField;
}
set {
this.fruitTypeField = value;
}
}
}
Although the type is string
, I know there are only three possible values for that field, say Banana
, Orange
, and Blueberry
.
In another part of the program, I check the content of that field:
// assume fruit is an instance of FRUITQueryType
if (fruit.FruitType == "Banana")
{
// do something
}
However, since there are only a few possible values for the field, this approach does not feel neat. I think it would be better if I could check the value of the field somewhat along these lines:
if (fruit.FruitType == FRUITQueryType.FruitType.Banana) // or something similar
Is there any point in achieving this?
If so, what's the best way to do it? By creating a class/struct with three static members containing Banana
, Orange
and Blueberry
?
I came up with a temporary solution - not ideal but does the job.
I defined the three strings as constants in the extended class:
public partial class FRUITQueryType
{
public const string Banana = "Banana";
public const string Orange = "Orange";
public const string Blueberry = "Blueberry";
// ...
}
This way the check becomes:
if (fruit.FruitType == FRUITQueryType.Banana)
I must say I am not entirely happy with this solution, as it feels like cluttering the class.
However, if I defined the constants in a sub-class/struct (say, public struct FruitChoice
), then the check would become more awkward, too (if (fruit.FruitType == FRUITQueryType.FruitChoice.Banana)
)
Anyone comes up with a neater way?