C#中的枚举问题 [英] problem of enum in c#

查看:88
本文介绍了C#中的枚举问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友
我有一个枚举

Hello friends
i have a enum

private enum enumIC
        {
            eSelect,
            eID,
            eEdit,
            eCETSH,
            eDesc,
            eTGoods,
            eSchedule,
            eUOM,
            eRODA,
            eRODS,
            eDel
        }


iam在验证中使用枚举数组


iam using array of enum in validation

object[] obj = new object[] { (int)enumIC.eCETSH , (int)enumIC.eDesc, (int)enumIC.eTGoods, (int)enumIC.eUOM, (int)enumIC.eRODA, (int)enumIC.eRODS};
string st = GetobjArrayToString(obj);

private string GetobjArrayToString(object[] obj)
        {
            string str = "";
            for (int i = 0; i < obj.Length; i++)
            {
                str = Convert.ToString(obj[i]) + ",";
            }
            return str;
        }



我必须使用此方法将enum(value)的数组更改为string和iam,是否有任何更好的方法做到这一点

[edit]已添加代码块-OriginalGriff [/edit]



i have to change this array of enum(value) to string and iam using this method is there any better way of doing this

[edit]Code block added - OriginalGriff[/edit]

推荐答案

这可能对您想做的事情有些矫kill过正,但是...
枚举元素的人类可读字符串 [
This may be a bit overkill for what you want to do, but...
Human readable strings for enum elements[^]

BTW: Why are you declaring it as an object array? Why not as an array of enumIC instead?


because this is my function
public static void ValidateGridFields(string ColumnList, int iRow, DataGridView Grid)
        {
            string[] sColumn = ColumnList.Split(',');
            for (int i = 0; i < sColumn.Length; i++)
            { 
                int CNo = Convert.ToInt32(sColumn[i]);
                if (Grid[CNo, iRow].Value + "" == "")
                    Grid.Rows[iRow].Cells[CNo].Style.BackColor = Color.LightBlue;
                else
                {
                    if (Grid.Rows[iRow].Cells[CNo].Style.BackColor == Color.LightBlue)
                        Grid.Rows[iRow].Cells[CNo].Style.BackColor = Color.White;
                }
            }
        }



用于评估网格
我想发送这样的参数




for valadating grid
and i want to send argument like this


ValidateGridFields("1,2,3,4",1,DataGridView1



第一个参数是我们的价值



啊!那么,您想要的是一个枚举值数组,并返回一串数字值?
我将使用枚举值的数组:



first argument is our value



Ah! So what you want is to be handed an array of enumIC values, and return a string of the numerical values?
I would use an array of the enum values:

private enum enumIC
    {
    eSelect,
    eID,
    eEdit,
    eCETSH,
    eDesc,
    eTGoods,
    eSchedule,
    eUOM,
    eRODA,
    eRODS,
    eDel
    }
enumIC[] obj = new enumIC[] { enumIC.eCETSH, enumIC.eDesc, enumIC.eTGoods, enumIC.eUOM, enumIC.eRODA, enumIC.eRODS };

    ...
    Console.WriteLine(GetStringFromEnumArray(obj));
    ...

private static string GetStringFromEnumArray(enumIC[] ar)
    {
    StringBuilder sb = new StringBuilder();
    string sep = "";
    foreach(enumIC e in ar)
        {
        sb.AppendFormat("{0}{1}", sep, (int)e);
        sep = ",";
        }
    return sb.ToString();
    }


有更好的方法.
尝试Enum.GetNames()方法.

示例
There is a better way.
Try the Enum.GetNames() method.

Example
foreach(string s in Enum.GetNames(typeof(enumIC)))
            Console.WriteLine(s);



此处详细了解此内容 [



Read more about this here[^].


这篇关于C#中的枚举问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆