our notes for our youngers

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

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.