Skip to content

Instantly share code, notes, and snippets.

@geersch
Created Feb 25, 2014
Embed
What would you like to do?
Instead of enums...
public class Error
{
public static readonly Error None = new Error("none");
public static readonly Error InternalError = new Error("E1");
// ...
protected Error(string errorCode)
{
Code = errorCode;
}
public string Code { get; private set; }
public static explicit operator Error(string errorCode)
{
return new Error(errorCode);
}
public override int GetHashCode()
{
return Code.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
var other = obj as Error;
if (other == null)
return false;
return Code.Equals(other.Code);
}
public static bool operator ==(Error a, Error b)
{
// If both are null, or both are same instance, return true.
if (ReferenceEquals(a, b))
return true;
// If one is null, but not both, return false.
if (((object)a == null) || ((object)b == null))
return false;
return a.Equals(b);
}
public static bool operator !=(Error a, Error b)
{
return !(a == b);
}
}
var error = (Error)"E4";
var otherError = Error.NoCellularNumber;
if (error != otherError)
{
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment