我有一个我想尝试使用的结构,如枚举:
public struct SQLDS_statementTypes
{
public static string Select = "Select",
Update = "Update", Insert = "Insert", Delete = "Delete";
}
但它抛出一个错误:“运算符'=='不能应用于此语句中'SQLDS_statementTypes'和'string'”类型的操作数:
if (statement == SQLDS_statementTypes.Update)
有没有解决这个问题?
答案 0 :(得分:3)
答案 1 :(得分:2)
有人正在寻找一些看起来或多或少你正在寻找的东西(我不能找到链接)并且我当时写了这个。您可能希望将类名更改为更符合您的需要。我希望添加/删除值的配置很简单,如果没有我可以详细说明。
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;
}
}
答案 2 :(得分:1)
你做不到。如果你想进行这样的字符串比较,为什么不在公共成员中使用静态类呢?
static class StatementTypes
{
public static Select
{
get { return "Select"; }
}
}
然后你可以在比较中使用StatementTypes.Select。
答案 3 :(得分:0)
错误是正确的,我认为(来自错误)statement
的类型为SQLDS_statementTypes,但SQLDS_statementTypes.Update的类型为string
,因此您尝试将结构与a进行比较字符串,默认情况下对C#没有意义。
制作statement
类型的string
,如果你真的想要这样做,它会编译,但我不确定为什么你不会只使用常规的enum
第一名。
您是否想要获得枚举值的string
值? C#默认情况下已经这样做了:
public enum Coordinates
{
Cartesian,
Polar
}
...
// x will contain the string "Cartesian"
var x = Coordinates.Cartesian.ToString();