3

我有一个我想像枚举一样使用的结构:

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

但它会抛出一个错误: “运算符'=='不能应用于'SQLDS_statementTypes'和'string'类型的操作数”在此语句中:

if (statement == SQLDS_statementTypes.Update)

有没有办法解决这个问题?

4

4 回答 4

5

有人正在寻找一些看起来或多或少的东西你正在寻找一段时间(我无法找到链接),我当时写了这个。您可能希望更改类名以更符合您的要求。我希望添加/删除值的配置简单明了,如果不是我可以详细说明。

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;
    }
}
于 2012-05-22T19:16:06.020 回答
3

你不能。如果你想做这样的字符串比较,为什么不使用带有公共成员的静态类呢?

static class StatementTypes
{
    public static Select
    {
        get { return "Select"; }
    }
}

然后你可以在比较中使用 StatementTypes.Select 。

于 2012-05-22T19:15:17.723 回答
2

你为什么不使用常规枚举?http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

于 2012-05-22T19:12:27.037 回答
0

错误是正确的,我认为它(从错误中)statement是 SQLDS_statementTypes 类型,但 SQLDS_statementTypes.Update 是 type string,因此您试图将结构与字符串进行比较,默认情况下这对 C# 没有意义。

Make statementof typestring如果你真的想这样做,它会编译,但我不确定你为什么不enum首先使用常规。

您是否正在尝试获取string枚举的值?默认情况下,C# 已经这样做了:

public enum Coordinates
{
    Cartesian,
    Polar
}

...

// x will contain the string "Cartesian"
var x = Coordinates.Cartesian.ToString();
于 2012-05-22T19:13:03.330 回答