I am using vs 2012. I have a simple string property
string _someString;
public string MyString
{
get
{
return _someString;
}
}
I want this property to hold only certain values. So that when the client uses this property only those certain values can be used.
It sounds like what you really want is an enum:
public enum MyValues //TODO rename all the things
{
SomeValue,
SomeOtherValue,
FinalValue,
}
Then your property can be:
private MyValues value;
public MyValues MyValue
{
get { return value; }
}
If you need to get a string representation of that value just call ToString on the enum value:
string stringValue = value.ToString();
Use an enum as in :
enum MyEnum
{
AllowableValue#1,
AllowableValue#2,
...
}
public MyEnum myEnum { get; set; }
Then populate some UI element with only the values of the enum.
I suppose you want to have some validation on the setter then:
public string MyString
{
get
{
return _someString;
}
set
{
if (value == "a" || value == "b" /* ... */)
_someString = value;
else
throw new InvalidArgumentException("Invalid value!");
}
}
Make sure to set it via the property, not the actual member variable.
Related
I have decorated the following enum with Display DataAnnotation attributes:
public enum RequiredOptions
{
[Display(Name="Optional",Description ="Optional")]
Optional,
[Display(Name="Not Used",Description ="Not Used")]
NotUsed,
[Display(Name="Required",Description ="Required")]
Required
}
I'd like to read out the Name value of the Display attribute for a given enum value in my code. How do I do this?
public static string DisplayRequiredOptionName(RequiredOptions opt)
{
// Return the value of Name from the display attribute from opt
}
Well, after doing some digging in the MVC source code (see src\System.Web.Mvc\Html\SelectExtensions.cs, see GetDisplayName()), here's what I got to work:
public static string GetEnumDisplayName<T>(T enumInstance)
{
return GetDisplayName(enumInstance.GetType().GetField(enumInstance.ToString()));
}
private static string GetDisplayName(FieldInfo field)
{
DisplayAttribute display = field.GetCustomAttribute<DisplayAttribute>(inherit: false);
if (display != null)
{
string name = display.GetName();
if (!String.IsNullOrEmpty(name))
{
return name;
}
}
return field.Name;
}
I have created a class that contains two variables: Type & Value. If the first property (Type) is filled, the second property (Value) can only contain a value that matches the type which is selected on the Type property.
public class Requirement
{
public RequirementType Type { get; set; }
public object Value { get; set; }
public enum RequirementType
{
OS, NetFramework, Connection
}
public enum OSType
{
// Used for RequirementType.OS
Win, Unix, MacOSX
}
public enum NetFrameworkType
{
// Used for RequirementType.NetFramework
Two, Three, Four, FourHalf
}
public enum ConnectionType
{
// Used for RequirementType.Connection
Internet, Connected, None
}
}
I'm using this class in the XAML:
<util:Requirement Type="OS" Value="Win" />
So for example, if the enum value OS has been chosen. The only valid values should be from the enum OSType. I started looking in the .Net source how they solved this with the System.Windows.Trigger & System.Windows.Setter but no success yet.. It seems to be something with the DependsOn attribute and XamlSetTypeConverterAttribute. Does someone know the solution to this problem?
You can use a backing field for value and check each type as it's being set.
public class Requirement
{
public RequirementType Type { get; set; }
private object _value;
public object Value
{
get { return _value; }
set
{
if (Type == RequirementType.OS &&
value.GetType() == typeof(OSType))
{
_value = value;
}
else
{
throw new Exception("Value type is incorrect for Type provided");
}
}
}
}
This test will throw the exception:
var req = new Requirement();
req.Type = RequirementType.OS;
req.Value = RequirementType.Connection;
While this second test will properly set the value:
var req = new Requirement();
req.Type = RequirementType.OS;
req.Value = OSType.Win;
You can use normal properties (type propful and hit Tab):
private RequirementType _type;
public RequirementType Type
{
get { return _type; }
set
{
_type = value;
// do whatever logic you want here
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you give a C# Auto-Property a default value?
I have a property in a class like so
public String fontWeight { get; set; }
I want it's default to be of "Normal"
Is there a way to do this in "automatic" style rather than the following
public String fontWeight {
get { return fontWeight; }
set { if (value!=null) { fontWeight = value; } else { fontWeight = "Normal"; } }
}
Yes you can.
If your looking for something like:
[DefaultValue("Normal")]
public String FontWeight
{
get;
set;
}
Do a google search for 'Aspect Oriented Programming using .NET'
..if this is overkill for you do this:
private string fontWeight;
public String FontWeight {
get
{
return fontWeight ?? "Normal";
}
set {fontWeight = value;}
}
No, an automatic property is just a plain getter and/or setter and a backing variable. If you want to put any kind of logic in the property, you have to use the regular property syntax.
You can use the ?? operator to make it a bit shorter, though:
private string _fontWeight;
public String FontWeight {
get { return _fontWeight; }
set { _fontWeight = value ?? "Normal"; }
}
Note that the setter is not used to initialise the property, so if you don't set the value in the constructor (or assign a value in the variable declaration), the default value is still null. You could make the check in the getter instead to get around this:
private string _fontWeight;
public String FontWeight {
get { return _fontWeight ?? "Normal"; }
set { _fontWeight = value; }
}
You will need to use a backing field.
private string fontWeight;
public String FontWeight
{
get { String.IsNullOrEmpty(fontWeight) ? "Normal" : fontWeight;}
set {fontWeight = String.IsNullOrEmpty(value) ? "Normal" : value;}
}
You either need to use a backing field and initialize that to your default value
private String fontWeight = "Normal";
public String FontWeight
{
get { return fontWeight; }
set { fontWeight = value; }
}
or, keep the auto property and call the setter in your constructor
public constructor()
{
FontWeight = "Normal";
}
public String FontWeight { get; set; }
One way to do it is using PostSharp as detailed in this answer to a similar question.
You could use the DefaultValue attribute:
[DefaultValue("Normal")]
public string FontWeight { get; set; }
Although it notes that
A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.
so you could use this in conjunction with initialisation in the constructor or via a backing field and default handling.
You'd need either a variable like so:
string fontWeight;
public string FontWeight
{
get
{
if (string.IsNullOrEmpty(fontWeight))
fontWeight = "Normal";
return fontWeight;
}
set { fontWeight = value; }
}
Or use a Constructer to set an initial value:
class FontClass
{
public string FontWeight { get; set; }
public FontClass()
{
FontWeight = "Normal";
}
}
i need an enum or something similiar to do something like this:
public enum MyStringEnum {
[StringValue("Foo A")] Foo = "A",
[StringValue("Foo B")] Foo = "B" }
is this possible? my example, i return back a dataset with a value represented as either A,B,C,D,E .. i need a solution to return this as a string representation?
i guess the obvious would be to create an extension method or something which just had a switch statement in and return a string .. any other cleaner solutions?
regards,
dave
Here is something we use for our MVC applications to retrieve a display name for our enums. It uses a custom attribute and an extension method to retrieve the enum display name.
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class EnumDisplayNameAttribute : Attribute
{
public EnumDisplayNameAttribute(string displayName)
{
DisplayName = displayName;
}
public string DisplayName { get; set; }
}
public static string GetDisplayName(this Enum enumType)
{
var displayNameAttribute = enumType.GetType()
.GetField(enumType.ToString())
.GetCustomAttributes(typeof(EnumDisplayNameAttribute), false)
.FirstOrDefault() as EnumDisplayNameAttribute;
return displayNameAttribute != null ? displayNameAttribute.DisplayName : Enum.GetName(enumType.GetType(), enumType);
}
Usage on the enum:
public enum Foo
{
[EnumDisplayName("Foo Bar")]
Bar = 0
}
Getting back the display name:
var f = Foo.Bar;
var name = f.GetDisplayName();
Would it be an option not to use enum and use structs instead?
struct FooEnum
{
private int value;
private string name;
private FooEnum(int value, string name)
{
this.name = name;
this.value = value;
}
public static readonly FooEnum A = new FooEnum(0, "Foo A");
public static readonly FooEnum B = new FooEnum(1, "Foo B");
public static readonly FooEnum C = new FooEnum(2, "Foo C");
public static readonly FooEnum D = new FooEnum(3, "Foo D");
public override string ToString()
{
return this.name;
}
//TODO explicit conversion to int etc.
}
You could then use FooEnum like an enum with an own ToString() overload:
FooEnum foo = FooEnum.A;
string s = foo.ToString(); //"Foo A"
If you want to do something like this:
MyStringEnum value = MyStringEnum.A;
string description = value.GetDescription();
// description == "Foo A"
Setup your enum like this:
public enum MyStringEnum
{
[Description("Foo A")]
A,
[Description("Foo B")]
B
}
And use a utility/extension method that reads the attribute:
public static string GetDescription(this MyStringEnum enumerationValue)
{
Type type = enumerationValue.GetType();
string name = enumerationValue.ToString();
//Tries to find a DescriptionAttribute for a potential friendly name for the enum
MemberInfo[] member = type.GetMember(name);
if (member != null && member.Length > 0)
{
object[] attributes = member[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
//Pull out the description value
return ((DescriptionAttribute)attributes[0]).Description;
}
}
return name;
}
I've seen this done where I would put
MyStringEnum.Foo.ToString();
In this case it would give "A"
The cleanest solution for this problem is to create a custom attribute that will store the string value you want for the enum constant. I've used that strategy in the past and it worked out fairly well. Here's a blog post detailing the work involved:
Enum With String Values In C# - Stefan Sedich's Blog
Of course this is only necessary if you need some kind of meaningful text. If the name of the enum constant works for you...then you can simply call ToString().
I have a string property that I would like to be able to force two things with:
- It can only be set to specific vaues within a pre-defined list,
- Error checking of the property's value can be performed at compile time.
An enum fits the bill perfectly except that in my list of pre-defined strings there is one with a hyphen and enum values cannot contain hyphens. To illustrate the ideal solution if an enum could contain hyphens I would create an enum of:
public enum SIPEventPackagesEnum
{
dialog,
message-summary,
refer
}
To use:
SIPEventPackagesEnum EventPackage = SIPEventPackagesEnum.message-summary;
To set:
string eventPackageStr = "message-summary";
SIPEventPackagesEnum EventPackage = (SIPEventPackagesEnum)Enum.Parse(typeof(SIPEventPackagesEnum), eventPackageStr, true);
In the above cases it's impossible to set the EventPackage property to anything but one of the enum values and is intuitive to use since intellisense will list the available options.
Due to the inability to use a hyphen, and I cannot change the pre-defined list to remove the hyphen, the very crude approach is to use a struct and have a "Value" property on the struct that does the enforcing in its setter, see below. It's very verbose compared to using an enum and also doesn't allow any compile time checking and isn't very intuitive.
Has anyone encountered this problem before and have a better solution? I have multiple lists with items containing hyphens so it's not a once off.
public struct SIPEventPackages
{
public const string DIALOG = "dialog";
public const string MESSAGE_SUMMARY = "message-summary";
public const string REFER = "refer";
public string Value
{
get { return Value; }
set
{
if (IsValid(value))
{
Value = value.ToLower();
}
else
{
throw new ArgumentException(value + " is invalid for a SIP event package.");
}
}
}
public bool IsValid(string value)
{
if (value.IsNullOrBlank())
{
return false;
}
else if (value.ToLower() == DIALOG || value.ToLower() == MESSAGE_SUMMARY || value.ToLower() == REFER)
{
return true;
}
else
{
return false;
}
}
public override string ToString()
{
return Value;
}
}
Seems this is simple using an delegate
Func<string, bool> isValid = str =>
{
List<string> validLst = new List<string>() { "dialog","message-summary","refer" };
if (validLst.Find(x => string.Equals(x,str,StringComparison.InvariantCultureIgnoreCase)) == null)
return false;
return true;
};
var teststr1 = "message-summary";
var teststr2 = "wrongone";
isValid(teststr1);
isValid(teststr2);
Uptdate:
Otherwise you can use the enum approach in a little different way. Have a enum value without an hyphen. And just strip the hyphens from your source string when parse the enum values. this will work as you expected
public enum SIPEventPackagesEnum
{
dialog,
messagesummary,
refer
}
string eventPackageStr = "message-summary";
SIPEventPackagesEnum EventPackage = (SIPEventPackagesEnum)Enum.Parse(typeof(SIPEventPackagesEnum), eventPackageStr.Replace("-",""), true);
You could create the enum without -, then have a static Dictionary<SIPEventPackagesEnum
,string> in a helper class mapping from enum value to string to convert from enum to string, then use Enum.Parse(typeof(SIPEventPackagesEnum), str.Replace("-", "")) when converting from string to enum.
Or use _ instead of - and replace _ with - and vice versa when required
Or use camel case in the enum values and replace a capital letter within the enum name with "-<lowercase letter>" using a regex
I managed to fine tune my approach so that it's almost as good as an enum albeit with a lot more plumbing code required. It's worth the plumbing code to save potential misues problems in the future.
public struct SIPEventPackage
{
public static SIPEventPackage None = new SIPEventPackage(null);
public static SIPEventPackage Dialog = new SIPEventPackage("dialog");
public static SIPEventPackage MessageSummary = new SIPEventPackage("message-summary");
public static SIPEventPackage Refer = new SIPEventPackage("refer");
private string m_value;
private SIPEventPackage(string value)
{
m_value = value;
}
public override string ToString()
{
return m_value;
}
public static SIPEventPackage Parse(string value)
{
if (!IsValid(value))
{
throw new ArgumentException("The value is not valid for a SIPEventPackage.");
}
else
{
string trimmedValue = value.Trim().ToLower();
switch (trimmedValue)
{
case "dialog": return SIPEventPackage.Dialog;
case "message-summary": return SIPEventPackage.MessageSummary;
case "refer": return SIPEventPackage.Refer;
default: throw new ArgumentException("The value is not valid for a SIPEventPackage.");
}
}
}
}
There's a little bit more plumbing required, implementing an IsValid method and operator == and a few more, but the main thing is I can now use the struct in almost an identical way to an enum and can have items with hyphens.