our notes for our youngers

October 2, 2009

basic of C# (part 25)

Filed under: Basic of C# — Tags: , , , , , — sevenlamp @ 10:21 AM

Function Overloading Vs Function Overriding

ကြၽန္ေတာ္တို႔ ဒီေန႔ function overloading နဲ႔ function overriding အေၾကာင္းေလး ေျပာၾကရေအာင္။ အရင္ဆံုး function overloading အေၾကာင္းေျပာရေအာင္။ overload ဆိုတာ အလုပ္တစ္ခုထက္ပိုျပီး လုပ္တာကို ေျပာခ်င္တာပါ။ function overloading ဆိုေတာ့ ကြၽန္ေတာ္တို႔ ေရးလိုက္တဲ႔ function က တာ၀န္ တစ္ခုထက္ပိုျပီး လုပ္ေဆာင္ေပးတာကိုရည္ရြယ္တာပါ။ function name က တစ္ခုထဲပဲ ဒါေပမယ့္ လုပ္ေဆာင္မႈအမ်ားၾကီး လုပ္နိုင္ေအာင္ေပါ့။ ဥပမာဗ်ာ.. ကြၽန္ေတာ္က string ကေန integer ေျပာင္းတဲ႔ function ေလးတစ္ခုေလာက္ ေရးခ်င္တယ္။

static int ConvertToInteger(string st)
{
    try
    {
        return Convert.ToInt32(st);
    }
    catch
    {
        return 0;
    }
}

ကဲ.. ဒီ function ေလး ေရးလိုက္ျခင္းအားျဖင့္ ကြၽန္ေတာ္တို႔ string ကေန integer ေျပာင္းခ်င္တိုင္း လြယ္လြယ္ကူကူ ေခၚလို႔ ရသြားမယ္။ ဒါေပမယ့္ တျခား type ေတြကေနေျပာင္းခ်င္ရင္ ဒီ function သံုးလို႔ ရမလား။ မရပါဘူး။ ဒါဆို ကြၽန္ေတာ္က တျခားtype ေတြ အတြက္ကိုလည္း ဒီလိုမ်ိဳး function ေတြ ထပ္ေရးေပးဖို႔ လိုပါလိမ့္မယ္။

static int ConvertToInteger(decimal dec)
{
    try
    {
        return Convert.ToInt32(dec);
    }
    catch
    {
        return 0;
    }
}
static int ConvertToInteger(long l)
{
    try
    {
        return Convert.ToInt32(l);
    }
    catch
    {
        return 0;
    }
}

ဟုတ္ျပီေနာ္.. အခုလိုမ်ိဳး function name တူျပီး parameter မတူတဲ႔ function ေတြ တစ္ခုထက္ပိုျပီး ေရးလိုက္တာကို function overloading လုပ္တယ္လို႔ ေခၚပါတယ္။ function overloading လုပ္ျခင္း အားျဖင့္ ဘာပိုေကာင္းလာမလဲ။ ဟုတ္ကဲ႔ ကြၽန္ေတာ္တို႔ program ေရးတဲ႔ေနရာမွာ အမ်ားၾကီးပိုျပီး လြယ္ကူေစပါတယ္။ အကယ္လို႔သာ function overload မလုပ္ရဘူးဆိုရင္ ကြၽန္ေတာ္တို႔ string ကေန integer ေျပာင္းဖို႔အတြက္က function name တစ္ခု၊ decimal ကေန integer ေျပာင္းဖို႔အတြက္က function name တစ္ခု၊ long ကေန integer ေျပာင္းဖို႔အတြက္က function name တစ္ခု စသည္ျဖင့္ name ေတြ အမ်ားၾကီး သံုးေနမွတ္ေနရမွာေပါ့။ အခု overloading လုပ္ျခင္းအားျဖင့္ function name တစ္ခုထဲကိုပဲ သိဖို႔ လိုပါေတာ့တယ္။ Framework class library ထဲမွာလည္း overload လုပ္ထားတဲ႔ function ေတြ အမ်ားၾကီး ေတြရမွာပါ။ ကဲ အႏွစ္ခ်ဳပ္ျပန္ေျပာပါမယ္ function overloading ျဖစ္ဖို႔အတြက္ function name တူရပါမယ္၊ argument (parameter) မတူရပါဘူး။ parameter မတူရဘူး ဆိုတာမွာ parameter ရဲ႕ datatype ျဖစ္ေစ၊ အရည္အတြက္ ျဖစ္ေစမတူရင္ ရပါတယ္။

ေနာက္တစ္ခုကေတာ့ function overriding။ overriding လို႔ ေျပာတဲ႔ အတိုင္းပါပဲ၊ function overrideing ဆိုတာက function တစ္ခုေပၚမွာ ေနာက္ထပ္ function တစ္ခုနဲ႔ ထပ္ျပီး အုပ္လိုက္တာကို ေျပာခ်င္တာပါ။ ဒီလိုလုပ္လိုက္ရင္ function ၂ ခု ျဖစ္သြားမလား၊ မျဖစ္ပါဘူး။ override လုပ္ျခင္းခံရတဲ႔ function က ေပ်ာက္သြားမွာပါ။ function overrideing ကို အဓိကအားျဖင့္ polymorphism နဲ႔ တြဲျပီးသံုးေလ့ ရွိပါတယ္။ parent class ထဲမွာ ရွိျပီးသား function တစ္ခုကို child class ထဲမွာျပန္ျပီးေရးခ်င္တယ္ ဆိုရင္ override လုပ္ဖို႔ လိုလာပါျပီ။ ဒီလို အခ်ိန္မ်ိဳးမွာ function overriding ကိုသံုးပါတယ္။ ဥပမာေလးနဲ႔ ေျပာရရင္… ကြၽန္ေတာ္မွာ animal ဆိုတဲ႔ class ရွိတယ္ဗ်ာ၊ parent class ေပါ့၊ အဲ့ဒီ animal class ထဲမွာ eat() ဆိုတဲ႔ function ေလးပါတယ္။

class animal
{
    public void eat()
    {
        Console.WriteLine("Eat something");
    }
}

အိုေက.. အဲ့ဒီ animal class မွာ child class ၂ ခုရွိတယ္။ dog class နဲ႔ ant class ေပါ့။ အဲ့ဒီ dog class နဲ႔ ant class က animal class ကေန inheritance လုပ္ထားတာျဖစ္တဲ႔ အတြက္ သူတို႔ဆီမွာ eat() function အလိုအေလွ်ာက္ ပါလာပါတယ္။ အဲ့ဒီ parent class ကေနပါလာတဲ႔ eat() function ထဲက statement ေတြကို child class မွာ မလိုခ်င္ဘူး။ ပိုျပီး detail ၾကတဲ႔ message နဲ႔ အစားထိုးခ်င္တယ္ဆိုပါေတာ့ (ဥပမာ.. Eat something အစား၊ Eat Bone တို႔ Eat sugur တို႔နဲ႔ အစားထိုးခ်င္ျပီဆိုရင္)၊ ဒါဆို ဘယ္လိုလုပ္မလဲ? ေနာက္ထပ္ function တစ္ခုထပ္ေဆာက္ခ်င္တာ မဟုတ္ဘူးေနာ္၊ parameter လည္း မေျပာင္းတဲ႔ အတြက္ overloading လည္း လုပ္လို႔ မရဘူး။ ဒီလို အေျခအေနမ်ိဳးမွာ ကြၽန္ေတာ္တို႔ မျဖစ္မေန function overriding လုပ္ဖို႔ လိုလာပါျပီ။

ဒါဆို function overriding လုပ္ဖို႔ ဘာလိုမလဲ? Parent class ထဲမွာ ဒီတိုင္းေရးထားတဲ႔ function တိုင္းကိုေတာ့ override လုပ္လို႔ မရပါဘူး။ override လုပ္ဖို႔ ခြင့္ျပဳထားတဲ႔ function ေတြကိုပဲ လုပ္လို႔ရတာပါ။ ဒီလို override လုပ္ဖို႔ ခြင့္ျပဳခ်င္တယ္ဆိုရင္ parent class ထဲက function ရဲ႕ေရွ႕မွာ virtual ဆိုတဲ႔ keyword ေလးထည့္ေရးေပးလိုက္ရပါတယ္။

class animal
{
    public virtual void eat()
    {
        Console.WriteLine("Eat something");
    }
}

override ျပန္လုပ္မယ့္ child class က function ေရွ႕မွာေတာ့ override ဆိုတဲ႔ keywordကို ထည့္ေပးရပါတယ္။

class dog : animal
{
    public override void eat()
    {
        Console.WriteLine("Eat Bone!");
    }
}

ကဲ… example program ေလး တစ္ခုေလာက္ေရးၾကည့္ပါဦး။

class animal
{
    public virtual void eat()
    {
        Console.WriteLine("Animal: Eat something");
    }
}
class dog : animal
{
    public override void eat() // function overriding
    {
        Console.WriteLine("Dog: Eat Bone!");
    }
}
class ant : animal
{
    public override void eat()
    {
        Console.WriteLine("Ant: Eat Sugar");
    }
}
class Program
{
    static void Main()
    {
        animal obj;
        obj = new animal();
        obj.eat();

        obj = new dog(); //polymorphism
        obj.eat();

        obj = new ant();
        obj.eat();

        Console.Read();
    }
}

Basic of C# ကို ကြၽန္ေတာ္ဒီမွာပဲ ရပ္လိုက္ပါေတာ့မယ္။ အခုေလာက္ဆိုရင္ ကြၽန္ေတာ္တို႔ C# ဆိုတာ ဘာလဲသိေလာက္ပါျပီ။ OOP အေၾကာင္းလည္း နည္းနည္းေတာ့ နားလည္ေလာက္ျပီလို႔ ယူဆပါတယ္။ ေနာက္ပိုဒ႔္ေတြမွာ window application အပိုင္းနဲ႔ web application အပိုင္းကို ဆက္ေရးသြားပါ့မယ္ခင္ဗ်ာ။

September 29, 2009

basic of C# (part 24)

Filed under: Basic of C# — Tags: , , , , , , , — sevenlamp @ 9:55 AM

ကဲ ဒီေန႔ေတာ့ database အပိုင္းေလး ခဏနားျပီး C# ဘက္ကို နည္းနည္းျပန္လွည့္ေအာင္။ ကြၽန္ေတာ္တို႔ အခုေနာက္ပိုင္း programming language မွာ အသံက်ယ္က်ယ္ေလာင္ေလာင္ ျဖစ္လာတာက OOT လို႔ ေခၚတဲ႔ Object-Oriented Technique ပဲျဖစ္ပါတယ္။ OO မွာ ဘာေတြပါလဲ…

အေမြဆက္ခံျခင္းလို႔ ေခၚတဲ႔ inheritance၊

အသြင္ေျပာင္းျခင္းလို႔ေျပာလို႔ရတဲ႔ Polymorphism၊

Data hiding လို႔ေခၚတဲ႔ member data ေတြကို သို၀ွက္သိမ္းဆည္းထားျခင္း၊

Encapsulation သိမ္းဆည္းထားတဲ႔ member data ေတြကို တဆင့္ခံျဖင့္ သံုးခြင့္ေပးျခင္း၊

စသည္ျဖင့္ေပါ႔ဗ်ာ.. အဲ့ဒါေတြ ပါပါတယ္။

Inheritance အေမြဆက္ခံျခင္းဆိုတာဘာလဲ???

ဒီလိုေလးေတြးၾကည့္ရေအာင္ဗ်ာ အခုကြၽန္ေတာ္တို႔ မိဘေတြပိုင္ဆိုင္တဲ႔ ပိုင္ဆိုင္မႈေတြဟာ ကြၽန္ေတာ္တို႔ရဲ႕ ပိုင္ဆိုင္မႈေတြပါပဲ။ ဒီလိုပါပဲ ကြၽန္ေတာ္တို႔ရဲ႕ ပိုင္ဆိုင္မႈေတြကိုလည္း ကြၽန္ေတာ္တို႔ရဲ႕ မ်ိဳးဆက္သစ္ေတြက ပိုင္ဆိုင္ၾကဦးမွပါ။ ဒီသေဘာတရားေလးကို ယူျပီး OOမွာ inheritance ဆိုတာေပၚေပါက္လာတာပါ။ class တစ္ခုရဲ႕ ပိုင္ဆိုင္မႈ property ေတြဟာ အဲ့ဒီ class ရဲ႕ child ေတြကလည္း ပိုင္ဆိုင္နိုင္ပါတယ္။ ဒီလို ပိုင္ဆိုင္နိုင္ျခင္းအားျဖင့္ ကြၽန္ေတာ္တို႔က class ေတြအားလံုး အတြက္ တူညီတဲ႔ property ေတြကို အၾကိမ္ၾကိမ္ေၾကျငာေပးေနစရာ မလိုေတာ့ပါဘူး။ ဥပမာဗ်ာ ကြၽန္ေတာ္က လူတစ္ေယာက္ကို ကိုယ္စားျပဳမယ့္ class တစ္ခုေဆာက္မယ္ဆိုရင္ အဲ့ဒီ class ထဲမွာ ဘယ္လို member ေတြပါမလဲ စဥ္းစားၾကည့္၊ နံမည္ပါမယ္၊ အသက္ပါမယ္၊ ေနရပ္လိပ္စာပါမယ္ဗ်ာ၊ စသည္ျဖင့္ေပါ့။

class People
{
    string Name;
    int Age;
    string Address;
}

ကဲ…အခု ေနာက္တစ္ခါ ကြၽန္ေတာ္က ေက်ာင္းသားတစ္ေယာက္ကို ကိုယ္စားျပဳမယ့္ class တစ္ခုထပ္ေဆာက္ဖို႔ လိုလာျပီဆိုရင္ေရာ ဘယ္လို member ေတြပါမလဲ၊ ေက်ာင္းသားမွာလည္း နံမယ္ပါမယ္၊ အသက္ပါမယ္၊ လိပ္စာပါမွာပါပဲ၊ ေက်ာင္းသားမွာ ပိုပါလာတာက roll နံပါတ္ပါမယ္၊ class ပါပါမယ္။

class Student
{
    string Name;
    int Age;
    string Address;
    int Roll;
    int Class;
}

ဟုတ္ျပီ ဒီလိုအေျခအေနမ်ိဳးမွာ နံမည္တို႔ အသက္တို႔ကို ထပ္ျပီး မေၾကျငာခ်င္ဘူးဆိုရင္ ကြၽန္ေတာ္တို႔ Student class ကို People class ရဲ႕ child အျဖစ္ ေၾကျငာလို႔ ရပါတယ္။ ဒီလိုမ်ိဳးပါ…

class Student : People
{
    int Roll;
    int Class;
}

ဒါကို inheritance လုပ္တယ္လို႔ ေခၚပါတယ္။ inheritance လုပ္လုိက္ျခင္းအားျဖင့္ People class ထဲက member data ကို Student class ထဲမွာ ျပန္ျပီ ေၾကျငာေပးေနစရာ မလုိေတာ့ပါဘူး။ ဒါေပမယ့္ ဒီေနရာမွာ အေရးၾကီးတဲ႔ အခ်က္တစ္ခ်က္ကို သိထားဖို႔ လိုပါေသးတယ္။ ဒီလိုဗ်ာ ကြၽန္ေတာ္တို႔ မိဘရဲ႕ ပိုင္ဆိုင္မႈအားလံုး ကြၽန္ေတာ္တို႔ရမလား၊ မရပါဘူး သူတို႔ ကိုယ္ပိုင္အတြက္ သိမ္းထားတာေတြလည္း ရွိနိုင္ပါတယ္။ C# language မွာလဲ အဲ့ဒီလိုမ်ိဳး ခြဲျခားဖို႔အတြက္ ကြၽန္ေတာ္တို႔ Access Modifier ဆိုတာကို သိဖို႔လိုအပ္လာပါတယ္။ C# language မွာ access modifier ၅ မ်ိဳးရွိပါတယ္။ အဲ့ဒါေတြ ကေတာ့

-       public                            ( အမ်ားသံုး )

-       protected                       ( မိမိႏွင့္child ေတြသံုး )

-       private                          ( မိမိကိုယ္တိုင္သံုး )

-       internal                         ( library တူအတြင္းသံုး )

-       protected internal           ( child ျဖစ္လွ်င္၄င္း၊ library တူလွ်င္၄င္း သံုး )

စသည္တို႔ ျဖစ္ပါတယ္။ အေပၚက example People class ေလးမွာ ကြၽန္ေတာ္တို႔ access modifier မထည့္ေပးလိုက္ပါဘူး။ ဒါဆိုရင္ default အေနနဲ႔ private ပါ။ ဒါေၾကာင့္ People class ထဲက member data သံုးခုက people class အတြင္းမွာပဲ သံုးခြင့္ရွိပါတယ္။ Student မွာ သံုးလို႔ မရပါဘူး။ အခုကြၽန္ေတာ္တို႔က Student class ထဲကေနပါ သံုးခြင့္ရခ်င္တာျဖစ္လို႔ protected member အျဖစ္ေၾကျငာေပးရမွာပါ။

class People
{
    protected string Name;
    protected int Age;
    protected string Address;
}

Polymorphism အသြင္ေျပာင္းျခင္းဆိုတာကေရာ???

ကြၽန္ေတာ္တို႔ ရပ္ကြက္ေတြမွာ လုပ္ေလ့ရွိတယ္ဗ်ာ တခါတေလ ရပ္ကြက္ရံုး ကေနျပီးေတာ့ တစ္အိမ္ကို လူတစ္ေယာက္ႏႈန္း အစည္းအေ၀းတက္ရမယ္တို႔ ဘာတို႔ ၾကံဳဖူးမွာေပါ့။ ဟုတ္ကဲ႔ ရပ္ကြက္ရံုးက ေခၚတာက လူတစ္ေယာက္လို႔ ေခၚတာပါ တနည္းအားျဖင့္ people class ကို ေခၚတာပါ။ အဲ့ဒါကို အိမ္မွာ ရွိေနတဲ႔ ေက်ာင္းသားတစ္ေယာက္က သြားတက္လို႔ မရဘူးလား၊ ရပါတယ္။ Teacher တစ္ေယာက္သြားတက္မယ္ဆိုလို႔လဲ ရပါတယ္။ Teacher ကလည္း people တစ္ေယာက္ပဲ ျဖစ္လို႔ပါ။ ဒါေပမယ့္ Student ပဲ သြားသည္ျဖစ္ေစ၊ Teacher ပဲသြားသည္ျဖစ္ေစ၊ ဒါမွမဟုတ္ People ကိုယ္တိုင္ပဲ သြားသည္ျဖစ္ေစ၊ အားလံုးကို People လို႔ပဲ ျမင္ပါတယ္။ အားလံုး အခြင့္အေရးတန္းတူပါပဲ။ Student မို႔ အခြင့္အေရးပိုရမလား မရပါဘူး။ အိုေက အဲ့ဒီလိုမ်ိဳး People ကိုေခၚတာကို Stuent သြားလိုက္တာကို အသြင္ေျပာင္းလဲျခင္း ( polymorphism ) လို႔ ေခၚပါတယ္။

static void Main()
{
    People p;
    p = new Student();
}

Polymorphism မွာ သတိထားရမည့္ အခ်က္ကေတာ့ child ကို parent အသြင္ေျပာင္းနိုင္ေပမယ့္ parent ကိုေတာ့ child အျဖစ္ေျပာင္းလို႔ မရပါဘူး။ ဆိုလိုတာကေတာ ေက်ာင္းသားအားလံုး စာေမးပြဲလာေျဖပါဆိုျပီး ေခၚတာကို အိမ္မွာ ရွိတဲ႔ လူၾကီးေတြသြားေျဖလို႔ ရမလား မရပါဘူး၊ ဒီသေဘာပါ။ Student ကိုေတာင္းရင္ေတာ့ People ေပးလို႔ မရပါဘူး၊ Student ပဲ ေပးမွ ရမွာပါ။

Data Hiding

Data hiding ဆိုတာကေတာ့ ကြၽန္ေတာ္တို႔ အိမ္မွာ မိသားစုပိုင္ဆိုင္မႈေတြကို တျခားလူေတြသိေအာင္ ထုတ္ေၾကျငာေလ့ရွိလား? မရွိပါဘူး တိတ္တိတ္ေလးပဲ သိမ္းထားေလ့ရွိပါတယ္။ ဒီသေဘာပါပဲ class ေတြထဲမွာရွိတဲ႔ member data ေတြကိုလည္း အျပင္ကေန ေခၚလို႔မရေအာင္ private လုပ္ထားျခင္းကို data hiding လို႔ ေခၚပါတယ္။ ဥပမာဗ်ာ အခု student class ထဲမွာ roll နဲ႔ class ပါတယ္မလား၊ အဲ့ဒီ member data ေတြကို အျပင္ကေန ေခၚသံုးလို႔ မရပါဘူ။ ဘာလို႔လဲဆိုေတာ့ private member data ေတြ ျဖစ္လို႔ပါ။ ဒီလုိမ်ိဳး class member data ေတြကို private အျဖစ္ပဲ ေၾကျငာတာကို Data Hiding လို႔ေခၚပါတယ္။

Encapsulation

Encapsulation ဆိုတာကေတာ့ ကြၽန္ေတာ္တို႔ hide လုပ္ထားတဲ႔ data ထဲက လိုအပ္တဲ႔ dataေတြကို function ေတြကေနေသာ္လည္းေကာင္း၊ property ေတြကေနတဆင့္ေသာ္လည္းေကာင္း၊ အသံုးျပဳနိုင္ဖို႔အတြက္ စီမံေပးျခင္းပဲ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္တို႔ C# မွာေတာ့ member data ေတြကို ကိုယ္စားျပဳဖို႔အတြက္ property ေတြကိုသံုးေလ့ရွိပါတယ္။ ဒီလိုမ်ိဳးပါ..

class Student : People
{
    	int Roll;
    	int Class;
	public int RollNo
	{
		get
		{
			return Roll;
		}
		set
		{
			Roll = value;
		}
	}
	public int ClassID
	{
		get
		{
			return Class;
		}
		set
		{
			Class = value;
		}
	}
}

September 17, 2009

basic of C# (part 23)

Filed under: Basic of C# — Tags: , , , — sevenlamp @ 10:07 AM

ဒီေန႔ ကြၽန္ေတာ္တို႔ C# ရဲ႕ property ေတြ အေၾကာင္း ေျပာၾကရေအာင္။ အရင္ဆံုး property ေတြ ဘယ္လိုေၾကာင့္ ျဖစ္ေပၚလာလဲ အရင္စေျပာပါ့မယ္။ ဟုတ္ျပီ ကြၽန္ေတာ္တို႔ class တစ္ခုထဲက instant member data ေတြကို တျခား class ေတြကေန ေခၚသံုးမယ္ဆိုရင္ အဲ့ဒီ instant member ရဲ႕ေရွ႕မွာ public keyword ထည့္ျပီး ေၾကျငာေပးရပါတယ္။ ဒီလိုမ်ိဳးပါ…

using System;
class Test_A
{
    public int var;
}
class Program
{
    static void Main()
    {
        Test_A obj = new Test_A();
        obj.var = 1;
        Console.WriteLine(obj.var);
        Console.Read();
    }
}

ဒီ program ေလးကိုၾကည့္ၾကည့္ပါ။ Test_A class ထဲက var ဆိုတဲ႔ variable ကို program class ထဲကေနေခၚျပီး တန္ဖိုးထည့္တယ္။ ျပီးေတာ့ ျပန္ရိုက္ထုတ္တယ္။ ဒီလိုေခၚလို႔ ရဖို႔အတြက္ Test_A ထဲမွာ public ဆိုျပီး ေၾကျငာေပးရတယ္။ ဒီလို member data ေတြကို public လုပ္ျပီး အျပင္ကေန ေခၚသံုးတာက ေရးရတာ လြယ္ပါတယ္၊ ဒါေပမယ့္ Object-oriented ဆိုျပီး ေအာ္လာၾကတဲ႔ သကာလမွာ data hiding ဆိုတဲ႔ rule ေလးတစ္ခုပါပါလာပါတယ္။ အဲ့ဒီ rule ကေတာ့ member data ေတြကို အျပင္ကေန တိုက္ရိုက္ေခၚျပီး မသံုးရပါဘူး၊ hide လုပ္ထားပါတယ္။ ဒါဆိုရင္ အဲ့ဒီ member ကို အျပင္ကေနဘယ္လိုေခၚမလဲ။ ဟုတ္ကဲ႔ အဲ့ဒီလိုေခၚဖို႔အတြက္ အေစာပိုင္းကာလေတြမွာ ကြၽန္ေတာ္တို႔ member function ေတြကို သံုးရပါတယ္။ ဒီလိုမ်ိဳးပါ….

using System;
class Test_B
{
    private int var;
    public void SetData(int value)
    {
        var = value;
    }
    public int GetData()
    {
        return var;
    }
}
class Program
{
    static void Main()
    {
        Test_B obj = new Test_B();
        obj.SetData(2);
        Console.WriteLine(obj.GetData());
        Console.Read();
    }
}

ဒီ program ေလးကိုၾကည့္လိုက္ပါ။ Type_B class ထဲက member variable ကို အျပင္ကေန ေခၚသံုးလို႔မရေအာင္ private လုပ္လိုက္ပါတယ္။ ျပီးေတာ့မွ အဲ့ဒီ member data ထဲကို တန္ဖိုးထည့္ဖို႔ SetData function နဲ႔ တန္ဖိုးျပန္ထုတ္ဖို႔ GetData function ကိုေရးေပးပါတယ္။ အဲ့ဒီ function ေတြကိုေတာ့ public လုပ္ေပးရပါမယ္။ ဒီလိုေရးလိုက္ျခင္းအားျဖင့္ data hiding rule ကို လိုက္နာျပီးသားျဖစ္သြားပါတယ္။ ခုလိုမ်ိဳး function ကေနတဆင့္ တန္ဖိုးအသြင္းအထုတ္လုပ္တာ မရဘူးလားဆိုေတာ့ ရပါတယ္၊ ဒါေပမယ့္ member variable ကို ေခၚသံုးရတာေလာက္ေတာ့ မလြယ္ကူပါဘူး။ အနည္းဆံုးေတာ့ member data တစ္ခုအတြက္ function ၂ခု မွတ္ေနရပါတယ္။ ဒါေၾကာင့္ C# language မွာေတာ့ data hiding rule ကိုလည္း လိုက္နာျပီး member data ကိုေခၚသလိုလည္း လြယ္လြယ္ကူကူ ေခၚလို႔ရေအာင္ property ဆိုတာကို သံုးလာၾကပါတယ္။ ကဲ property ကို ဘယ္လိုေရးရလဲ….

using System;
class Test_C
{
    private int var;
    public int MyVar
    {
        get
        {
            return var;
        }
        set
        {
            var = value;
        }
    }
}
class Program
{
    static void Main()
    {
        Test_C obj = new Test_C();
        obj.MyVar = 3;
        Console.WriteLine(obj.MyVar);
        Console.Read();
    }
}

Property ေရးတဲ႔ ပံုစံက member function နဲ႔ေတာ့ဆင္ပါတယ္။ အရင္ဆံုး အျပင္ကေနေခၚသံုးလို႔ရေအာင္ public ေၾကျငာေပးရပါတယ္။ ျပီးရင္property ကေန return ျပန္မယ့္ data type ေၾကျငာရမယ္၊ အဲ့ဒီေနာက္မွာေတာ့ property ရဲ႕ name ေပါ့။ function နဲ႔မတူတဲ႔ အခ်က္ကေတာ့ property မွာ parameter လက္ခံတဲ႔ အပိုင္းမပါပါဘူး။ property ထဲကို တန္ဖိုးထည့္ဖို႔အတြက္ set keyword နဲ႔ တန္ဖိုးထုတ္ဖို႔အတြက္ get keyword ကိုသံုးရပါတယ္။ ဆိုလိုတာကေတာ့ property မွာ get နဲ႔ set ဆိုျပီး အပိုင္း၂ ပိုင္းပါ၀င္ပါတယ္။ property ထဲက user က တန္ဖိုးထည့္လိုက္တဲ႔ အခါ အဲ့ဒီ property ထဲက set အပိုင္းထဲမွာ ေရးထားတဲ႔ code ေတြကို အလုပ္လုပ္ပါတယ္။ user ထည့္လိုက္တဲ႔ တန္ဖိုးကေတာ့ value လို႔ေခၚတဲ႔ keyword ထဲကိုေရာက္သြားပါတယ္။ အဲ့ဒီကေနတဆင္ member data ထဲကိုထည့္ေပးပါတယ္။ property ထဲက တန္ဖိုးထုတ္တဲ႔အခါမွာေတာ့ get ထဲမွာ ေရးထားတဲ႔ code ေတြက အလုပ္လုပ္ေပးပါတယ္။ property ကို အျပင္ကေန ေခၚတဲ႔ ပံုစံကေတာ့ ရိုးရိုး member data ကိုေခၚသလိုပဲ ေခၚရပါတယ္။ အိုေက… အခုကြၽန္ေတာ္တို႔ Property ေတြ ေရးတက္ျပီလိုထင္ပါတယ္။ property ကို member data ေတြကို ကိုယ္စားျပဳဖို႔ သံုးတာမ်ားေပမယ့္ member data မရွိပဲနဲ႔လည္း property ကိုပဲေရးလို႔လည္း ရပါတယ္။ ဒီ example program ေလး ေရးျပီး run ၾကည့္ပါဦး။

using System;
class myNumber
{
    private int num;
    public string Num
    {
        get
        {
            return num.ToString();
        }
        set
        {
            try
            {
                num = Convert.ToInt32(value);
            }
            catch
            {
                num = 0;
            }
        }
    }
    public int Square // read only property
    {
        get
        {
            return num * num;
        }
    }
}
class Program
{
    static void Main()
    {
        myNumber obj = new myNumber();
        Console.Write("Enter a number : ");
        obj.Num = Console.ReadLine();
        Console.WriteLine("Square of {0} is {1}", obj.Num, obj.Square);
        Console.Read();
    }
}

September 12, 2009

basic of C# (part 22)

Filed under: Basic of C# — Tags: , , , , , — sevenlamp @ 1:30 PM

class တစ္ခုထဲမွာ ရွိတဲ႔ member ေတြကို static member နဲ႔ instant member ဆိုျပီး ၂ မ်ိဳးခြဲ႔ျခားနိုင္ပါေသးတယ္။ static member ကို class member လို႔လည္း ေခၚပါတယ္။ instant member ကေတာ့ object member ေပါ့။ ဟုတ္ျပီ ဒါဆုိရင္ အဲ့ဒီ static member ေတြနဲ႔ instant member ေတြ ဘာကြာျခားလဲ ဆက္ၾကည့္ရေအာင္။ အဓိက ကြာျခားခ်က္ ၄ ခ်က္ေလာက္ ရွိတယ္ဗ်။ အရင္ဆံုး ဒီ program ေလးကို run ၾကည့္လိုက္ပါဦး။

class Test
{
    public static string clsVar;    // static member
    public string objVar;   // instant member
    public void Show()
    {
        Console.WriteLine(clsVar);  // use class member from instant member
        Console.WriteLine(objVar);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        Test t2 = new Test();
        t.objVar = "this is object member"; // call from object instant
        Test.clsVar = "this is class member";   // call from class name
        t.Show();
        Console.WriteLine();
        t2.Show();
        Console.Read();
    }
}

၁။ static member ေတြ ေၾကျငာဖို႔အတြက္ static keyword ကို သံုးရပါတယ္။ ရိုးရိုးmember ေတြမွာေတာ့ static ထည့္ရန္မလိုပါ။

public static string clsVar;    // static member
public string objVar;   // instant member

၂။ instant member ေတြက class ကိုေဆာက္တဲ႔ object ေတြမွာ တစ္ခုဆီ copy ပါသြားမွာ ျဖစ္ေပမယ့္၊ static member ကေတာ့ object အားလံုးအတြက္ တစ္ခုထဲရွိမွာ ျဖစ္ပါတယ္။

Test t = new Test();
Test t2 = new Test();

static member vs instant member

၃။ instant member ကို object name ကေန ျပန္ေခၚသံုးရျပီး၊ static member ကိုသံုးဖို႔အတြက္ေတာ့ class name ကေနတဆင့္သာ ေခၚခြင့္ရွိပါတယ္။

t.objVar = "this is object member"; // call from object instant
Test.clsVar = "this is class member";   // call from class name

၄။ static member ေတြကို instant member ေတြကေန တဆင့္ေခၚသံုးခြင့္ရွိပါတယ္။ ဒါေပမယ့္ instant member ေတြကိုေတာ့ static member ေတြကေန ေခၚခြင့္မရွိပါဘူး။

public void Show()
{
      	Console.WriteLine(clsVar); // call class member from object member
      	Console.WriteLine(objVar);
}

ကြၽန္ေတာ္တို႔ static member ေတြကို အရင္က သံုးဖူးတယ္ထင္လား? ဟုတ္ကဲ႔ သံုးဖူးပါတယ္။ ဥပမာအေနနဲ႔ screen မွာ စာရိုက္ဖို႔သံုးတဲ႔ WriteLine function ကိုပဲ ၾကည့္ပါ။ WriteLine function က Console Class ထဲက member function တစ္ခုပါ။ ဒါေပမယ့္ ကြၽန္ေတာ္တို႔ WriteLine function ကိုသံုးဖို႔အတြက္ Console Class ကို object ေဆာက္ပါသလား။ မေဆာက္ဖူးေနာ္ class name ကေနပဲ ေခၚသံုးသြားတာပါ Console.WriteLine() ဆိုျပီးေလ။ အိုေက အဲ့ဒီလို ေခၚခြင့္ရွိတာ WriteLine function က static member function ျဖစ္ေနလို႔ပါ။ ကဲ ကြၽန္ေတာ္တို႔ class member ကိုသံုးျပီး program တစ္ခုေလာက္ေရးၾကည့္ရေအာင္။ အဲ့ဒီ program ေလးမွာ ကြၽန္ေတာ္တို႔ customer တစ္ေယာက္ အသစ္ထည့္လိုက္တိုင္း account number auto ေပၚလာခ်င္တယ္။ ျပီးေတာ့ customer စုစုေပါင္း ဘယ္ႏွစ္ေယာက္ ရွိလဲဆိုတာကို ျပန္ျပေပးခ်င္တယ္။

class Customer
{
    string acno;
    string name;
    public Customer(string name)
    {
        this.acno = GetNewAcno(); //call static member function
        this.name = name;
    }
    public override string ToString()
    {
        return acno + "-" + name;
    }

    public static int counter = 0;
    public static string GetNewAcno()
    {
        counter++;
        return "C" + counter.ToString("D3");
    }
}
public class Program
{
    static void Main()
    {
        Customer a = new Customer("Mg Mg");
        Customer b = new Customer("Zw Zw");
        Customer c = new Customer("Ag Ag");
        Console.WriteLine("{0}", a);
        Console.WriteLine("{0}", b);
        Console.WriteLine("{0}", c);
        Console.WriteLine("Customer Count is {0}", Customer.counter);
        Console.Read();
    }
}

September 10, 2009

basic of C# (part 21)

Filed under: Basic of C# — Tags: , , , , — sevenlamp @ 9:31 AM

ဟိုေန႔က ကြၽန္ေတာ္တို႔ Instant constructor အေၾကာင္းသိခဲ႔ျပီးျပီ။ Constructor လို႔ဆိုလိုက္တာနဲ႔ programmer ေတာ္ေတာ္မ်ားမ်ား ေျပးျမင္မီၾကတာလည္း ဒီ Instant Constructor ပါပဲ။ အသံုးအမ်ားဆံုးနဲ႔ မသံုးမျဖစ္ Constructor လည္းျဖစ္ပါတယ္။ Instant Constructor အျပင္ တျခား လူသိနည္းတဲ႔ constructor ၂ ခုရွိပါေသးတယ္။ အဲ့ဒါေတြကေတာ့ Private Constructor နဲ႔ Static Constructor တို႔ပါပဲ။ Instant Constructor ကို public constructor လိုလဲ ေခၚပါေသးတယ္။ constructor ရဲ႕ ေရွ႕မွာ public keyword ကိုေရးေပးရပါတယ္။ ဘာလို႔လဲ ဆိုေတာ့ class ကို object ေဆာက္ဖို႔အတြက္ တျခား class ေတြ ထဲမွာ ေခၚသံုးလို႔ ရေအာင္ပါ။ private လို႔ေရးလိုက္လို႔ရွိရင္ေတာ့ အဲ့ဒီ private constructor ပါတဲ႔ class ကို object ေဆာက္လုိ႔ မရေတာ့ပါဘူး။ ဒါေၾကာင့္ object မေဆာက္ေစခ်င္တဲ႔ class ေတြမွာ private constructor ကိုေရးေပးေလ့ရွိပါတယ္။ ဥပမာ…

using System;
class Constant
{
    private Constant()
    {}
    public static string DeveloperName = "Sevenlamp";
}
class Program
{
    static void Main()
    {
    //Constant c = new Constant(); // error
        Console.WriteLine(Constant.DeveloperName);
        Console.Read();
    }
}

ဒီ program ေလးမွာ private constructor ေၾကျငာထားလို႔ Constant class ကို object ေဆာက္လို႔ မရပါဘူး။ ဒါဆိုရင္ အဲ့ဒီ class ထဲမွာ ရွိတဲ႔ member data ေတြကို ဘယ္လို ျပန္ေခၚသံုးမလဲ။ ဟုတ္ကဲ႔ ကြၽန္ေတာ္တို႔ private constructor ရွိတဲ႔ class ေတြထဲက member ေတြကို ျပန္သံုးလို႔ ရေအာင္ အဲ့ဒီ member ေတြကို static keyword နဲ႔ တြဲ႔ျပီး ေၾကျငာေပးရပါမယ္။ ဟုတ္ျပီ အဲ့ဒီ variable ေတြကို static variable တနည္းအားျဖင့္ class variable လို႔ေခၚပါတယ္။ အဲ့ဒီ class variable ေတြကို ေခၚဖို႔အတြက္ သူရွိေနတဲ႔ class ကုိ object ေဆာက္စရာ မလိုပါဘူး။ program မွာ ေခၚထားသလို class name ကေနတဆင့္ ေခၚသံုးလို႔ ရပါတယ္။ ေနာက္ constructor တစ္မ်ိဳးကေတာ့ static constructor ပါ။ class တစ္ခုအတြင္းမွာ ရွိတဲ႔ static member ေတြအလုပ္မလုပ္ခင္ အရင္ဆံုး static constructor က အလုပ္လုပ္ေလ့ရွိပါတယ္။ ခုန program ေလးကိုပဲ static constructor ေလးထည့္ျပီး ျပန္ run ၾကည့္ရေအာင္။

using System;
class Constant
{
    private Constant()
    {}
    static Constant()
    {
        Console.WriteLine("print from static constructor");
        DeveloperName = "Sevenlamp";
    }
    public static string DeveloperName;
}
class Program
{
    static void Main()
    {
        Console.WriteLine(Constant.DeveloperName);
        Console.WriteLine(Constant.DeveloperName);
        Console.Read();
    }
}

Program run လိုက္ရင္ “Sevenlamp” ဆိုတဲ႔ စာသားကို မရိုက္ျပခင္မွာ “print from static constructor” ဆိုတဲ႔ စာသားကိုအရင္ ရိုက္သြားတာ ေတြ႔ရမွာပါ။ ဒါေပမယ့္ တစ္ခုသိထားရမွာက static constructor က static member ေတြကို မေခၚခင္ ပထမဆံုး တစ္ၾကိမ္ပဲ အလုပ္လုပ္ပါတယ္။ ဒါေၾကာင့္ ကြၽန္ေတာ္က developername ကို ၂ ၾကိမ္ရိုက္ျပထားေပမယ့္  “print from static constructor” ကေတာ့ တစ္ၾကိမ္ပဲ ေပၚလာမွာျဖစ္ပါတယ္။ ကဲ ဒီေလာက္ဆို ကြၽန္ေတာ္တို႔ constructor ေတြ အေၾကာင္း နည္းနည္းနားလည္လာျပီ ထင္ပါတယ္။

ေနာက္ပိုဒ့္မွာ static member နဲ႔ instant member အေၾကာင္း ဆက္ေျပာပါ့မယ္။

September 7, 2009

basic of C# (part 20)

Filed under: Basic of C# — Tags: , , , , — sevenlamp @ 11:20 AM

ဒီေန႔ ကြၽန္ေတာ္တို႔ Constructor အေၾကာင္း ေျပာရေအာင္။ ကြၽန္ေတာ္တို႔ student ကို object ေဆာက္တုန္းကေရးခဲ႔တဲ႔ code ေလးကို ျပန္ၾကည့္ရေအာင္။

Student s1 = new Student();

ကြၽန္ေတာ္တို႔ class ေတြကို object ေဆာက္ဖို႔ အတြက္ new operator ကိုသံုးျပီး function တစ္ခုကို ေခၚရပါတယ္။ အဲ့ဒီ function name က class name နဲ႔ တူပါတယ္။ အဲ့ဒီ function ကို constructor function လို႔ ေခၚပါတယ္။ ကြၽန္ေတာ္တို႔ ွStudent class ေဆာက္တုန္းက constructor function ေဆာက္ခဲ႔လား။ မေဆာက္ေပးခဲ႔ပါဘူး။ ကြၽန္ေတာ္တို႔မေဆာက္ေပးခဲ႔ပဲ ဘယ္လိုျဖစ္လို႔ ေခၚသံုးလို႔ ရေနပါလိမ္႔။ ဟုတ္ကဲ႔ ကြၽန္ေတာ္တို႔ class တစ္ခု ေဆာက္လိုက္ျပီဆိုတာနဲ႔ compiler က အဲ့ဒီ class ထဲမွာ constructor function ပါလားစစ္ပါတယ္၊ မပါဘူး ဆိုရင္ default constructor function တစ္ခုကို compiler က အလိုအေလွ်ာက္ ေဆာက္ေပးပါတယ္။ ဒီလိုေဆာက္ေပးသြားလိုသာ ကြၽန္ေတာ္တို႔ class ေတြကို object ေဆာက္လို႔ ရတာဗ်။ အိုေက compiler ကေဆာက္ေပးတဲ႔ default constructor က ဘာေတြလုပ္ေပးလဲ ဆိုေတာ့ class ထဲမွာ ရွိတဲ႔ member data ေတြကို default value assign လုပ္ေပးပါတယ္။ default value ဆိုတာဘာလဲ ေျပာျပီးျပီထင္တယ္ေနာ္။ တခါတေလ default constructor က လုပ္ေပးတဲ႔ အလုပ္ေတြကို ကြၽန္ေတာ္က မၾကိဳက္ဘူး ဆိုရင္ ကြၽန္ေတာ္တို႔ လိုခ်င္တဲ႔ ပံုစံရေအာင္ ကိုယ္တိုင္ constructor ေတြ ေဆာက္ေပးဖို႔ လိုပါတယ္။ ဟုတ္ျပီ ဒါဆိုရင္ constructor ေတြဘယ္လို အလုပ္လုပ္လဲ၊ constructor ဘယ္ႏွစ္မ်ိဳးရွိလဲ၊ ကိုယ္ပိုင္ constructor ေတြ ဘယ္လိုေဆာက္လဲ ေလ့လာလိုက္ရေအာင္။

Instant Constructor, Private Constructor နဲ႔ Static Constructor ဆိုျပီး Constructor အမ်ိဳးအစား ၃ မ်ိဳးရွိပါတယ္။ လူသိအမ်ားဆံုးနဲ႔ အသံုးလည္း အမ်ားဆံုး မျဖစ္မေနသံုးေနရတဲ႔ constructor အမ်ိဳးအစားကေတာ့ instant constructor ျဖစ္ပါတယ္။ instant constructor ဆိုတာကေတာ့ ကြၽန္ေတာ္တို႔ class ေတြကို object ေဆာက္ဖို႔အတြက္ new operator နဲ႔ တြဲျပီး ေခၚေနရတဲ႔ constructor ပဲ ျဖစ္ပါတယ္။ instant constructor မွာ ၂ မ်ိဳးထပ္ခြဲနိုင္ပါတယ္။ Default Constructor နဲ႔ Parameterized Constructor ဆိုျပီး ျဖစ္ပါတယ္။ Default constructor ကို compiler က auto ေဆာက္ေပးတယ္ဆိုတာ အေပၚမွာ ကြၽန္ေတာ္ေျပာခဲ႔ျပီးပါျပီ။ ဒါေပမယ့္ တစ္ခု သိထားရမွာက compiler က ကြၽန္ေတာ္တို႔ class ထဲမွာ constructor လံုး၀ မပါဘူး ဆိုမွ default constructor ကို ေဆာက္ေပးတာပါ။ ကြၽန္ေတာ္တို႔ class ထဲမွာ constructor တစ္ခု ပါျပီဆိုတာနဲ႔ default constructor ေဆာက္တဲ႔ အလုပ္ကို compiler က မလုပ္ေပးေတာ့ပါဘူး။ instant constructor တစ္ခုေဆာက္ဖို႔အတြက္ လိုက္နာရမယ့္ စည္းကမ္းေတြကေတာ့ constructor ရဲ႕ ေရွ႕မွာ public keyword ပါရပါမယ္၊ class name နဲ႔ နာမည္တူရပါမယ္၊ return type မထည့္ေပးရပါဘူး။ ကဲ ဟိုေန႔က ေရးခဲ႔တဲ႔ student class ေလးမွာပဲ စမ္းၾကည့္ရေအာင္ဗ်ာ။ ကြၽန္ေတာ္က roll, name နဲ႔ mark တို႔ကို ကိုယ့္ စိတ္ၾကိဳက္ default value ထည့္ေပးဖို႔ ကိုယ္ပိုင္ default constructor ေလး ေဆာက္ပါမယ္။

class Student
{
	public int roll;
	public string name;
	public int mark;
	public Student() // default constructor
	{
		roll = -1;
		name = “?”;
		mark = 0;
	}
	public void Display()
	{
		Console.WriteLine(“\n Roll : {0} \n Name : {1} \n Mark : {2}”,
			roll, name, mark);
	}
}
class StudentTest
{
	static void Main()
	{
		Student s1 = new Student();
		s1.Display();
		Console.Read();
	}
}

အိုေက ဒီ program ေလးကို run ၾကည့္ရင္ constructor ထဲမွာ ကြၽန္ေတာ္တို႔ ထည့္ေပးထားတဲ႔ အတိုင္း Roll = 1, Name = “?” နဲ႔ Mark = 0 ဆိုျပီး ေပၚလာမွာပါ။ constructor ကို မထည့္ပဲ run ၾကည့္ရင္ေတာ့ Roll = 0, Name = “” နဲ႔ Mark = 0 ျဖစ္ေနပါလိမ့္မယ္။ ဒါက default constructor အလုပ္လုပ္ပံုပါ။ အခု parameterized constructor အေၾကာင္းဆက္ၾကည့္ရေအာင္။ ကြၽန္ေတာ္က student class ကို object ေဆာက္တဲ႔ constructor function ေလးထဲကို student ရဲ႕ name ေလးကို parameter အေနနဲ႔ ထည့္ေပးခ်င္တယ္ ဆိုရင္ အခုကြၽန္ေတာ္တို႔ ေဆာက္ထားတဲ႔ default constructor ကို သံုးလို႔ မရေတာ့ပါဘူး။ ဒါဆို string တစ္ခု လက္ခံမယ့္ constructor တခုထပ္ေရးေပးဖို႔ လိုအပ္လာပါျပီ။ အခုလို ေရးလိုက္ပါမယ္။

class Student
{
	public int roll;
	public string name;
	public int mark;
	public Student(string name) // parameterized constructor
	{
		roll = -1;
		this.name = name;  // assign local to member
		mark = 0;
	}
	public void Display()
	{
		Console.WriteLine(“\n Roll : {0} \n Name : {1} \n Mark : {2}”,
			roll, name, mark);
	}
}
class StudentTest
{
	static void Main()
	{
		Student s1 = new Student(“Mg Mg”);
		s1.Display();
		Console.Read();
	}
}

ဒီ program ေလးကို run ရင္ Roll = -1, Name = “Mg Mg” နဲ႔ Mark = 0 ဆိုျပီး ျပပါလိမ့္မယ္။ ဒီ program မွာ ကြၽန္ေတာ္က default constructor အစား parameterized constructor ကိုထည့္လိုက္ပါတယ္။ ဒါဆိုရင္ compiler က default constructor ေဆာက္ေပးမွာလား ? မေဆာက္ေပးဘူးေနာ္။ ဒါေၾကာင့္ ကြၽန္ေတာ္က Main function ထဲမွာ ဒီလို သြားေရးရင္ error တက္ပါလိမ့္မယ္။
Student s2 = new Student(); // error code
ဘာလို႔လဲ ဆိုေတာ့ default constructor မပါလို႔ပါ။ အခုေရးလိုက္တဲ႔ constructor ထဲမွာ this.name = name; ဆိုျပီး ေရးလိုက္တာကို ေတြ႔မွာပါ၊ အဲ႕ဒါေလး ျပန္ရွင္းပါဦးမယ္။ Student class ထဲမွာ ကြၽန္ေတာ္က name ဆိုတဲ႔ variable ေလးကို ေၾကျငာထားျပီးသားပါ။ အဲ့ဒါကို member variable လို႔ ေခၚေၾကာင္းလည္း သိျပီးသားပါ။ ဒီလိုပါပဲ ကြၽန္ေတာ္ အခု ေဆာက္လိုက္တဲ႔ constructor ထဲမွာ လဲ parameter variable name ကို ထပ္ေၾကျငာလိုက္ပါတယ္။ အဲ့ဒါကိုေတာ့ local variable လို႔ ေခၚပါတယ္။ ကဲ အခု member နဲ႔ local နာမည္တူေနပါျပီ။ ဒါဆို အေနအထားမ်ိဳးမွာ ကြၽန္ေတာ္က name လို႔ ေခၚလိုက္ရင္ အနီးဆံုး local variable ကိုပဲ compiler က ယူသံုးသြားပါတယ္။ ဒါေၾကာင့္ ကြၽန္ေတာ္က member variable name ကို ေခၚဖို႔အတြက္ this ဆိုတဲ႔ keyword နဲ႔ တြဲျပီး ေခၚေပးရပါတယ္။ instant constructor အေၾကာင္း နားလည္ျပီလားေတာ့ မသိဘူး။ နားမလည္ဘူးဆိုရင္လည္း ေျပာၾကဦးဗ်ာ။ ထပ္ျပီး ျပန္ရွင္းေပးပါ့မယ္။

ေနာက္ေန႔မွ Private နဲ႔ Static constructor ေတြ အေၾကာင္းဆက္ရေအာင္ဗ်ာ။

September 4, 2009

basic of C# (part 19)

Filed under: Basic of C# — Tags: , , , — sevenlamp @ 9:57 AM

ကြၽန္ေတာ္တို႔ မေန႔က ေရးခဲ႔တဲ႔ structure test program ေလးနဲ႔ပဲ ဆက္ျပီး ေျပာရေအာင္ဗ်ာ။ structure ကိုသံုးျခင္းအားျဖင့္ ကြၽန္ေတာ္တို႔ record ေတြသိမ္းလို႔ ရတယ္ဆိုတာ သိခဲ႔ပါျပီ။ structure ဟာ object-oriented programming မွာ အေျခခံအက်ဆံုး အပိုင္းျဖစ္တဲ႔ class ေတြျဖစ္ေပၚလာဖို႔ ကနဦး အစပါပဲ။ structure က value type ပါ။ ဒါေၾကာင့္ OOမွာ သံုးလို႔ အဆင္မေျပပါဘူး။ ဒီအခါမွာ structure ပံုစံအတိုင္း reference type အေနနဲ႔ သံုးနိုင္မယ့္ class ကို ေနာက္ပိုင္းမွာ အသံုးတြင္က်ယ္လာပါတယ္။ OOP မွာ အဓိကအက်ဆံုးကေတာ့ class ေတြပါပဲ။ ဟုတ္ျပီ၊ class တစ္ခုမွာ အေျခခံအားျဖင့္ေတာ့ data member ေတြနဲ႔ member function ေတြပါေလ႔ရွိပါတယ္။ member data ေတြဟာ သက္ဆိုင္ရာ classရဲ႕ ၀ိေသသ လကၡဏာေတြကို ကိုယ့္စားျပဳျပီးေတာ့၊ member function ေတြကေတာ့ အဲ့ဒီ classရဲ႕ လုပ္ေဆာင္ခ်က္ေတြပါ။ ဥပမာအားျဖင့္ People ဆိုတဲ႔ class တစ္ခုမွာ အမည္(name)၊ အသက္(age) စသည္တို႔ဟာ member data ေတြ ျဖစ္ၾကျပီး၊ စားသည္(eat())၊ အိပ္သည္(sleep()) စသည္တို႔ကေတာ့ member function ေတြ ျဖစ္ၾကမွာပါ။

class People
{
	string name;		// member data
	int age;		// member data
	void  eat()		// member function
	{
		// eat something…
	}
	void sleep()		// member function
	{
		// sleep at night.
	}
}

အခုကြၽန္ေတာ္ ေရးလိုက္တဲ႔ People class ကို datatype class လို႔ ေခၚပါတယ္။ မေန႔က program ေလးကို class အေနနဲ႔ ျပန္ျပီး ေရးၾကည့္ရေအာင္။

class Student
{
	public int roll;
	public string name;
	public int mark;
	public void Display()
	{
		Console.WriteLine(“\n Roll : {0} \n Name : {1} \n Mark : {2}”,
			roll, name, mark);
	}
}
class StudentTest
{
	static void Main()
	{
		Student s1 = new Student();
		s1.roll = 1; s1.name = “Ag Ag”; s1.mark = 90;
		Student s2 = new Student();
		s2.roll = 2; s2.name = “Zw Zw”; s2.mark = 88;
		Student s3 = s2;
		s3.roll = 3; s3.name = “Ma Ma”;
		// display 
		s1.Display();
		s2.Display();
		s3.Display();
		Console.Read();
	}
}

OUTPUT :

1       Ag Ag                90

3       Ma Ma                88

3       Ma Ma                88

ဒီ program ေလးကို အေသးစိတ္ျပန္ရွင္းျပပါ႔မယ္။ ပထမဆံုး data type class (Student) ေလး ေဆာက္ပါတယ္။ member data အေနနဲ႔ roll၊ name နဲ႔ mark တို႔ပါျပီး။ student information ေတြ ျပန္ထုတ္ျပေပးဖို႔ Display ဆိုတဲ႔ member function တစ္ခုပါပါတယ္။ student class ထဲက member ေတြ အားလံုးရဲ႕ ေရွ႕မွာ public ဆိုတဲ႔ keyword ေလးေရးထားတာ ေတြ႕မွာပါ၊ class တစ္ခုအတြင္းမွာ member ေတြ ေၾကျငာရင္ default က private ပါ။ ဆိုလိုတာကေတာ့ သက္ဆိုင္ရာ class အတြင္းမွာပဲ သံုးခြင့္ရွိျပီး အျပင္(တျခား class ေတြ) ကေန ေခၚသံုးလို႔ မရဘူးဆိုတဲ႔ အဓိပၸါယ္ပါ။ အခုကြၽန္ေတာ္က public လုပ္ေပးလိုက္တဲ႔ အတြက္ ၾကိဳက္တဲ႔ ေနရာကေန ေခၚသံုးခြင့္ရွိသြားပါတယ္။ ဟုတ္ျပီ။ အခုကြၽန္ေတာ္တို႔ program ေလးကို run ဖို႔ အတြက္ Main function ပါတဲ႔ run class တစ္ခု ထပ္ေဆာက္ပါတယ္။ Main function အတြင္းမွာ ပထမဆံုး ေက်ာင္းသားတစ္ေယာက္ရဲ႕ information သိမ္းဖို႔အတြက္ Student အမ်ိဳးအစား variable a ကို ေၾကျငာျပီး object ေဆာက္ေပးပါတယ္။ class ေတြဟာ reference data type ျဖစ္တဲ႔ အတြက္ variable ေၾကျငာရံုနဲ႔ တန္ဖိုးသိမ္းလို႔ မရေသးပါဘူး။ ဒါေၾကာင့္ တန္ဖိုးတကယ္ သိမ္းနိုင္ဖို႔အတြက္ object ေဆာက္ေပးရပါတယ္။ ရႈပ္သြားျပီထင္တယ္ :D
ကြၽန္ေတာ္တို႔ memory မွာ stack အပိုင္းနဲ႔ heap အပိုင္း ဆိုျပီး အပိုင္း၂ပိုင္းရွိတယ္ဗ်။ value type variable ေတြကို stack အပိုင္းမွာပဲ သိမ္းေလ့ရွိတယ္။ ဒါေၾကာင့္ မေန႔က သိမ္းခဲ႔တဲ႔ Student အမ်ိဳးအစား s1, s2, s3 တို႔က structure (value type) ျဖစ္တဲ႔အတြက္ stack အပိုင္းမွာ ေနရာယူသြားပါတယ္။ အဲ့ဒီေနရာမွာပဲ တန္ဖိုးကို တကယ္သိမ္းပါတယ္။ class ကေတာ့ မတူပါဘူး။ သူက stack ရယ္ heat ရယ္ ခြဲျပီးေနရာယူပါတယ္။ object reference(variable) ကို stack မွာ သိမ္းျပီး၊ real object ကိုေတာ့ heat မွာ သိမ္းပါတယ္။ structure နဲ႔ class တို႔ memory ေပၚမွာ ေနရာယူပံုကို ပံုေလး ဆြဲျပေပးထားပါတယ္၊ ပံုၾကည့္လိုက္ရင္ နည္းနည္းနားလည္သြားမယ္ ထင္ပါတယ္။


Structure မွာ သိမ္းသြားတဲ႔ ပံုကေတာ့ ရွင္းပါတယ္ ကြၽန္ေတာ္တို႔ class မွာ သိမ္းသြားတဲ႔ ပံုကိုပဲ ၾကည့္ရေအာင္။ ကြၽန္ေတာ္ေျပာခဲ႔သလိုပဲ class ကို variable ေၾကျငာလိုက္လို႔ ရလာတဲ႔ ေနရာမွာ value ေတြသိမ္းလို႔မရပါဘူး၊ reference ေတြပဲ သိမ္းလို႔ ရပါတယ္။ value တကယ္သိမ္းနိုင္ဖို႔အတြက္ေတာ့ ကြၽန္ေတာ္တို႔က new operator သံုးျပီး object ေဆာက္ေပးရပါတယ္။ အဲ့ဒီလို new နဲ႔ ေဆာက္လိုက္တဲ႔ object ေတြဟာ heap memoryထဲမွာ ေနရာသြားယူပါတယ္။ ဒါေၾကာင့္ Student s3 က object မေဆာက္ထားပါဘူး၊ class variable ပဲ ေၾကျငာထားပါတယ္။ ဒါေၾကာင့္ s3 အတြက္ ကိုယ္ပိုင္ object မရွိပါဘူး။ s3 ထဲကို s2 ကိုထည့္လိုက္ပါတယ္။ ဒီေနရာမွာ s2 ထဲမွာ ဘာရွိလဲ ကြၽန္ေတာ္တို႔ ျပန္ၾကည့္ေတာ့ s2 ေၾကျငာထားတဲ႔ ေနရာမွာ memory address (f002) ပဲ ရွိပါတယ္။ ဒါေၾကာင့္ s3 ထဲကိုလည္း f002 ပဲ ေရာက္သြားမွာပါ။ ဒါဆို s3 ကလည္း memory address f002 ရွိေနတဲ႔ ေနရာက object ကိုပဲ သြားျပီး point လုပ္ပါတယ္။ ဒါေၾကာင့္ s2 နဲ႔ s3 ၂ ခုလံုးက object တစ္ခုထဲကိုပဲ point ထားၾကပါတယ္။ ဒါေၾကာင့္ s3 ထဲကိုထည့္လိုက္တဲ႔ roll 3 နဲ႔ name “Ma Ma” ဟာ s2 ကို ရိုက္ထုတ္တဲ႔ ေနရာမွာပါ ေပၚလာရျခင္းျဖစ္ပါတယ္။ ေတာ္ေတာ္ေလးေတာ့ ရႈပ္သြားမယ္ထင္တယ္။ ကြၽန္ေတာ္ စာအေရးအသား ညံ့ေတာ့ ေသေသခ်ာခ်ာ နားလည္ေအာင္ မရွင္းျပနိုင္လို႔ပါ။

September 3, 2009

basic of C# (part 18)

Filed under: Basic of C# — Tags: , , , , — sevenlamp @ 5:29 PM

Enumeration ကိုသံုးျခင္းအားျဖင့္ ကြၽန္ေတာ္တို႔ ကိုယ္ပိုင္data type ေတြေဆာက္ျပီး သံုးလို႔ရလာပါမယ္။ enumeration သံုးတာ ဘာပိုေကာင္းလာလဲ အတိအက်ေတာ့ မသိဘူးဗ်။ ကြၽန္ေတာ္စိတ္ထင္ေတာ့ enumeration သံုးလိုက္ျခင္းအားျဖင့္ ၁။ memory ေပၚမွာ ေနရာယူတဲ႔ size ေသးသြားမယ္၊ ၂။ project တစ္ခုကို develop လုပ္တဲ႔ programmer ေတြ ၾကားထဲမွာ standardized ျဖစ္သြားမယ္ ( ဥပမာ.. developer တစ္ေယာက္က Sunday လို႔ေပါင္းရင္ေပါင္းမယ္၊ ေနာက္တစ္ေယာက္က SUNDAY လို႔ေပါင္းမယ္၊ တျခားတစ္ေယာက္က sun လို႔ပဲ ေပါင္းရင္ေပါင္းမယ္၊ enum defined လုပ္လိုက္ျခင္းအားျဖင့္ ဒါမ်ိဳးကိစၥေတြ မရွိနိုင္ေတာ့ဘူး ေပါ့ )။
Enumeration သံုးရင္ ကြၽန္ေတာ္တို႔က data type တစ္မ်ိဳးထဲပဲ သံုးလို႔ရတယ္။ integer ဆို integer, byte ဆို byte ေပါ့၊ record တစ္ခုပံုစံမ်ိဳး data type အစံုသိမ္းခ်င္ရင္ေတာ့ enum အစား structure ေဆာက္ျပီးသံုးၾကပါတယ္။ (ဆိုင္မဆိုင္ေတာ့ မသိဘူး တြဲျပီးေျပာလိုက္တာ :P ) ဥပမာ.. ကြၽန္ေတာ္က ေက်ာင္းသားတစ္ေယာက္ရဲ႕ data ေတြသိမ္းခ်င္တယ္ဆိုရင္ သိမ္းမယ့္ data ေတြထဲမွာ roll (int), name (string), mark (int) စတာေတြ ပါမယ္ဗ်ာ။ ဒါဆိုရင္ enum သံုးလို႔ မရေတာ့ဘူး၊ structure ဒါမွမဟုတ္ class ကိုသံုးရေတာ့မယ္။ structure နဲ႔ class က အဓိက value type နဲ႔ reference type ပဲကြာသြားတာ။ က်န္တာအားလံုး အတူတူနီးပါးပဲ။ example program ေလးၾကည့္ရေအာင္။

using System;
struct Student
{
    public int roll;
    public string name;
    public int mark;
}
class structuretest
{
    static void Main()
    {
        Student s1;
        s1.roll = 1; s1.name = "Ag Ag"; s1.mark = 90;
        Student s2 = s1;
        s2.roll = 2; s2.name = "Mg Mg";
        Student s3 = new Student();
        s3.roll = 3; s3.name = "Zw Zw"; s3.mark = 80;

        Console.WriteLine("\nRoll : {0} \nName : {1} \nMark : {2}", s1.roll, s1.name, s1.mark);
        Console.WriteLine("\nRoll : {0} \nName : {1} \nMark : {2}", s2.roll, s2.name, s2.mark);
        Console.WriteLine("\nRoll : {0} \nName : {1} \nMark : {2}", s3.roll, s3.name, s3.mark);
        Console.Read();
    }
}

ျပန္ရွင္းရေအာင္

struct Student
{
    public int roll;
    public string name;
    public int mark;
}

ဒါက Student ဆိုတဲ႔ user defined type တစ္ခု create လုပ္တာပါ။ ဒီလိုေရးလိုက္ျခင္းအားျဖင့္ compiler က Student ဆိုတာမွာေတာ့ integer အမ်ိဳးအစား variable ႏွစ္ခုရယ္၊ string တစ္ခုရယ္ ပါတယ္ဆိုတာ သိသြားပါျပီ။ variable ေတြရဲ႕ေရွ႕မွာ public လို႔ေရးတာကေတာ့ အဲ့ဒီ variable ေတြကို structure ရဲ႕ အျပင္ဘက္ တျခားေနရာကေန ေခၚသံုးခြင့္ျပဳတာပါ။
ျပီးေတာ့ Main function ထဲမွာ ကြၽန္ေတာ္က ေက်ာင္းသား ၃ေယာက္ရဲ႕ data သိမ္းခ်င္တယ္ ဆိုေတာ့ Student variable ၃ ခုေၾကျငာရမွာေပါ့။ ဒါေၾကာင့္ ကြၽန္ေတာ္က s1, s2 နဲ႔ s3 ဆိုျပီး ေၾကျငာလို္က္ပါတယ္။

Student s1;

ဆိုျပီး ေၾကျငာလိုက္တာနဲ႔ ကြၽန္ေတာ္တို႔ စက္ရဲ႕ memory ေပၚမွာ Student အမ်ိဳးအစား data ေတြသိမ္းလို႔ရမယ့္ ေနရာတစ္ခု ေဆာက္ေပးသြားပါတယ္။ အဲ့ဒီ ေနရာထဲမွာ roll, name, mark တို႔ သိမ္းဖို႔ေနရာပါ ပါျပီးသားပါ။

s1.roll = 1; s1.name = "Ag Ag"; s1.mark = 90;

ျပီးေတာ့မွ ကြၽန္ေတာ္က အဲ့ဒီထဲကို 1, “Mg Mg”, 90 တို႔ကို ထည့္လိုက္ပါတယ္။ Student အမ်ိဳးအစား s1 ထဲမွာ ရွိတဲ႔ variable ေတြကို ျပန္ေခၚခ်င္တာျဖစ္လို႔ s1(dot)variable name ဆိုျပီး ေရးရပါတယ္။

Student s2 = s1;

ေနာက္ထပ္ s2 ကို ေၾကျငာတဲ႔ေနရာမွာေတာ့ s1 ထဲက တန္ဖိုးေတြကိုပါ s2 ထဲကို ထည့္လိုက္ပါတယ္။ ဒါဆိုရင္ s2 ထဲမွာ ရွိတဲ႔ roll, name နဲ႔ mark မွာ s1 ထဲက တန္ဖိုးေတြ ေရာက္ျပီးသားျဖစ္သြားပါတယ္။

s2.roll = 2; s2.name = "Mg Mg";

ျပီးေတာ့မွ s2 ရဲ႕ roll နဲ႔ name ကို ျပန္ျပင္လိုက္ပါတယ္။ အမွတ္ကိုေတာ့ မျပင္လိုက္ပါဘူး။ ဒီလိုျပင္လိုက္ျခင္းအားျဖင့္ နဂိုတန္ဖိုးျဖစ္တဲ႔ s1 ထဲမွာ ေျပာင္းသြားမလား? Structure က value type ျဖစ္လို႔ s1 ထဲမွာ လုိက္ျပီး ေျပာင္းသြားမွာ မဟုတ္ပါဘူး။

Student s3 = new Student();
s3.roll = 3; s3.name = "Zw Zw"; s3.mark = 80;

s3 ထပ္ေၾကျငာတယ္၊ တန္ဖိုးထည့္ပါတယ္။ ဒီေနရာမွာ s3 ကို new Student() ဆိုျပီး object ေဆာက္ေပးထားတာ ေတြ႔မွာပါ။ ကြၽန္ေတာ္တို႔ structure ကို object ေဆာက္ျပီးေတာ့ ျဖစ္ျဖစ္ မေဆာက္ပဲနဲ႔ ျဖစ္ျဖစ္ ၾကိဳက္သလိုသံုးလို႔ရပါတယ္။ object ေဆာက္ေပးရင္ structure ထဲက variable ေတြကို default value assign လုပ္ေပးသြားမွာျဖစ္ျပီးေတာ့၊ object မေဆာက္ပဲသံုးရင္ေတာ့ default value assign မလုပ္ေပးပါဘူး။ ဒါဆို default value ကဘာေတြလဲ? ဟုတ္ကဲ႔ number ေတြဆို default value က 0 ပါ။ string အတြက္ကေတာ့ null ေပါ့။

ဒီ program ေလးကို ရိုက္ျပီး run ၾကည့္လိုက္ပါဦး။

basic of C# (part 17)

Filed under: Basic of C# — Tags: , , , , , — sevenlamp @ 3:22 PM

ဒီေန႔ ကြၽန္ေတာ္ OOP အေၾကာင္း မစခင္ enumeration နဲ႔ structure အေၾကာင္း နည္းနည္း ေျပာခ်င္တယ္ဗ်ာ။  အရင္ဆံုး variable declaration အေၾကာင္းကို ျပန္ေႏႊးရေအာင္။ ကြၽန္ေတာ္တို႔ number တန္ဖိုးတစ္ခု သိမ္းဖို႔ integer အမ်ိဳးအစား variable တစ္ခုေၾကျငာမယ္ဆိုရင္…

int num;

ဆိုျပီးေၾကျငာပါတယ္။ ဒါဆိုရင္ memory ေပၚမွာ integer value တစ္ခုသိမ္းဖို႔ အတြက္ ေနရာ တစ္ေနရာ ယူလိုက္ပါတယ္။ ဟုတ္ျပီ… ဒါဆိုရင္ ကြၽန္ေတာ္က

myOwnType  var1;

ဆိုျပီး ေၾကျငာလိုက္တယ္ ဆိုပါေတာ့။ ဒါဆိုကြၽန္ေတာ္တို႔ program ကို compiler က နားလည္ပါဦးမလား။ နားမလည္နိုင္ေတာ့ပါဘူး ဘာလို႔လဲဆိုေတာ့ myOwnType ဆိုတဲ႔ pre-defined type မဟုတ္တာတစ္ခုကို ေၾကျငာလိုက္လို႔ပါ။ ကြၽန္ေတာ္တို႔ Integer type ေၾကျငာေတာ့ compiler က integer value ေတြထည့္ဖို႔မွန္းသိပါတယ္။ myOwnType ကိုေၾကျငာလိုက္ေတာ့ compiler က ဘာအမ်ိဳးအစားအတြက္ ေၾကျငာေပးရမွန္းမသိဘူး ျဖစ္သြားပါတယ္။ ဒါဆိုရင္ ကြၽန္ေတာ္တို႔က compiler ကို myOwnType ဆိုတာကေတာ့ ဘယ္လို data မ်ိဳးေတြကို သိမ္းခ်င္တာပါဆိုျပီး ေျပာေပးလိုက္ရင္ မျဖစ္ဘူးလား။ ျဖစ္ပါတယ္။ ဘယ္လိုလုပ္ေပးရမွာလဲ ကြၽန္ေတာ္တို႔ user defined data type ေတြ ေၾကျငာေပးရမွာပါ။ ဒီလိုလုပ္ဖို႔အတြက္ ကြၽန္ေတာ္တို႔ enumeration ေတြ၊ structure ေတြ၊ class ေတြ အေၾကာင္းကို သိဖို႔လိုလာပါျပီ။
ကြၽန္ေတာ္တို႔ enumeration ေဆာက္ဖို႔အတြက္ enum keyword ကိုသံုးရပါတယ္။ ဟုတ္ျပီ အခု ကြၽန္ေတာ္ myOwnType ကို create လုပ္ပါေတာ့မယ္။

enum myOwnType
{
	Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}

myOwnType ထဲမွာ ကြၽန္ေတာ္က ေန႔ ၇ ေန႔ကို သိမ္းခ်င္ပါတယ္လို႔ defined လုပ္လိုက္ပါတယ္။ ကြၽန္ေတာ္က ေန႔ေတြကို စာသားေတြနဲ႔ ေရးလိုက္ေပမယ့္ enumeration မွာ တစ္ကယ္သိမ္းသြားမွာက default integer type ပါ။ ကြၽန္ေတာ္က integer type အစား တျခား ကိန္းျပည့္ number type အျဖစ္ ေၾကျငာခ်င္လို႔လည္း ရပါတယ္။ default အားျဖင့္ 0 ကေန စျပီး တခုစီကို 1 ေပါင္းေပါင္းျပီးသိမ္းသြားမွာပါ။ ဆိုလိုတာက ေရွ႕ဆံုးမွာ ရွိတဲ႔ Sunday ရဲ႕ value က 0 ျဖစ္ျပီး Monday က 1 ၊ Tuesday က 2 ပါ။ ကြၽန္ေတာ္တို႔ ၾကိဳက္တဲ႔ number နဲ႔လည္း ကိုယ္စားျပဳလို႔ ရပါတယ္။ ဥပမာ..

enum myOwnType : byte
{
	Sunday, Monday = 5, Tuesday, Wednesday, Thursday = 100, Friday, Saturday
}

အိုေက ဒီပံုစံအတိုင္းဆိုရင္
Sunday = 0,
Monday = 5,
Tuesday = 6,
Wednesday = 7,
Thursday = 100,
Friday = 101,
Saturday = 102
ဆိုျပီး ျဖစ္သြားပါမယ္။
ကြၽန္ေတာ္တို႔ enumeration test လုပ္ဖို႔ program တစ္ခုေလာက္ ေရးၾကည့္ရေအာင္။

using System;
class Program
{
    enum myOwnType : byte
    {
        Sunday, Monday = 5, Tuesday, Wednesday, Thursday = 100, Friday, Saturday
    }

    static void Main()
    {
        myOwnType wd = myOwnType.Friday;
        int i = 7;
        Console.WriteLine("{0} is {1}", wd, (int)wd);
        Console.WriteLine("{0} is {1}", i, (myOwnType)i);
        Console.ReadLine();
    }
}

Output

Friday is 101
7 is Wednesday

August 29, 2009

basic of C# (part 16)

Filed under: Basic of C# — Tags: , , , , , — sevenlamp @ 2:07 PM

ကြၽန္ေတာ္တို႔ array ေတြ ေၾကျငာတဲ႔ အခါမွာ integer array ေၾကျငာရင္ integer ပဲ သိမ္းလို႔ ရပါမယ္။ string array ဆိုရင္လည္း string ေတြပဲ သိမ္းမွာေပါ့။ ဒီလိုမဟုတ္ပဲ ကိုယ္ၾကိဳက္တဲ႔ data type ကို သိမ္းလို႔ ရခ်င္ရင္ေတာ့ object array ေဆာက္ေပးရပါတယ္။

object[] arr = new object[3];
arr[0] = 1;
arr[1] = 2.2;
arr[2] = "abcdefg";

အခုထိ ကြၽန္ေတာ္တို႔ သံုးခဲ႔တဲ႔ array ေတြရဲ႕ size ဟာ static size ျဖစ္ပါတယ္။ ကြၽန္ေတာ္တို႔ array ကို ၃ ခန္းေၾကျငာလိုက္ရင္ ၃ ခန္းထက္ပိုျပီး တန္ဖိုးထည့္လို႔ မရပါဘူး။ ကြၽန္ေတာ္တို႔ သံုးမယ့္ array ရဲ႕ size ကို dynamic size အျဖစ္သံုးခ်င္တယ္ ဆိုရင္ေတာ့ သီးျခားအေနနဲ႔ collection class ေတြကို သံုးၾကရမွာပါ။ ပထမဆံုးအေနနဲ႔ ArrayList Class အေၾကာင္း စေျပာပါ့မယ္။
ArrayList Class က array တစ္ခုလိုမ်ိဳး အလုပ္လုပ္ေပးပါတယ္။ သူက variant size ပါ။ စေၾကျငာခ်င္းခ်င္းမွာ ArrayList ရဲ႕ capacity က ၄ ခန္းယူထားေပးပါတယ္။ ကြၽန္ေတာ္တို႔ တန္ဖိုးထည့္ရင္းနဲ႔ ၄ခန္းျပည့္ျပီးလို႔ ေနာက္ထပ္ထပ္ထည့္တာနဲ႔ capacity က ၈ခန္းျဖစ္သြားမွာပါ။ ကိုယ္တကယ္ တန္ဖိုးထည့္ထားတဲ႔ အေရအတြက္ကိုေတာ့ count ဆိုတဲ႔ property ကေန ျပန္သိနိုင္ပါတယ္။ ArrayList က zero base index ျဖစ္ျပီးေတာ့ သူ႔ရဲ႕ index က integer type ပါ။ ArrayList ကလည္း object type လက္ခံတာျဖစ္တဲ႔ အတြက္ ကြၽန္ေတာ္တို႔ ၾကိဳက္တဲ႔ type ထည့္ေပးခြင့္ရွိပါတယ္။ example ေလးၾကည့္ရေအာင္။

using System;
using System.Collections;

class arraylistTest
{
    static void Main()
    {
        object[] arr = new object[3];
        arr[0] = 1;
        arr[1] = 2.2;
        arr[2] = "abcdefg";

        ArrayList ar = new ArrayList();
        ar.Add(1);
        ar.Add(9.456);
        ar.Add("ABC");
        ar.Add('Z');
        ar.Add(new arraylistTest());

        for (int i = 0; i < ar.Count; i++)
        {
            Console.WriteLine("{0}\t( {1} )", ar[i], ar[i].GetType());
        }
        Console.WriteLine("\nActual Size of your Array : " + ar.Count);
        Console.WriteLine("Total Capacity of your Array : " + ar.Capacity);
        Console.Read();
    }
}

Hashtable Class
Hashtable class ထဲမွာ key/value တြဲ႕ data ေတြ လက္ခံပါတယ္။ key/value အတြဲလိုက္ကို DictionaryEntry လို႔ေခၚပါေသးတယ္။ ArrayList မွာ ကြၽန္ေတာ္တို႔ index ကို integer type သံုးပါတယ္။ Hashtable မွာေတာ့ index ေထာက္ဖို႔အတြက္ key ကိုသံုးပါတယ္။ သူကလည္း object type ပါ၊ ဒါေၾကာင့္ ကြၽန္ေတာ္တို႔ ၾကိဳ႔က္တဲ႔ type နဲ႔ index ေထာက္လို႔ ရပါတယ္။ သိမ္းမယ့္ data ေတြကိုေတာ့ value အပိုင္းမွာ သိမ္းရပါတယ္။ သူလည္း object type ပါပဲ။ Hashtable ကလည္း ArrayList လိုပဲ size ကို dynamically increment လုပ္လို႔ ရပါတယ္။ Example ေလးၾကည့္ရေအာင္။

using System;
using System.Collections;
class arraylistTest
{
    static void Main()
    {
        Hashtable ht = new Hashtable();
        ht.Add("Name", "Sevenlamp");
        ht.Add("Address", "Yangon");
        ht.Add("Age", 28);

        foreach (DictionaryEntry de in ht)
        {
            Console.WriteLine("{0}\t\t: \t {1} ", de.Key, de.Value);
        }
        Console.WriteLine("\nThere are {0} element/s in your Hashtable Array", ht.Count);
        Console.Read();
    }
}
Older Posts »

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.