Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, how do I use struct like an enum?

Tags:

c#

I have a struct that I am trying to use like an enum:

public struct SQLDS_statementTypes
{
    public static string Select = "Select", 
        Update = "Update", Insert = "Insert", Delete = "Delete";
}

But it throw an error: "Operator '==' cannot be applied to operands of type 'SQLDS_statementTypes' and 'string'" on this statement:

if (statement == SQLDS_statementTypes.Update)

Is there anyway to solve this?

like image 451
Bill Software Engineer Avatar asked Nov 08 '25 14:11

Bill Software Engineer


2 Answers

Someone was looking for something that seems more or less what you're looking for a while back (I can't be bothered to find a link) and I wrote this at the time. You may want to change the class name to be more inline with what you want. I hope that the configurations for adding/removing values are straightforward, if not I can elaborate.

public struct Group
{
    #region Code that is to be configured
    public static readonly Group Alpha = new Group("Group Alpha");
    public static readonly Group Beta = new Group("Group Beta");
    public static readonly Group Invalid = new Group("N/A");


    public static IEnumerable<Group> AllGroups
    {
        get
        {
            yield return Alpha;
            yield return Beta;
            yield return Invalid;
            //...
            //add a yield return for all instances here.
        }
    }

    #endregion
    private string value;

    /// <summary>
    /// default constructor
    /// </summary>
    //private Group()
    //{
    //    //you can make this default value whatever you want.  null is another option I considered, but you 
    //    //shouldn't have this me anything that doesn't exist as one of the options defined at the top of 
    //    //the page.
    //    value = "N/A";
    //}
    /// <summary>
    /// primary constructor
    /// </summary>
    /// <param name="value">The string value that this is a wrapper for</param>
    private Group(string value)
    {
        this.value = value;
    }

    /// <summary>
    /// Compares the Group to another group, or to a string value.
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public override bool Equals(object obj)
    {
        if (obj is Group)
        {
            return this.value.Equals(((Group)obj).value);
        }

        string otherString = obj as string;
        if (otherString != null)
        {
            return this.value.Equals(otherString);
        }

        throw new ArgumentException("obj is neither a Group nor a String");
    }

    public override int GetHashCode()
    {
        return value.GetHashCode();
    }

    /// <summary>
    /// returns the internal string that this is a wrapper for.
    /// </summary>
    /// <param name="group"></param>
    /// <returns></returns>
    public static implicit operator string(Group group)
    {
        return group.value;
    }

    /// <summary>
    /// Parses a string and returns an instance that corresponds to it.
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Group Parse(string input)
    {
        return AllGroups.Where(item => item.value == input).FirstOrDefault();
    }

    /// <summary>
    /// Syntatic sugar for the Parse method.
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    public static explicit operator Group(string other)
    {
        return Parse(other);
    }

    public override string ToString()
    {
        return value;
    }
}
like image 85
Servy Avatar answered Nov 11 '25 14:11

Servy


why don't you use regular enum? http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

like image 30
Adam Avatar answered Nov 11 '25 13:11

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!