пятница, 21 ноября 2008 г.

Walking through Enum entries in C#

Sometimes the need appears in make foreach() construction for Enum entries. Here I'll show how to do it.

Type theType = typeof(MyEnum);
MemberInfo[] theMembers = theType.GetMembers();

foreach (MemberInfo entry in theMembers)
if ((entry.MemberType.ToString() == "Field"))
if((entry.Name != "value__"))
MyEnum theTyp = (MyEnum)Enum.Parse(typeof(MyEnum), entry.Name);

As earlier I'm trying to present finished solutions ready to use in your own projects. :)

пятница, 7 ноября 2008 г.

C# numeric format string with fixed length

Do you want to get from 'int' value 'string' representation with fixed length?
Here is solution:) Example:

int iVal = 53;
string strVal = iVal.ToString("0000");