تبلیغات
مرجع عملی و کاربردی زبان سی شارپ ، تصاویر دیدنی ، دانلود نرم افزار های گوناگون - کار کردن با انواع داده ها

کار کردن با انواع داده ها

نویسنده :مرتضی عالمین
تاریخ:شنبه 16 خرداد 1388-03:16 ب.ظ

2-Enums

بدست آوردن مقدار یک Enum از رشته متنی متناظر با آن


کد:

 public static T StringToEnum<T>(string name)
{
    return (T)Enum.Parse(typeof(T), name);
}

مثال - با فرض داشتن :

کد:

public enum DaysOfWeek
{
   Monday,
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday,
   Sunday
}

میتوانیم بنویسیم :

کد:

 DaysOfWeek d = StringToEnum<DaysOfWeek>("Monday");
//d is now DaysOfWeek.Monday
if(Enum.IsDefined(typeof(DaysOfWeek), "Katillsday"))
    StringToEnum<DaysOfWeek>("Katillsday");

منبع : http://blog.paranoidferret.com/index...from-a-string/

بدست آوردن مقدار یک Enum از عدد متناظر آن


کد:

 public T NumToEnum<T>(int number)

{

   return (T)Enum.ToObject(typeof(T), number);

}

مثال - با فرض داشتن :

کد:

public enum DaysOfWeek

{

   Monday,

   Tuesday,

   Wednesday,

   Thursday,

   Friday,

   Saturday,

   Sunday

}

میتوانیم بنویسیم :

کد:

int day = 3;

DaysOfWeek d  = NumToEnum<DaysOfWeek>(day);

//d is now DaysOfWeek.Thursday

تبدیل یک Enum به یک DataTable


با فرض داشتن یک enum مانند :

کد:

public enum UserNames
{
    Sinpin,
    Masoud,
    Behrouz,
    Omid,
    Ali
}

کد:

public DataTable ConvertEnumToDataTable()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Name");
    int i = 0;
    foreach (string s in Enum.GetNames(typeof(UserNames)))
    {
        DataRow dr = dt.NewRow();
        dr["ID"] = i;
        dr["Name"] = s;
        dt.Rows.Add(dr);
        i++;
    }
    return dt;
}

مثال از نحوه ی استفاده :

کد:

dataGridView1.DataSource = ConvertEnumToDataTable();

منبع : http://www.daniweb.com/code/snippet811.html

تبدیل نوع enum به int


کد:

public  enum  Days { Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri };

int x = (int) Days.Mon;

 




داغ کن - کلوب دات کام
نظرات() 
shoe lifts for women uneven legs
جمعه 28 اردیبهشت 1397 10:31 ب.ظ
Informative article, just what I was looking for.
Robert's BJ boy
شنبه 18 فروردین 1397 09:08 ب.ظ
Hydro power is an established technology which have been used
for somewhat over a century.
http://garciniacambogiapremium.net
شنبه 18 فروردین 1397 02:19 ق.ظ
Have actually been taking little over a month.
Robert's BJ boy
شنبه 18 فروردین 1397 02:01 ق.ظ
Above you would imagine. Who you attract, and how you might be
treated, has a lot to do with how you feel about your self.
http://garciniacambogiapremium.net/myths-that-are-sabotaging-your-weight-loss-efforts
شنبه 18 فروردین 1397 02:00 ق.ظ
Have actually been taking little over a month.
foot problems
شنبه 25 شهریور 1396 07:47 ب.ظ
Do you mind if I quote a couple of your articles as long
as I provide credit and sources back to your blog?
My blog is in the very same area of interest as yours and my users would definitely benefit from a
lot of the information you provide here. Please let me know if this okay with you.
Thanks a lot!
How do I stretch my Achilles tendon?
شنبه 14 مرداد 1396 09:11 ق.ظ
I really like your blog.. very nice colors & theme.
Did you create this website yourself or did you hire someone to
do it for you? Plz respond as I'm looking to create my own blog and would like to know where u got
this from. thanks
http://hollyfavero.weebly.com
جمعه 23 تیر 1396 04:19 ق.ظ
Hi there! This is my first comment here so I just wanted
to give a quick shout out and say I really enjoy reading through your blog posts.
Can you recommend any other blogs/websites/forums that go over the same topics?
Many thanks!
BHW
دوشنبه 28 فروردین 1396 04:02 ق.ظ
Hi! I just want to give you a huge thumbs up for your excellent info you've
got here on this post. I am returning to your
website for more soon.
manicure
سه شنبه 22 فروردین 1396 02:55 ق.ظ
My family all the time say that I am killing my time here at net, except I know I am getting experience every
day by reading thes pleasant content.
 
لبخندناراحتچشمک
نیشخندبغلسوال
قلبخجالتزبان
ماچتعجبعصبانی
عینکشیطانگریه
خندهقهقههخداحافظ
سبزقهرهورا
دستگلتفکر




Admin Logo
themebox Logo