ErrorProvider - Input string was not in a recognized format C# - c#

I'm currently work with Entity Framework 5.0 and WinForms. What I'd like to do is set-up validation on my POCO classes so that when I databind them to form fields I'd like to display UI validation errors via an ErrorProvider. I have set up a base "ValidationEntity" class that implements the IDataErrorInfo interface with some simple validation methods for my child classes to call. For the most part, validating the field length, range, etc.. seem to be working fine displaying the errors to the user via the ErrorProvider.
However, I seem to have ran into an issue with the "ValidateRequiredField" method. If I have a class that has a non-nullable integer field and the user happens to remove this value on the form the ErrorProvider does show a message to the end-user, but the message is "Input string was not in a recognized format". Now I assume this is because the form, being bound to an integer field, is attempting to convert the empty text into an integer and a conversion error is occurring prior to the value being sent to the POCO class property. My question is, what would the best approach to solve this be?
I'm guessing that I may have to implement the Validating methods of the TextBox controls inside the form, catch the empty/null entry, and set the appropriate error message on the error provider. However, I was hoping on a way to let the class handle the empty/null value and set the error inside the POCO class so that it propagates to the UI. Originally I had thought of creating a custom TypeConverter (e.g. RequiredIntTypeConverter) but ran into issues since I'm inheriting from the ValidationEntity class and couldn't think of a good way of adding to the errors.
Below is a sample from the ValidationEntity class as well as a snippet from the Company class.
ValidationEntity.cs
public class ValidationEntity : IDataErrorInfo
{
private readonly Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>();
string IDataErrorInfo.Error
{
get { return string.Empty; }
}
string IDataErrorInfo.this[string propertyName]
{
get
{
return (!errors.ContainsKey(propertyName) ? null : String.Join(Environment.NewLine, errors[propertyName]));
}
}
public void AddError(string propertyName, string error, bool isWarning)
{
if (!errors.ContainsKey(propertyName))
{
errors[propertyName] = new List<string>();
}
if (!errors[propertyName].Contains(error))
{
if (isWarning)
{
errors[propertyName].Add(error);
}
else
{
errors[propertyName].Insert(0, error);
}
}
}
public void RemoveError(string propertyName, string error)
{
if (errors.ContainsKey(propertyName) &&
errors[propertyName].Contains(error))
{
errors[propertyName].Remove(error);
if (errors[propertyName].Count == 0)
{
errors.Remove(propertyName);
}
}
}
public void ValidateFieldLength(string propertyName, string value, int maxLength)
{
string errorMessage = string.Format("Text entered exceeds max length of {0} characters", maxLength);
if (value != null)
{
if (value.Length > maxLength)
{
if (!errors.ContainsKey(propertyName))
{
AddError(propertyName, errorMessage, false);
}
}
else
{
RemoveError(propertyName, errorMessage);
}
}
else
{
RemoveError(propertyName, errorMessage);
}
}
public void ValidateRequiredField(string propertyName, string value)
{
string errorMessage = string.Format("{0} is required.", propertyName);
if (string.IsNullOrWhiteSpace(value))
{
AddError(propertyName, errorMessage, false);
}
else
{
RemoveError(propertyName, errorMessage);
}
}
}
Company.cs
public class Company : ValidationEntity
{
private int companyID;
private string companyName;
public int CompanyID
{
get { return this.companyID; }
set
{
OnCompanyIdChanging(value.ToString());
this.companyID = value;
}
}
public string CompanyName
{
get { return this.companyName; }
set
{
OnCompanyNameChanging(value);
this.companyName = value;
}
}
private void OnCompanyIdChanging(string value)
{
ValidateRequiredField("CompanyID", value);
}
private void OnCompanyNameChanging(string value)
{
ValidateRequiredField("CompanyName", value);
ValidateFieldLength("CompanyName", value, 30);
}
}
Thank you for your assistance.

After doing some more research on this and testing out code samples I was able to find a solution to this particular item. A custom TypeConverter was needed for a non-nullable integer conversion. I was able to locate information here
and ended up with the following TypeConverter for testing a "Required Integer":
public class RequiredIntConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
[System.Diagnostics.DebuggerNonUserCode]
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
{
throw new ApplicationException("This field requires an integer value and cannot be blank.");
}
int result = 0;
if (!int.TryParse(value.ToString(), out result))
{
throw new ApplicationException("The value could not be parsed as a valid integer data type.");
}
else
{
return result;
}
}
}
When I initially tested the code from the link above my debugger kept breaking when I attempted to throw the ApplicationException. Thinking this was an error in my code I was confused on how to use the TypeConverter. However, I found the following post which describes how to suppress the debugger from breaking on a method.
I'm still a bit unsure on why it breaks on this ApplicationException and how the ErrorProvider knows to display the underlying exception. If anyone could point me to some additional resources on this it would be greatly appreciated.
Have a great day.

Related

C# Optional<TObject> as a return type?

Often i have a method where i want to return the error if something goes wrong, and instead of returning null, I want something less prone to errors at runtime and more easy to consume. Is there anything already done in .Net or maybe a nuget package?
Maybe have a constructor with optional parameters or object initializer would be enough?
This would have been the first approach but then every new Dto has to either have these Error property or inherit from a base class.
if (condition)
{
return new MyDto(null, error);
}
return new MyDto(someVariable, null);
So I've made this class to use a return type:
public class Optional<TObject> where TObject : class
{
public Optional(TObject? value)
{
Value = value;
}
public Optional(String error)
{
Error = error;
}
public TObject? Value { get; }
public String Error { get;} = String.Empty;
public Boolean IsError => !String.IsNullOrEmpty(Error);
}
I return it in the method:
if (condition)
{
return new Optional(error);
}
return new Optional(new MyDto(someVariable));
And then consume it like this:
var result = await myService.GetSomethingAsync();
if(result.IsError)
{
await DisplayAlert("error", result.Error, "Ok");
}
else
{
await DoSomethingElse(result.Value);
}
By creating a small class hierarchy, you could ensure that the Value property is only available when no error occurred
public abstract class Result
{
public virtual string Message => null;
public static Error Error(string message) => new Error(message);
public static Okay<T> Okay<T>(T value) where T : class => new Okay<T>(value);
}
public class Error : Result
{
public Error(string errorMessage) => Message = errorMessage;
override public string Message { get; }
}
public class Okay<T> : Result
where T : class
{
public Okay(T value) => Value = value;
public T Value { get; }
}
Usage
Result result = Result.Error("Something went wrong");
// OR
Result result = Result.Okay(new MyDto(someVariable));
if (result is Okay<MyDto> dtoResult) {
Console.WriteLine(dtoResult.Value);
} else {
Console.WriteLine(result.Message);
}
Or by using a recursive pattern, we can retrieve the value into a variable directly
if (result is Okay<MyDto> { Value: var dto }) {
Console.WriteLine(dto);
} else {
Console.WriteLine(result.Message);
}
Note that I have declared the Message property in the abstract base class Result, so that you don't have to cast to the Error type to get the message.
I used null as defualt value for the error message, as it allows us to write
Console.Writeline(result.Message ?? "okay");
This OneOf recommendation you got looks promising. I will personally have a look at it later.
What I do with my services is to standardize the result they return by using a SvcResult class or an inherited class.
Example:
public class SvcResult
{
public List<Error> Errors { get; } // Error is a class of my own. Add set; if deserialization is needed.
public bool Success { get; } // Add set; if deserialization is needed.
// Then parameterless constructor for a successful result.
// Then parameterized constructor to receive errors for a failed result.
}
That is the class for side-effect service calling. If The service returns data, I derive from the above to create DataSvcResult:
public class DataSvcResult<TResult> : SvcResult
{
public TResult Data { get; }
// Add constructor that receives TResult for a successful object result.
// Expose base class constructor that takes errors.
}
Basically that's what I do. But that OneOf thing, though. Looks super intersting.

Special meaning of property "Name" within a property grid

I use a PropertyGrid to allow the enduser to edit properties from a class ClassA
This class has a List<ClassB> property.
For the List<ClassB> property, the PropertyGrid shows (Collection) and a button with 3 dots which opens a new windows which looks like this (taken from another SO post).
I want to customize the Members: DisplayName on the left, so for ClassB I have overridden the ToString() method
public class ClassB
{
public string Name { get; set; }
public TimeSpan Value { get; set; }
public override ToString() { return String.Format("{0} ({1})", this.Name, this.Value); }
}
Now here comes the problem:
If Name is empty, it shows (00:00:00) as expected.
If I change the name to Test I expect it to show Test (00:00:00), but it just shows Test
If I rename the property Name to something else, it works as expected.
I suppose this is a special conventions that, if a class has a property Name and the value is not null or empty, the control shows this property rather than the name.
However, I have not found a doc that verifies that and I don't know how to change this behaviour. How do I achive this?
Note: Changing the property name is not an option.
Unfortunately, the logic is pretty hardcoded in the CollectionEditor.GetDisplayText Method. It's not documented, but you can disassemble it with a tool. This is the code:
protected virtual string GetDisplayText(object value)
{
string str;
if (value == null)
return string.Empty;
// use the Name property
PropertyDescriptor defaultProperty = TypeDescriptor.GetProperties(value)["Name"];
if ((defaultProperty != null) && (defaultProperty.PropertyType == typeof(string)))
{
str = (string) defaultProperty.GetValue(value);
if ((str != null) && (str.Length > 0))
{
return str;
}
}
// or use the DefaultPropertyAttribute
defaultProperty = TypeDescriptor.GetDefaultProperty(this.CollectionType);
if ((defaultProperty != null) && (defaultProperty.PropertyType == typeof(string)))
{
str = (string) defaultProperty.GetValue(value);
if ((str != null) && (str.Length > 0))
{
return str;
}
}
// or use the TypeConverter
str = TypeDescriptor.GetConverter(value).ConvertToString(value);
if ((str != null) && (str.Length != 0))
{
return str;
}
// or use the type name
return value.GetType().Name;
}
This code is pretty nasty because it does things basically the other way around. It should use the Name property as the last resort instead of focusing on it...
But all hope is not lost since the CollectionEditor class is not sealed. This is how you can fix it:
1) declare the EditorAttribute on the class holding the collection, something like this:
public class ClassA
{
[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]
public List<ClassB> List { get; set; }
}
2) define you custom collection editor, like this;
public class MyCollectionEditor : CollectionEditor // needs a reference to System.Design
{
public MyCollectionEditor(Type type)
: base(type)
{
}
protected override string GetDisplayText(object value)
{
// force ToString() usage, but
// you also could implement some custom logic here
return string.Format("{0}", value);
}
}

using INotifyDataErrorInfo

On my class I am implementing INotifyDataErrorInfo and it is working fine when validation error happens. It is putting that red frame around the TextBox, but it is not getting rid of it when validation error is fixed.
Here is my code:
string IDataErrorInfo.this[string columnName]
{
get
{
string error = null;
if (Context != null)
{
var messages = //GetMessages(); messages are validation errors
if (messages.Count != 0)
{
error = "This is critical error. Must be fixed";
AddError(columnName, "Some kind of error happend", false);
}
else
{
RemoveError(columnName);
}
}
return error;
}
}
//I call this method to check for validation errors.
public void CheckValidationErrors(string propertyName)
{
var error = this as IDataErrorInfo;
string message = error[propertyName];
}
private Dictionary<String, List<String>> errors =
new Dictionary<string, List<string>>();
public void AddError(string propertyName, string error, bool isWarning)
{
if (!errors.ContainsKey(propertyName))
errors[propertyName] = new List<string>();
if (!errors[propertyName].Contains(error))
{
if (isWarning) errors[propertyName].Add(error);
else errors[propertyName].Insert(0, error);
RaiseErrorsChanged(propertyName);
}
}
public void RemoveError(string propertyName, string error="")
{
if (error == "")
{
errors.Remove(propertyName);
RaiseErrorsChanged(propertyName);
}
else
{
if (errors.ContainsKey(propertyName) &&
errors[propertyName].Contains(error))
{
errors[propertyName].Remove(error);
if (errors[propertyName].Count == 0) errors.Remove(propertyName);
RaiseErrorsChanged(propertyName);
}
}
}
public void RaiseErrorsChanged(string propertyName)
{
if (ErrorsChanged != null)
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public System.Collections.IEnumerable GetErrors(string propertyName)
{
if (String.IsNullOrEmpty(propertyName) ||
!errors.ContainsKey(propertyName)) return null;
return errors[propertyName];
}
public bool HasErrors
{
get { return errors.Count > 0; }
}
I am calling RemoveError() method to remove errors.
Am I doing something wrong? When validation fixed I need to go to TextBox and tabbing of will take care of it. I want to remove that red frame right away when validation error is gone.
UPDATE:
When I type something to text box I need to send that info to the server asynchronously and response will bring me a message if there is any validation issues. So I can't do validation things on property changed. I will check for response if there any message added or removed. If any removed then I will call CheckValidationErrors().
ANSWER
I was implementing IDataErrorInfo and then decided to implement INotifyDataErrorInfo. I was thinking to make this validation work I need to implement both interfaces. So basically I removed IDataErrorInfo implementation from my class and that fixed the problem.
Thanks for the help!
Default value of UpdateSourceTrigger for TextBox is LostFocus.
You should change it to PropertyChanged if you want to run your validation logic right away once error is fixed.
<TextBox Text="{Binding PropertyName, UpdateSourceTrigger=PropertyChanged}"/>
I was implementing IDataErrorInfo and then decided to implement INotifyDataErrorInfo. I was thinking to make this validation work I need to implement both interfaces. So basically I removed IDataErrorInfo implementation from my class and that fixed the problem.

What's the order of execution in property setters when using IDataErrorInfo?

Situation: Many times with WPF, we use INotifyPropertyChanged and IDataErrorInfo to enable binding and validation on our data objects. I've got a lot of properties that look like this:
public SomeObject SomeData
{
get { return _SomeData; }
set { _SomeData = value; OnPropertyChanged("SomeData"); }
}
Of course, I have an appropriate overridden IDataErrorInfo.this[] in my class to do validation.
Question: In a binding situation, when does the validation code get executed? When is the property set? When is the setter code executed? What if the validation fails?
For example:
User enters new data.
Binding writes data to property.
Property set method is executed.
Binding checks this[] for validation.
If the data is invalid, the binding sets the property back to the old value.
Property set method is executed again.
This is important if you are adding "hooks" into the set method, like:
public string PathToFile
{
get { return _PathToFile; }
set
{
if (_PathToFile != value && // prevent unnecessary actions
OnPathToFileChanging(value)) // allow subclasses to do something or stop the setter
{
_PathToFile = value;
OnPathToFileChanged(); // allow subclasses to do something afterwards
OnPropertyChanged("PathToFile");
}
}
}
If you want fine-grained control over the timing of validation, you can have it:
private Dictionary<string, string> Errors = new Dictionary<string, string>();
private object _MyProperty;
public object MyProperty
{
get { return _MyProperty; }
set
{
Errors["MyProperty"] = null;
if (value == _MyProperty)
{
return;
}
ValidateMyProperty(value); // may set Errors["MyProperty"]
if (Errors["MyProperty"] == null)
{
_MyProperty = value;
OnPropertyChanged("MyProperty");
}
}
}
public string this[string propertyName]
{
return Errors[propertyName];
}
No matter when data error information is requested and who's requesting it, it always returns the property's validation status as of the last time something tried to set the property.
Note that if you work at it, you can encapsulate the logic thusly:
public object MyProperty
{
set { _MyProperty = Validate("MyProperty", value, _MyProperty); }
}
private Dictionary<string, Func<object, string>> ValidationFunctions;
private object Validate(string propertyName, object value, object field)
{
Errors[propertyName] = null;
if (value == field)
{
return;
}
if (!ValidationFunctions.ContainsKey(propertyName))
{
return value;
}
Errors[propertyName] = ValidationFunctions[propertyName](value);
return (Errors[propertyName] == null)
? value
: field;
}
}

Associating enums with strings in C#

I know the following is not possible because the Enumeration's type has to be an int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
From my database I get a field with incomprehensive codes (the OEM and CMBs). I would want to make this field into an enum or something else understandable. Because if the target is readability, the solution should be terse.
What other options do I have?
I like to use properties in a class instead of methods, since they look more enum-like.
Here's an example for a Logger:
public class LogCategory
{
private LogCategory(string value) { Value = value; }
public string Value { get; private set; }
public static LogCategory Trace { get { return new LogCategory("Trace"); } }
public static LogCategory Debug { get { return new LogCategory("Debug"); } }
public static LogCategory Info { get { return new LogCategory("Info"); } }
public static LogCategory Warning { get { return new LogCategory("Warning"); } }
public static LogCategory Error { get { return new LogCategory("Error"); } }
public override string ToString()
{
return Value;
}
}
Pass in type-safe string values as a parameter:
public static void Write(string message, LogCategory logCategory)
{
var log = new LogEntry { Message = message };
Logger.Write(log, logCategory.Value);
}
Usage:
Logger.Write("This is almost like an enum.", LogCategory.Info);
You could also use the extension model:
public enum MyEnum
{
[Description("String 1")]
V1= 1,
[Description("String 2")]
V2= 2
}
Your Extension Class
public static class MyEnumExtensions
{
public static string ToDescriptionString(this MyEnum val)
{
DescriptionAttribute[] attributes = (DescriptionAttribute[])val
.GetType()
.GetField(val.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : string.Empty;
}
}
usage:
MyEnum myLocal = MyEnum.V1;
print(myLocal.ToDescriptionString());
How about using a static class with constants?
static class GroupTypes
{
public const string TheGroup = "OEM";
public const string TheOtherGroup = "CMB";
}
void DoSomething(string groupType)
{
if(groupType == GroupTypes.TheGroup)
{
// Be nice
}
else if (groupType == GroupTypes.TheOtherGroup)
{
// Continue to be nice
}
else
{
// unexpected, throw exception?
}
}
Try adding constants to a static class. You don't end up with a Type, but you will have readable, organised constants:
public static class GroupTypes {
public const string TheGroup = "OEM";
public const string TheOtherGroup = "CMB";
}
I used a structure as alluded to in a previous answer, but did away with any complexity. To me, this was most like creating an enumeration of strings. It is used in the same manner that an enumeration is used.
struct ViewTypes
{
public const string View1 = "Whatever string you like";
public const string View2 = "another string";
}
Example use:
switch( some_string_variable )
{
case ViewTypes.View1: /* do something */ break;
case ViewTypes.View2: /* do something else */ break;
}
You can do it very easily actually. Use the following code.
enum GroupTypes
{
OEM,
CMB
};
Then when you want to get the string value of each enum element just use the following line of code.
String oemString = Enum.GetName(typeof(GroupTypes), GroupTypes.OEM);
I've used this method successfully in the past, and I've also used a constants class to hold string constants, both work out pretty well, but I tend to prefer this.
You can add attributes to the items in the enumeration and then use reflection to get the values from the attributes.
You would have to use the "field" specifier to apply the attributes, like so:
enum GroupTypes
{
[field:Description("OEM")]
TheGroup,
[field:Description("CMB")]
TheOtherGroup
}
You would then reflect on the static fields of the type of the enum (in this case GroupTypes) and get the DescriptionAttribute for the value you were looking for using reflection:
public static DescriptionAttribute GetEnumDescriptionAttribute<T>(
this T value) where T : struct
{
// The type of the enum, it will be reused.
Type type = typeof(T);
// If T is not an enum, get out.
if (!type.IsEnum)
throw new InvalidOperationException(
"The type parameter T must be an enum type.");
// If the value isn't defined throw an exception.
if (!Enum.IsDefined(type, value))
throw new InvalidEnumArgumentException(
"value", Convert.ToInt32(value), type);
// Get the static field for the value.
FieldInfo fi = type.GetField(value.ToString(),
BindingFlags.Static | BindingFlags.Public);
// Get the description attribute, if there is one.
return fi.GetCustomAttributes(typeof(DescriptionAttribute), true).
Cast<DescriptionAttribute>().SingleOrDefault();
}
I opted to return the DescriptionAttribute itself above, in the event that you want to be able to determine whether or not the attribute is even applied.
Use a class.
Edit: Better example
class StarshipType
{
private string _Name;
private static List<StarshipType> _StarshipTypes = new List<StarshipType>();
public static readonly StarshipType Ultralight = new StarshipType("Ultralight");
public static readonly StarshipType Light = new StarshipType("Light");
public static readonly StarshipType Mediumweight = new StarshipType("Mediumweight");
public static readonly StarshipType Heavy = new StarshipType("Heavy");
public static readonly StarshipType Superheavy = new StarshipType("Superheavy");
public string Name
{
get { return _Name; }
private set { _Name = value; }
}
public static IList<StarshipType> StarshipTypes
{
get { return _StarshipTypes; }
}
private StarshipType(string name, int systemRatio)
{
Name = name;
_StarshipTypes.Add(this);
}
public static StarshipType Parse(string toParse)
{
foreach (StarshipType s in StarshipTypes)
{
if (toParse == s.Name)
return s;
}
throw new FormatException("Could not parse string.");
}
}
Create a second enum, for your DB containing the following:
enum DBGroupTypes
{
OEM = 0,
CMB = 1
}
Now, you can use Enum.Parse to retrieve the correct DBGroupTypes value from the strings "OEM" and "CMB". You can then convert those to int and retrieve the correct values from the right enumeration you want to use further in your model.
Another way to deal with the problem, is to have a enum and a array of strings that will map the enum values with the list of strings:
public enum GroupTypes
{
TheGroup = 0,
TheOtherGroup
}
string[] GroupTypesStr = {
"OEM",
"CMB"
};
you may use it something like this:
Log.Write(GroupTypesStr[(int)GroupTypes.TheOtherGroup]);
It will prompt CMB
PROS:
Easy and clean code.
High Performance (specially in comparison with those approaches that
uses classes)
CONS:
Prone to mess up the list when editing it, but it will be okay for a
short list.
Here is the extension method that I used to get the enum value as string. First here is the enum.
public enum DatabaseEnvironment
{
[Description("AzamSharpBlogDevDatabase")]
Development = 1,
[Description("AzamSharpBlogQADatabase")]
QualityAssurance = 2,
[Description("AzamSharpBlogTestDatabase")]
Test = 3
}
The Description attribute came from System.ComponentModel.
And here is my extension method:
public static string GetValueAsString(this DatabaseEnvironment environment)
{
// get the field
var field = environment.GetType().GetField(environment.ToString());
var customAttributes = field.GetCustomAttributes(typeof (DescriptionAttribute), false);
if(customAttributes.Length > 0)
{
return (customAttributes[0] as DescriptionAttribute).Description;
}
else
{
return environment.ToString();
}
}
Now, you can access the enum as string value using the following code:
[TestFixture]
public class when_getting_value_of_enum
{
[Test]
public void should_get_the_value_as_string()
{
Assert.AreEqual("AzamSharpBlogTestDatabase",DatabaseEnvironment.Test.GetValueAsString());
}
}
New in .Net Core 3.0/C# 8.0 (if your work environment allows you to upgrade your project) is a short-hand switch statement that looks somewhat enum-ish. At the end of the day it's the same old boring switch statement we've been using for years.
Only real difference here is that the switch statement got a new suit.
public static RGBColor FromRainbow(Rainbow colorBand) =>
colorBand switch
{
Rainbow.Red => new RGBColor(0xFF, 0x00, 0x00),
Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
Rainbow.Green => new RGBColor(0x00, 0xFF, 0x00),
Rainbow.Blue => new RGBColor(0x00, 0x00, 0xFF),
Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
_ => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
};
You'll notice that the code above which I copied from here, is actually using an enum as a param.
It's not exactly what you want (and trust me, I've wanted something of similar to what the OP is requesting for a long time), but I actually feel like this is somewhat of an olive branch from MS. JMO.
Hope it helps someone!
Why not just use the same enum, but just call .ToString()?
using System;
public class EnumSample
{
enum Holidays
{
Christmas = 1,
Easter = 2
};
public static void Main()
{
Enum myHolidays = Holidays.Christmas;
Console.WriteLine("The value of this instance is '{0}'", myHolidays.ToString());
}
}
Taken from #EvenMien and added in some of the comments. (Also for my own use case)
public struct AgentAction
{
private AgentAction(string value) { Value = value; }
public string Value { get; private set; }
public override string ToString() { return this.Value; }
public static AgentAction Login = new AgentAction("Login");
public static AgentAction Logout = new AgentAction("Logout");
public static implicit operator string(AgentAction action) { return action.ToString(); }
}
Have you considered a lookup table using a Dictionary?
enum GroupTypes
{
TheGroup,
TheOtherGroup
}
Dictionary<string, GroupTypes> GroupTypeLookup = new Dictionary<string, GroupTypes>();
// initialize lookup table:
GroupTypeLookup.Add("OEM", TheGroup);
GroupTypeLookup.Add("CMB", TheOtherGroup);
You can then use GroupTypeLookup.TryGetValue() to look up a string when you read it.
I would just create a dictionary and use the code as the key.
Edit: To address the comment about doing a reverse lookup (finding the key), this would not be terribly efficient. If this is necessary, I would write a new class to handle it.
public class DataType
{
private readonly string value;
private static readonly Dictionary<string, DataType> predefinedValues;
public static readonly DataType Json = new DataType("json");
public static readonly DataType Xml = new DataType("xml");
public static readonly DataType Text = new DataType("text");
public static readonly DataType Html = new DataType("html");
public static readonly DataType Binary = new DataType("binary");
static DataType()
{
predefinedValues = new Dictionary<string, DataType>();
predefinedValues.Add(Json.Value, Json);
predefinedValues.Add(Xml.Value, Xml);
predefinedValues.Add(Text.Value, Text);
predefinedValues.Add(Html.Value, Html);
predefinedValues.Add(Binary.Value, Binary);
}
private DataType(string value)
{
this.value = value;
}
public static DataType Parse(string value)
{
var exception = new FormatException($"Invalid value for type {nameof(DataType)}");
if (string.IsNullOrEmpty(value))
throw exception;
string key = value.ToLower();
if (!predefinedValues.ContainsKey(key))
throw exception;
return predefinedValues[key];
}
public string Value
{
get { return value; }
}
}
Here is my take on this, using C# 9.0 syntax to keep it clean. I define a base class for the enums:
public class StringEnum
{
protected StringEnum(string value) { Value = value; }
public string Value { get; }
public override string ToString() => Value;
}
Creating new enum style types is then easy and compact:
public class GroupTypes : StringEnum
{
private GroupTypes(string value) : base(value) {}
public static readonly GroupTypes TheGroup = new("OEM");
public static readonly GroupTypes TheOtherGroup = new("CMB");
}
Use it like this:
void Example(GroupTypes groupType)
{
Console.WriteLine(groupType); // Will print "OEM" or "CMB"
if (groupType == GroupTypes.TheGroup) { ... }
}
You can also add more functionality to StringEnum, which will then be available for all your subclasses (e. g., implementing IComparable and overriding Equals and GetHashCode)
My first question - Do you have access to the Database itself? This should be normalized in the database, ideally, otherwise, any solution is going to be prone to error. In my experience, data fields full of "OEM" and "CMB" tend to wind up having things like "oem " and other 'crap data' mixed in over time.... If you can normalize it, you could use the key in the table containing the elements as your Enum, and you're done, with a much cleaner structure.
If that's not available, I'd make your Enum, and make a class to parse your string into the Enum for you. This would at least give you some flexibility in handling non-standard entries and much more flexibility for trapping or handling errors than doing any of the workarounds using Enum.Parse/Reflection/etc. A dictionary would work, but could break down if you ever have case issues, etc.
I'd recommend writing a class so you can do:
// I renamed this to GroupType, since it sounds like each element has a single type...
GroupType theType = GroupTypeParser.GetGroupType(theDBString);
This preserves most of your readability without having to change the DB.
C# doesn't support enumerated strings, but for most situations you can use a List or Dictionary to get the desired effect.
E.g. To print pass/fail results:
List<string> PassFail = new List<string> { "FAIL", "PASS" };
bool result = true;
Console.WriteLine("Test1: " + PassFail[result.GetHashCode()]);
This is a way to use it as a strongly typed parameter or as a string :
public class ClassLikeEnum
{
public string Value
{
get;
private set;
}
ClassLikeEnum(string value)
{
Value = value;
}
public static implicit operator string(ClassLikeEnum c)
{
return c.Value;
}
public static readonly ClassLikeEnum C1 = new ClassLikeEnum("RandomString1");
public static readonly ClassLikeEnum C2 = new ClassLikeEnum("RandomString2");
}
A small tweak to Glennular Extension method, so you could use the extension on other things than just ENUM's;
using System;
using System.ComponentModel;
namespace Extensions {
public static class T_Extensions {
/// <summary>
/// Gets the Description Attribute Value
/// </summary>
/// <typeparam name="T">Entity Type</typeparam>
/// <param name="val">Variable</param>
/// <returns>The value of the Description Attribute or an Empty String</returns>
public static string Description<T>(this T t) {
DescriptionAttribute[] attributes = (DescriptionAttribute[])t.GetType().GetField(t.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : string.Empty;
}
}
}
Or Using Linq
using System;
using System.ComponentModel;
using System.Linq;
namespace Extensions {
public static class T_Extensions {
public static string Description<T>(this T t) =>
((DescriptionAttribute[])t
?.GetType()
?.GetField(t?.ToString())
?.GetCustomAttributes(typeof(DescriptionAttribute), false))
?.Select(a => a?.Description)
?.FirstOrDefault()
?? string.Empty;
}
}
Following the answer of #Even Mien I have tried to go a bit further and make it Generic, I seem to be almost there but one case still resist and I probably can simplify my code a bit.
I post it here if anyone see how I could improve and especially make it works as I can't assign it from a string
So Far I have the following results:
Console.WriteLine(TestEnum.Test1);//displays "TEST1"
bool test = "TEST1" == TestEnum.Test1; //true
var test2 = TestEnum.Test1; //is TestEnum and has value
string test3 = TestEnum.Test1; //test3 = "TEST1"
var test4 = TestEnum.Test1 == TestEnum.Test2; //false
EnumType<TestEnum> test5 = "TEST1"; //works fine
//TestEnum test5 = "string"; DOESN'T compile .... :(:(
Where the magics happens :
public abstract class EnumType<T> where T : EnumType<T>
{
public string Value { get; set; }
protected EnumType(string value)
{
Value = value;
}
public static implicit operator EnumType<T>(string s)
{
if (All.Any(dt => dt.Value == s))
{
Type t = typeof(T);
ConstructorInfo ci = t.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,null, new Type[] { typeof(string) }, null);
return (T)ci.Invoke(new object[] {s});
}
else
{
return null;
}
}
public static implicit operator string(EnumType<T> dt)
{
return dt?.Value;
}
public static bool operator ==(EnumType<T> ct1, EnumType<T> ct2)
{
return (string)ct1 == (string)ct2;
}
public static bool operator !=(EnumType<T> ct1, EnumType<T> ct2)
{
return !(ct1 == ct2);
}
public override bool Equals(object obj)
{
try
{
return (string)obj == Value;
}
catch
{
return false;
}
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public static IEnumerable<T> All
=> typeof(T).GetProperties()
.Where(p => p.PropertyType == typeof(T))
.Select(x => (T)x.GetValue(null, null));
}
I only then have to declare this for my enums:
public class TestEnum : EnumType<TestEnum>
{
private TestEnum(string value) : base(value)
{}
public static TestEnum Test1 { get { return new TestEnum("TEST1"); } }
public static TestEnum Test2 { get { return new TestEnum("TEST2"); } }
}
I would make it into a class an avoid an enum altogether. And then with the usage of a typehandler you could create the object when you grab it from the db.
IE:
public class Group
{
public string Value{ get; set; }
public Group( string value ){ Value = value; }
public static Group TheGroup() { return new Group("OEM"); }
public static Group OtherGroup() { return new Group("CMB"); }
}
If I understand correctly, you need a conversion from string to enum:
enum GroupTypes {
Unknown = 0,
OEM = 1,
CMB = 2
}
static GroupTypes StrToEnum(string str){
GroupTypes g = GroupTypes.Unknown;
try {
object o = Enum.Parse(typeof(GroupTypes), str, true);
g = (GroupTypes)(o ?? 0);
} catch {
}
return g;
}
// then use it like this
GroupTypes g1 = StrToEnum("OEM");
GroupTypes g2 = StrToEnum("bad value");
You can make it more fancy with generics for the enum type if you wish.
In VS 2015, you can use nameof
public class LogCategory
{
public static string Trace;
public static string Debug;
public static string Info;
public static string Warning;
public static string Error;
}
Usage:
Logger.Write("This is almost like an enum.", nameof(LogCategory.Info));
I wanted to avoid using string literals completely, and also I didn't need to have space in item descriptions. More importantly, I wanted to have a mechanism to check if the provided string is a valid item, so I came up with this solution:
public class Seasons
{
public static string Spring { get; }
public static string Summer { get; }
public static string Fall { get; }
public static string Winter { get; }
public static bool IsValid(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
{
return false;
}
try
{
return typeof(Seasons).GetProperty(propertyName) != null;
}
catch
{
return false;
}
}
}
And here is how it works:
void Main()
{
string s = nameof(Seasons.Fall);
Console.WriteLine($"Fall is valid: {Seasons.IsValid(s)}"); // true
s = "WrongSeason";
Console.WriteLine($"WrongSeason is valid: {Seasons.IsValid(s)}"); // false
}
I tried to refactor IsValid() into a base class and use reflection to read the type (MethodBase.GetCurrentMethod().DeclaringType), but since I wanted to have it static, it returns the base class type, not the inherited type. Your remedy to this will be very welcomed! Here is what I was trying to achieve:
public class Seasons : ConstantStringsBase
{
// ... same
}
public class ConstantStringsBase
{
public static bool IsValid(string propertyName)
{
return MethodBase.GetCurrentMethod().DeclaringType.GetProperty(propertyName) != null;
}
}
Based in other opinions, this is what I come up with. This approach avoids having to type .Value where you want to get the constant value.
I have a base class for all string enums like this:
using System;
using Newtonsoft.Json;
[JsonConverter(typeof(ConstantConverter))]
public class StringEnum: IConvertible
{
public string Value { get; set; }
protected StringEnum(string value)
{
Value = value;
}
public static implicit operator string(StringEnum c)
{
return c.Value;
}
public string ToString(IFormatProvider provider)
{
return Value;
}
public TypeCode GetTypeCode()
{
throw new NotImplementedException();
}
public bool ToBoolean(IFormatProvider provider)
{
throw new NotImplementedException();
}
//The same for all the rest of IConvertible methods
}
The JsonConverter is like this:
using System;
using Newtonsoft.Json;
class ConstantConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
serializer.Serialize(writer, null);
}
else
{
serializer.Serialize(writer, value.ToString());
}
}
}
And an actual string enum will be something like this:
public sealed class Colors : StringEnum
{
public static Colors Red { get { return new Catalog("Red"); } }
public static Colors Yellow { get { return new Catalog("Yellow"); } }
public static Colors White { get { return new Catalog("White"); } }
private Colors(string value) : base(value) { }
}
And with this, you can just use Color.Red to even serialize to json without using the Value property
I even implemented a few enums as suggested by #Even (via class X and public static X members), just to find out later that these days, starting .Net 4.5, there's the right ToString() method.
Now I'm reimplementing everything back to enums.
You can use two enums. One for the database and the other for readability.
You just need to make sure they stay in sync, which seems like a small cost.
You don't have to set the values, just set the positions the same, but setting the values makes it very clear the two enums are related and prevents errors from rearranging the enum members. And a comment lets the maintenance crew know these are related and must be kept in sync.
// keep in sync with GroupTypes
public enum GroupTypeCodes
{
OEM,
CMB
}
// keep in sync with GroupTypesCodes
public enum GroupTypes
{
TheGroup = GroupTypeCodes.OEM,
TheOtherGroup = GroupTypeCodes.CMB
}
To use it you just convert to the code first:
GroupTypes myGroupType = GroupTypes.TheGroup;
string valueToSaveIntoDatabase = ((GroupTypeCodes)myGroupType).ToString();
Then if you want to make it even more convenient you can add an extension function that only works for this type of enum:
public static string ToString(this GroupTypes source)
{
return ((GroupTypeCodes)source).ToString();
}
and you can then just do:
GroupTypes myGroupType = GroupTypes.TheGroup;
string valueToSaveIntoDatabase = myGroupType.ToString();

Categories