Click here to Skip to main content
14,734,150 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends
i have a enum

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

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;
        }


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]
Posted
Updated 11-Dec-11 23:50pm
v2

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();
    }
   
v2
Comments
srilekhamenon 12-Dec-11 4:59am
   
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
OriginalGriff 12-Dec-11 5:18am
   
Answer updated
srilekhamenon 12-Dec-11 5:33am
   
Thankx Griff ;)
OriginalGriff 12-Dec-11 5:45am
   
You're welcome!
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[^].
   
Comments
srilekhamenon 12-Dec-11 5:17am
   
thankx Abhinav
Actually these enums represent the column of Grid and i do not want all the columns
Abhinav S 12-Dec-11 7:51am
   
You can use GetName() in that case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900