[英]In C#, how do I use struct like an enum?
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: 但它抛出一个错误: “运算符'=='不能应用于此语句中'SQLDS_statementTypes'和'string'类型的操作数 :
if (statement == SQLDS_statementTypes.Update)
Is there anyway to solve this? 反正有没有解决这个问题?
why don't you use regular enum? 为什么不使用常规枚举? http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
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;
}
}
You can't. 你不能。 If you want to do string comparisons like this why not use a static class with public members?
如果你想进行这样的字符串比较,为什么不使用公共成员的静态类呢?
static class StatementTypes
{
public static Select
{
get { return "Select"; }
}
}
Then you could use StatementTypes.Select in a comparison. 然后你可以在比较中使用StatementTypes.Select。
The error is correct, I take it (from the error) that statement
is of type SQLDS_statementTypes, but SQLDS_statementTypes.Update is of type string
, so you are attempting to compare the struct to a string, which makes no sense to C# by default. 错误是正确的,我认为(来自错误)该
statement
的类型为SQLDS_statementTypes,但SQLDS_statementTypes.Update的类型为string
,因此您尝试将结构与字符串进行比较,默认情况下对C#没有意义。
Make statement
of type string
and it will compile if you really want to do this, though I'm not sure why you wouldn't just use a regular enum
in the first place. 创建类型
string
statement
,如果你真的想这样做它会编译,虽然我不知道你为什么不首先使用常规enum
。
Are you trying to get string
values of your enums? 您是否尝试获取枚举的
string
值? C# already does this by default: C#默认情况下已经这样做了:
public enum Coordinates
{
Cartesian,
Polar
}
...
// x will contain the string "Cartesian"
var x = Coordinates.Cartesian.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.