Search This Blog

Friday, December 24, 2010

C# ထဲက #derivative တွေအကြောင်း

#derivative ကို preprocessor derivative လို့လည်း ခေါ်ကြပါတယ်။ Compiler က ပရိုဂရမ်တစ်ခုကို Compile မလုပ်ခင်မှာ # နဲ့စတဲ့ derivative တွေကို အရင်ရှာပါတယ်။ အဲဒီ # တွေဟာ တချို့နေရာတွေမှာ အသုံးဝင်တာကို တွေ့ရပါတယ်။ အောက်မှာ ဖော်ပြထားတာကတော့ ကျွန်တော်လေ့လာမိသလောက်ပါ။

#warning

ကျွန်တော် မကြာခဏသုံးလေ့ရှိတဲ့ # တစ်ခုပါ။ အထူးသဖြင့် ကျွန်တော် ပရိုဂရမ်ထဲမှာ Test Data တွေကို ထည့်သုံးချင်ရင် ရေးလေ့ရှိပါတယ်။

#warning "Remove the following line after you had finished the testing"
MyData = 123;


အဲဒီလိုရေးထားတဲ့ အတွက် Compile လုပ်တဲ့အခါမှာ warning "Remove the following line after you had finished the testing" ဆိုတဲ့ စာကြောင်းက warning အနေနဲ့ ပေါ်ပါတယ်။ Test Data ကို ပရိုဂရမ်ထဲမှာ မေ့ပြီး ချန်မထားမိအောင် ကိုယ့်ဘာသာကိုယ် သတိပေးတဲ့သဘောမျိုးပါ။

#define #if #elif #else #endif

#define ဆိုတဲ့ Keyword ကို C Programming သင်ဖူးတဲ့သူတိုင်း သိကြမှာပါ။
#define PI 3.14159 ဆိုတာမျိုးတွေ ကျွန်တော်တို့ Constant ကြေငြာဖို့ သုံးခဲ့ဖူးပါတယ်။
C# မှာတော့ Constant ကြေငြာဖို့အတွက် const နဲ့ readonly စတဲ့ Keyword တွေကို သုံးပြီး ကြေငြာနိုင်ပါတယ်။

ဒါပေမယ့် တစ်ချို့နေရာတွေမှာတော့ #define ကို သုံးဖို့လိုအပ်ပြီး #if #elif #endif စတာတွေနဲ့ တွဲသုံးလို့ရပါတယ်။

ဥပမာအနေနဲ့ ပြောရရင် ကျွန်တော့်မှာ Class 3 ခုရှိတယ်ဆိုပါစို့။ နောက်ကျွန်တော့် ပရိုဂရမ်ကို Compile လုပ်တဲ့အခါမှာ Configuration1 အတွက် ClassA ကိုသုံးပြီး Compile လုပ်ချင်ပါတယ်။
Configuration2 အတွက်တော့ ClassB ကိုသုံးပြီး Compile လုပ်ချင်ပါတယ်။
ပုံမှန် Configuration အတွက်တော့ ClassC ကို သုံးပြီး Compile လုပ်ချင်ပါတယ်။
အဲဒီလို အချိန်မျိုးမှာ #define ကို #if #elif #else #endif နဲ့ တွဲပြီး အခုလိုသုံးလို့ရပါတယ်။

#define Configuration1

#if Configuration1
ClassA MyClass = new ClassA();
#elif Configuration 2
ClassB MyClass = new ClassB();
#else
ClassC MyClass = new ClassC()
#endif


အဲဒီလိုရေးရင် ClassA နဲ့ Compile လုပ်ပါလိမ့်မယ်။ Configuration1 အစား Configuration2 ကို define လုပ်ထားရင်တော့ ClassB နဲ့ Compile လုပ်ပါလိမ့်မယ်။ ဘာကို မှ define လုပ်မထားရင်တော့ ClassC နဲ့ Compile လုပ်ပါလိမ့်မယ်။

#define မကြေငြာပဲ Configuration1 ကို Project Properties ထဲမှာ Command Line Argument အနေနဲ့ ထည့်လို့လည်းရပါတယ်။

နောက်တစ်ခုသုံးနိုင်တာက ပရိုဂရမ်ကို Debug လုပ်တဲ့အချိန်မှာ တစ်ချို့ Function တွေကို Skip လုပ်ချင်တယ်ဆိုရင် အခုလိုရေးပေးလို့ ရပါတယ်။

void thisFunctionSkipAtDebug()
{
#if DEBUG
return;
#else
//your function code here
#endif
}


ပရိုဂရမ်က DEBUG လုပ်နေတဲ့ အချိန်မှာ DEBUG ကို define လုပ်ပြီးသား ဖြစ်တဲ့အတွက် သတ်သတ်ကြေငြာပေးစရာ မလိုပါဘူး။

#error


ပထမဥပမာနဲ့ပတ်သက်ပြီး မေးစရာတစ်ခုရှိလာပါတယ်။ အကယ်၍ ပရိုဂရမ်မှာ တစ်ယောက်ယောက်က Configuration1 ရော Configuration2 ပါ define လုပ်သွားရင် ဘယ်နှယ့်လုပ်မလဲ ဆိုတဲ့ မေးခွန်းပါ။ အဲဒီ Condition ကကျွန်တော်တို့အတွက် မလိုလားအပ်တဲ့ Condition မျိုးပါ။ အဲဒီလိုအခြေအနေ မှာ ပရိုဂရမ်ထဲမှာ အခုလို #error နဲ့ ရေးပြီး error message နဲ့ ပရိုဂရမ်ကို Compilation Fail ဖြစ်အောင် တားထားလို့ရပါတယ်။

#if Configuration1 && Configuration2
#error "Both Configuration1 and Configuration2 defined in the program."
#endif

#region #endregion


ဒီအသုံးအနှုန်းတွေကိုတော့ ကုတ်ဒ်ကို ဖတ်ရလွယ်အောင်လို့ တစ်ခါတစ်လေ သုံးတတ်ပါတယ်။
ဥပမာ ကျွန်တော့် ပရိုဂရမ်ထဲမှာ UI Enhancement လုပ်ထားတဲ့ Cosmetic Code တစ်ချို့ပါတယ် ဆိုပါစို့။ ကျွန်တော် အဲဒီကုတ်ဒ်တွေကို DEBUG လုပ်နေတုန်းမှာ မျက်စိရှုပ်ခံပြီး မကြည့်ချင်ရင် #region #endregion နဲ့ အခုလိုရေးထားပြီး Collapse လုပ်ပြီး ပိတ်ထားလို့ရပါတယ်။

#region My Cosmetic Code
//100 lines of cosmetic code here
#endregion


#region #endregion ဟာ Compile လုပ်တဲ့အချိန်မှာ ဘာမှ မပြောင်းလဲပါဘူး။ ကုတ်ဒ်ကို ဖတ်ရလွယ်အောင်လို့ သက်သက်သုံးတာပါ။

ဒီပို့စ်လည်း MyanmarITPro မှာ တင်ခဲ့ဖူးပါတယ်။

3 comments:

Aungnyc said...

ေက်းဇူးတင္တယ္.. Unicode ေဖာင့္ ကုိ ေဒါင္လုပ္ သြားတယ္. အခက္အခဲရွိရင္ ေၿပာမယ္ဗ်ာ..

Aungnyc said...

unable to read my post comment as you see above.

ZT said...

I change the comment box to pop-up style. You will be able to read it if you click on "Post Comment" or "3 comments" etc. But you won't be able to see it if you click on the post title. Thank you for your support. Let me know if you still have difficulties. :)