[英]In C#, how do I use struct like an enum?
我有一個結構,我試圖像枚舉一樣使用:
public struct SQLDS_statementTypes
{
public static string Select = "Select",
Update = "Update", Insert = "Insert", Delete = "Delete";
}
但它拋出一個錯誤: “運算符'=='不能應用於此語句中'SQLDS_statementTypes'和'string'類型的操作數 :
if (statement == SQLDS_statementTypes.Update)
反正有沒有解決這個問題?
有人在尋找一些看起來或多或少你正在尋找的東西(我找不到鏈接的麻煩)我當時就寫了這個。 您可能希望將類名更改為更符合您的需要。 我希望添加/刪除值的配置很簡單,如果沒有我可以詳細說明。
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;
}
}
你不能。 如果你想進行這樣的字符串比較,為什么不使用公共成員的靜態類呢?
static class StatementTypes
{
public static Select
{
get { return "Select"; }
}
}
然后你可以在比較中使用StatementTypes.Select。
錯誤是正確的,我認為(來自錯誤)該statement
的類型為SQLDS_statementTypes,但SQLDS_statementTypes.Update的類型為string
,因此您嘗試將結構與字符串進行比較,默認情況下對C#沒有意義。
創建類型string
statement
,如果你真的想這樣做它會編譯,雖然我不知道你為什么不首先使用常規enum
。
您是否嘗試獲取枚舉的string
值? 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.