our notes for our youngers

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

August 23, 2009

basic of C# (part 15)

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

အခု ကြၽန္ေတာ္တို႔ C# မွာ သံုးလို႔ရတဲ႔ Array ေတြအေၾကာင္း ေလ့လာရေအာင္။ C# မွာ array အမ်ိဳးအစား ေတာ္ေတာ္စံုစံုလင္လင္ကို သံုးလို႔ရပါတယ္။ အရင္ဆံုး ကြၽန္ေတာ္တို႔ သိျပီးသား single dimension array ေလးကေန စလိုက္ရေအာင္။ ကြၽန္ေတာ္က integer value ၅ ခုသိမ္းဖို႔အတြက္ array ေလးတစ္ခု ေဆာက္ခ်င္ရင္ ခုလိုေၾကျငာရမွာပါ။

int[ ] arr = new int[5];

ျပီးရင္ အဲ့ဒီ array ထဲကို တန္ဖိုး ၅ခု ထည့္ခ်င္တယ္ဆိုရင္ ခုလိုေရးရမယ္။

  arr[0] = 2;
  arr[1] = 4;
  arr[2] = 6;
  arr[3] = 8;
  arr[4] = 0;

ေနာက္တစ္နည္းအားျဖင့္ array ေၾကျငာေနတုန္းမွာ တန္းဖိုးကိုပါ ထည့္ေပးလိုက္ခ်င္တယ္ဆိုရင္ လည္းရပါတယ္။ အဲ့ဒါကို array initialization လို႔ေခၚပါတယ္။ ဒီလိုမ်ိဳးပါ….

int[ ] arr = new int[ ] { 2, 4, 6, 8, 0 };

အခုလိုမ်ိဳး statement တစ္ေၾကာင္းထဲမွာ array declaration ေရာ initialization ပါ ေပါင္းလုပ္မယ္ဆိုရင္ ေနာက္တစ္မ်ိဳးက ဒီလိုလည္း ေရးလို႔ ရပါတယ္…

int[ ] arr = { 2, 4, 6, 8, 0 };

ဒါေပမယ့္ statement ၂ ေၾကာင္းခြဲေရးမယ္ဆိုရင္ေတာ့ new int[ ] ဆိုတဲ႔ အပိုင္းေလးကို မထည့္လို႔ မရပါဘူး။

  int[ ] arr;          // declaration statement
  arr = new int[ ] { 2, 4, 6, 8, 0 };// initialization statement

example ေလး တစ္ခုေလာက္ ေရးၾကည့္ရေအာင္..

      using System;
      class forlooptest
      {
          static void Main()
          {
              int[] arr1 = { 1, 3, 5, 7, 9 };
              int[] arr2 = { 2, 4, 6, 8, 0 };
              int[] arr3 = new int[5];
              for (int i = 0; i < arr1.Length; i++)
              {
                  arr3[i] = arr1[i] + arr2[i];
              }
              Console.Write("Result array : ");
              foreach (int v in arr3)
                  Console.Write(v + " ");
              Console.Read();
          }
      }

multi-dimension array ဆိုတာကေတာ့ dimension တစ္ခုထက္ပိုပါတဲ႔ array လို႔ ဆိုလိုတာေပါ့ :) ။ dimension တစ္ခုစီကို ( , ) ေလးနဲ႔ ခံျပီးေရးေပးရပါတယ္။

int[,] arr = new int[3,2];

အခု ေၾကျငာလိုက္တဲ႔ array ေလးမွာ dimension ၂ ခုပါျပီး၊ ပထမ dimension မွာ ၃ ခန္း၊ ဒုတိယ dimension မွာ ၂ ခန္း၊ ယူသြားပါမယ္။
တန္ဖိုးထည့္မယ္ဆိုရင္…

arr[0,0] = 1;
arr[0,1] = 2;
arr[1,0] = 3;
arr[1,1] = 4;
arr[2,0] = 5;
arr[2,1] = 6;

multidimensional array ေတြကိုလည္း initialize လုပ္လို႔ရပါတယ္။ ဒီလိုမ်ိဳးပါ…

int[,] arr = { {1,2} , {3,4} , {5,6} } ;

multidimensional array ေတြကို looping ပတ္မယ္ဆိုရင္ေတာ့ dimension အေရအတြက္ေပၚမူတည္ျပီး loopingေတြ ေရးရမွာပါ။

for (int i = 0; i <= arr.GetUpperBound(0); i++)
{
for (int j = 0; j <= arr.GetUpperBound(1); j++)
      		Console.Write(arr[i, j]+" ");
Console.WriteLine();
}

GetUpperBound function ေလးက demension တစ္ခုရဲ႕ upperbound တန္ဖိုးကို return ျပန္ပါတယ္။ ဒါေၾကာင့္ ကိုယ္သိခ်င္တဲ႔ dimension index ေလးကို parameter ထည့္ေပးရပါတယ္။
အခုကြၽန္ေတာ္တို႔ matrix 2 ခု ေျမွာက္တဲ႔ program ေလး ေရးၾကည့္ရေအာင္။ နည္းနည္းေတာ့ ရွည္ျပီး ႐ႈပ္မယ္ထင္တယ္ :D

using System;
class Program
{
    static int GetInteger()
    {
        try
        {
            return Convert.ToInt32(Console.ReadLine());
        }
        catch
        {
            return 1;
        }
    }
    static void Main(string[] args)
    {
        int r1, r2, c1, c2;
        int[,] matrix1;
        int[,] matrix2;
        int[,] matrix3;
        Console.WriteLine("First Matrix\n************");
        Console.Write("row : ");
        r1 = GetInteger();
        Console.Write("col : ");
        c1 = GetInteger();
        matrix1 = new int[r1, c1];

        Console.WriteLine();
        Console.WriteLine("Second Matrix\n************");
        Console.Write("row : ");
        r2 = GetInteger();
        Console.Write("col : ");
        c2 = GetInteger();
        matrix2 = new int[r2, c2];

        if (!((r1 == 1 && c1 == 1) || (r2 == 1 && c2 == 1) || (c1 == r2)))
        {
            Console.WriteLine("\nOut of result!");
            Console.Read();
            return;
        }

        Console.WriteLine();
        for (int i = 0; i <= matrix1.GetUpperBound(0); i++)
        {
            for (int j = 0; j <= matrix1.GetUpperBound(1); j++)
            {
                Console.Write("Enter value for first matrix : row {0}, column {1} : ", i, j);
                matrix1[i, j] = GetInteger();
            }
        }
        Console.WriteLine();
        for (int i = 0; i <= matrix2.GetUpperBound(0); i++)
        {
            for (int j = 0; j <= matrix2.GetUpperBound(1); j++)
            {
                Console.Write("Enter value for second matrix : row {0}, column {1} : ", i, j);
                matrix2[i, j] = GetInteger();
            }
        }

        if (c1 != r2)
        {
            if (r1 == 1 && c1 == 1)
            {
                matrix3 = new int[r2, c2];
                for (int i = 0; i <= matrix2.GetUpperBound(0); i++)
                    for (int j = 0; j <= matrix2.GetUpperBound(1); j++)
                        matrix3[i, j] = matrix2[i, j] * matrix1[0, 0];
            }
            else
            {
                matrix3 = new int[r1, c1];
                for (int i = 0; i <= matrix1.GetUpperBound(0); i++)
                    for (int j = 0; j <= matrix1.GetUpperBound(1); j++)
                        matrix3[i, j] = matrix1[i, j] * matrix2[0, 0];
            }
        }
        else
        {
            matrix3 = new int[r1, c2];
            for (int i = 0; i <= matrix1.GetUpperBound(1); i++)
            {
                for (int j = 0; j <= matrix1.GetUpperBound(0); j++)
                {
                    for (int k = 0; k <= matrix2.GetUpperBound(1); k++)
                        matrix3[j, k] += matrix1[j, i] * matrix2[i, k];
                }
            }
        }

        Console.WriteLine("\nResult Matrix\n*************");
        for (int i = 0; i <= matrix3.GetUpperBound(0); i++)
        {
            for (int j = 0; j <= matrix3.GetUpperBound(1); j++)
            {
                Console.Write(matrix3[i, j] + " ");
            }
            Console.WriteLine();
        }
        Console.Read();
    }
}

August 12, 2009

basic of C# (part 14)

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

တခါတေလ ကြၽန္ေတာ္တို႔ Looping ေတြထဲမွာ looping variable မပါတာမ်ိဳးလည္း ရွိပါတယ္။ looping ကို အၾကိမ္အေရအတြက္ အတိအက် မဟုတ္ပဲ looping variable မပါတဲ႔ looping မ်ိဳး သံုးဖို႔ လိုလာျပီ ဆိုရင္ေတာ့ while နဲ႔ do-while ကိုသံုးေလ့ရွိပါတယ္။ ဥပမာ.. ကြၽန္ေတာ္က user ဆီက character တစ္လံုးလက္ခံမယ္၊ ျပီးရင္ အဲ့ဒီ character ရဲ႕ ASCII value ကိုျပန္ျပီးထုတ္ျပေပးမယ္။ ဒီအလုပ္ကိုပဲ user ၾကိဳက္သေလာက္ လုပ္နိုင္တယ္။ မလုပ္ခ်င္ေတာ့ဘူးဆိုရင္ ESC key ႏွိပ္ျပီးထြက္ရမယ္။ ဟုတ္ျပီေနာ္ ဒီ program ေလးေရးၾကည့္ရေအာင္။

using System;
class forlooptest
{
    static void Main()
    {
        bool stop = false;
        do
        {
            Console.Write("Enter a char to see ASCII value of it (or) press ESC to close.");
            ConsoleKeyInfo cki = Console.ReadKey();
            if (cki.Key != ConsoleKey.Escape)
                Console.WriteLine("\n{0} = {1}\n", cki.Key.ToString(), (int)cki.KeyChar);
            else
                stop = true;
        } while (stop==false);
    }
}

ဒီ program ေလးမွာ looping ဆက္လုပ္မလား မလုပ္ေတာ့ဘူးလား ဆိုတာကို stop ဆိုတဲ႔ boolean variable ေလးနဲ႔ ထိန္းထားတယ္။ default value အေနနဲ႔ false ထည့္ေပးထားတယ္(program မပိတ္ဖူးလို႔ ဆိုလိုတာေပါ့) user ဆီကေန key တစ္ခုထဲလက္ခံမယ္ဆိုရင္ Console.ReadKey() function ကိုသံုးရတယ္။ သူက ConsoleKeyInfo ဆိုတဲ႔ structure ေလး return ျပန္တယ္။ အဲ့ဒီ ConsoleKeyInfo structure ထဲမွာ Key ဆိုတဲ႔ property က user ရိုက္လိုက္တဲ႔ ConsoleKey အမ်ိဳးအစားျဖစ္ျပီး၊ KeyChar ကေတာ့ character ျပန္ထုတ္ေပးတယ္။ ဒါေၾကာင့္ user ရိုက္လိုက္တဲ႔ key က Escape နဲ႔ မတူဘူးဆိုရင္ user ကို output ထုတ္ျပတယ္။ တူတယ္ဆိုရင္ေတာ့ program ပိတ္ဖို႔အတြက္ stop ထဲကို true ထည့္ေပးလိုက္တယ္။ ေနာက္ဆံုးမွ while condition မွာ stop ထဲက တန္ဖိုးဟာ false နဲ႔ ညီလား စစ္လိုက္တယ္။ ညီရင္ looping ကို ေနာက္တစ္ေခါက္ျပန္လုပ္မယ္။ မညီဘူးဆိုရင္ program ပိတ္သြားမွာပါ။

foreach looping
foreach looping ကို collection(အစုအေ၀း) တစ္ခုအတြင္းမွာ ရွိသေလာက္ အားလံုး လုပ္ခ်င္တယ္ဆိုရင္ သံုးေလ႔ရွိပါတယ္။ array ထဲမွာ ရွိသေလာက္လုပ္ခ်င္တယ္၊ class ထဲက property ေတြအားလံုးလုပ္ခ်င္တယ္၊ ဒီလို အေျခအေနမ်ိဳးမွာ foreach loop ကိုသံုးပါတယ္။ ဥပမာ.. ကြၽန္ေတာ့္ ဘေလာ့ကို လာၾကည့္တဲ႔ သူငယ္ခ်င္းေတြ အားလံုးကို တစ္ေယာက္ laptop တစ္လံုး လက္ေဆာင္ေပးခ်င္တယ္ ဆိုရင္ foreach ကိုသံုးျပီး ဒီလို ေရးရမွာပါ.. :D

foreach( People p in myBlog.visitors )
{
	p = “a laptop”;
}

foreach ကိုသံုးလိုက္ျခင္းအားျဖင့္ ကြၽန္ေတာ့ ဘေလာ့ကို လာၾကည့္တဲ႔သူ ဘယ္ေလာက္ရွိလဲ ဆိုတာကို သိေနစရာ မလိုေတာ့ပါဘူး။ visitor မကုန္မခ်င္း ဒီ looping က အလုပ္လုပ္မွာပါ။ foreach looping မွာေတာ့ start ေတြ stop ေတြ step ေတြ မလိုပါဘူး။ in keyword ရဲ႕ ေရွ႕မွာ colletion အတြင္းမွာ ရွိတဲ႔ value ေတြရဲ႕ datatype ကိုေၾကျငာရျပီး in ရဲ႕ ေနာက္မွာေတာ့ collection name ကိုေပးရပါတယ္။ အခု foreach ကိုသံုးျပီး program ေလးတစ္ခု ေရးၾကည့္ရေအာင္။ ကြၽန္ေတာ္က user ဆီက စာေၾကာင္းတစ္ေၾကာင္းလက္ခံမယ္။ ျပီးရင္ အဲ့ဒီ စာေၾကာင္းထဲမွာ ရွိတဲ႔ character ေတြရဲ့ ASCII value ကို ျပန္ျပီး ျပမယ္။

using System;
class forlooptest
{
    static void Main()
    {
            string st;
            Console.Write("Enter a string : ");
            st = Console.ReadLine();
            foreach (char c in st)
            {
                Console.WriteLine("{0} = {1}", c, (int)c);
            }
            Console.Read();
    }
}

August 11, 2009

basic of C# (part 13)

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

ဒီေန႔ ကြၽန္ေတာ္တို႔ looping ေတြ အေၾကာင္း ဆက္ရေအာင္။ looping ဆိုတာဘာလဲ…. အလုပ္တစ္ခုကို ထပ္တလဲလဲ လုပ္တာကို loop လိုေခၚတယ္။ ဥပမာဗ်ာ ကြၽန္ေတာ္က screen မွာ လိုင္း၁လိုင္းမွာ star(*) တစ္လံုးပါတဲ လိုင္း ၁၀လိုင္းရိုက္ျပခ်င္တယ္ ဆိုပါေတာ့။ ဒါကိုေရးမယ္ဆိုရင္ Console.WriteLine(“*”); ဆိုတာကို ၁၀ခါေရးရမွာေပါ့။ ဟုတ္တယ္မလား။ ဒီလို ၁၀ခါမေရးပဲ ၁ခါပဲေရးျပီး အဲ့ဒီ statement ကိုပဲ ၁၀ၾကိမ္လုပ္ေပးပါလို႔ compiler ကိုခိုင္းခ်င္ရင္ ကြၽန္ေတာ္တို႔ looping ကိုသံုးရေတာ့မွာပါ။ C# မွာ looping ၄ မ်ိဳးရွိပါတယ္။

၁။ while

၂။ do – while

၃။ for

၄။ foreach

looping ဆိုရင္ ေတာ္ေတာ္မ်ားမ်ား သံုးၾကတာက for loop ပါ။ looping ေတြမွာ looping variable ဆိုတာေလး မ်ားေသာအားျဖင့္ရွိၾကပါတယ္၊ ဥပမာ looping ၁၀ၾကိမ္လုပ္မယ္ဆိုရင္ လက္ရွိမွာ ဘယ္ႏွစ္ၾကိမ္ရွိျပီလဲဆိုတာ မွတ္ေပးထားမယ့္ variable ေလးေပါ့။ looping အလုပ္လုပ္ဖို႔အတြက္ အဓိက အခ်က္ ၃ခ်က္ သိရပါမယ္။ loopng variable ေလး ဘယ္ကစျပီး အလုပ္လုပ္မလဲ ဆိုတဲ႔ start ရယ္၊ ဘယ္အခ်ိန္ထိအလုပ္လုပ္မလဲဆိုတဲ႔ stop ရယ္၊ ဘယ္လို တိုးသြားမလဲဆိုတဲ႔ step ရယ္ေပါ့။ ဒီ၃ခ်က္ကို for looping မွာ ဒီလိုေရးပါတယ္…

for( start; stop; step )
{
	statements;
}

Example:

for( int i = 1; i<=10;  i++ )
{
	Console.WriteLine(“*”);
}

start ဆိုတာ looping variable ေလး စေၾကျငာျပီး တန္ဖိုးတစ္ခုထည့္ေပးလိုက္တဲ႔ အပိုင္းျဖစ္ပါတယ္။ stop ဆိုတာကေတာ့ condition တစ္ခုျဖစ္ျပီး၊ အဲ့ဒီ condition မွာ မွန္ေနသေရႊ႕ looping က အလုပ္လုပ္ေနမွာပါ။ step အပိုင္းမွာေတာ့ looping variable ထဲကို ေျပာင္းသြားခ်င္တဲ႔ တန္ဖိုးတခုခုထည့္ေပးရပါတယ္။ အေပၚက ဥပမာမွာ looping variable ထဲထတန္ဖိုးကို ၁တိုးသြားခ်င္လို႔ i++ လိုေရးတာပါ။ တခါတေလ start အပိုင္းကို looping ရဲ႕ အေပၚမွာလည္း ေရးတာေတြ႕ဖူးမွာပါ။ ဒီလိုပါပဲ step ကိုလည္း looping statements ေတြထဲမွာ ထည့္ေရးလို႔ရပါတယ္။ ဒါဆိုရင္ ဒီလိုေလး ျဖစ္သြားမွာေပါ့။

start;
for( ; stop; )
{
	statements;
	step;
}

Example:

int i = 1;
for( ; i<=10; )
{
	Console.WriteLine(“*”);
        i++;
}

အခုေရးျပလိုက္တဲ႔ for looping ေလးကို while looping နဲ႔ ေျပာင္းေရးမယ္ဆိုရင္ေတာ့ ဒီလိုျဖစ္သြားပါမယ္။

start;
while ( stop )
{
	statements;
	step;
}

Example:

int i = 1;
while( i<=10 )
{
	Console.Write(“*”);
	i++;
}

ကဲ… အဲ့ဒီ while loop ေလးမွာပါတဲ႔ while() ေလးကို ေအာက္ကိုပို႔ျပီးေရးမယ္ ဆိုရင္ေတာ့ do-while ေလးက ဒီလိုပံုစံပါ။

start;
do{
	statements;
	step;
}while( stop );

Example:

int i = 1;
do{
	Console.WriteLine(“*”);
	i++;
}while( i<=10 );

do-while looping မွာ condition စစ္တဲ႔ အလုပ္ကို ေနာက္ဆံုးမွာ ေရးလိုက္ေတာ့ condition မွန္သည္ျဖစ္ေစ၊ မွားသည္ျဖစ္ေစ အနည္းဆံုးတစ္ၾကိမ္ေတာ့ looping ထဲက statement ေတြကို အလုပ္လုပ္သြားမွာပါ။ for loop နဲ႔ while loop ကေတာ့ မွားရင္ တစ္ၾကိမ္မွ အလုပ္လုပ္မွာ မဟုတ္ပါဘူး။

August 5, 2009

basic of C# (part 12)

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

Selection control statement

C# program ေတြ ေရးေနတဲ႔အခါ ကိုယ္ေရးထားတဲ႔ statement ေတြကို line by line compiler ကအလုပ္လုပ္ေပးပါတယ္။ တခါတေလ ကိုယ္ေရးထားတာေတြအားလံုး မလုပ္ေစခ်င္ပဲ အေျခအေန တစ္ခုေပၚမူတည္ျပီး ေရြးခ်ယ္လုပ္ခိုင္းခ်င္ရင္ Condition စစ္ဖို႔လိုလာပါျပီ။ ကြၽန္ေတာ္တို႔ C# မွာ condition ေတြစစ္ဖို႔အတြက္ ပံုစံ ၃မ်ိဳးေရးလို႔ရတယ္ဗ်။

if…..else

switch

conditional operator (?:)

အရင္ဆံုး ဥပမာေလး တစ္ခုနဲ႔ သူတို႔ကို ဘယ္လိုသံုးလဲ ယွဥ္ျပီး ေျပာၾကည့္မယ္ဗ်ာ။ ကြၽန္ေတာ္က user ဆီကေန number value တစ္ခုကို လက္ခံျပီး even လား order လား ဆိုတာ စစ္တဲ႔ program ေလးတစ္ခုေရးၾကည့္ရေအာင္။

    using System;
    class Program
    {
        static void Main()
        {
            int num;
            Console.Write("enter number");
            try
            {
                num = Convert.ToInt32(Console.ReadLine());
                if (num % 2 == 0)
	        {
                    Console.WriteLine("Even");
      	        }
                else
 	        {
                    Console.WriteLine("Odd");
	        }
            }
            catch
            {
                Console.WriteLine("Error");
            }
            Console.Read();
        }
    }

ကြၽန္ေတာ္ အနီေရာက္ကာလာနဲ႔ ေရးထားတဲ႔အပိုင္းေလးကို ျပန္ရွင္းျပပါ့မယ္။ user ထည့္လိုက္တဲ႔ တန္ဖိုးကို num ထဲမွာ သိမ္းလိုက္ပါတယ္။ အဲ့ဒီ တန္ဖိုးကို even ျဖစ္လားစစ္ဖို႔အတြက္ 2 နဲ႔ စားၾကည့္ပါတယ္။ အၾကြင္းက 0 နဲ႔ ညီေနရင္ စားလိုျပတ္တယ္ေပါ့။ ဒါဆိုရင္ စံုကိန္းျဖစ္ျပီး စားလို႔ မျပတ္ဘူးဆိုရင္ေတာ့ မကိန္းပါ။ if ေနာက္မွာ ကိုယ္စစ္ခ်င္တဲ႔ expression ကို ( နဲ႔ ) ထဲထည့္ေရးရပါတယ္။ ျပီး { } အတြင္းမွာ မွန္ရင္ လုပ္ခ်င္တဲ႔ အလုပ္ကိုေရးတယ္။ else ေနာက္က { } ထဲမွာေတာ့ မွားရင္ လုပ္ခ်င္တဲ႔ အလုပ္ကို ေရးရပါတယ္။ အခုကြၽန္ေတာ္ေရးထားသလိုမ်ိဳး လုပ္ခ်င္တဲ႔ အလုပ္က statement တစ္ေၾကာင္းထဲဆိုရင္ { } ကို မထည့္ေပးဘူး ဆိုရင္လည္း မွန္ပါတယ္။ statement တစ္ေၾကာင္းထက္ပိုရင္ေတာ့ { } ကို မထည့္ေပးလို႔ မရပါဘူး။ if condition မွာ မွန္ရင္လုပ္ခ်င္တာပဲ ရွိျပီး မွားရင္ လုပ္ခ်င္တာ မရွိဘူး ဆိုရင္လည္း else အပိုင္းကို ထည့္မေရးရပါဘူး။ အဲ.. မွားရင္လုပ္ခ်င္ျပီး မွန္ရင္ လုပ္ခ်င္တာ မရွိဘူးဆိုရင္ေတာ့ if ကို ျဖဳတ္ထားျပီး else ၾကီးပဲ ေရးလို႔ေတာ့ မရဘူးေနာ္။ အဲ့လိုလုပ္ခ်င္တယ္ဆိုရင္ေတာ့ if () ထဲမွာ not (!) ခံေပးရပါမယ္။

ကဲ.. အခု switch ကိုသံုးျပီး ေရးၾကည့္ရေအာင္။

    using System;
    class Program
    {
        static void Main()
        {
            int num;
            Console.Write("enter number");
            try
            {
                num = Convert.ToInt32(Console.ReadLine());
                switch (num % 2)
                {
                    case 0: Console.WriteLine("Even");
                        break;
                    default: Console.WriteLine("Odd");
                        break;
                }
            }
            catch
            {
                Console.WriteLine("Error");
            }
            Console.Read();
        }
    }

switch ကိုသံုးျပီး စစ္ဖို႔အတြက္ switch ေနာက္မွာ ကိုယ္စစ္ခ်င္တဲ႔ condition တစ္ခုထည့္ေပးရတယ္။ ျပီး အဲ့ဒီ condition ကထြက္လာမယ့္ result ေတြကို case ေနာက္မွာ ေရးျပီးစစ္ရပါတယ္။ case ေတြၾကိဳက္သေလာက္ ေရးလို႔ရပါတယ္။ case တစ္ခုစီအဆံုးမွာ break ထည့္ေပးရပါတယ္။ default ဆိုတာကေတာ့ else သေဘာမ်ိဳးပါ။ အေပၚက case ေတြအားလံုး မွားခဲ႔ရင္လို႔ အဓိပၸါယ္ရပါတယ္။

ဒါေလးကိုပဲ conditional operator နဲ႔ ေျပာင္းစစ္ၾကည့္ရေအာင္…

    using System;
    class Program
    {
        static void Main()
        {
            int num;
            Console.Write("enter number");
            try
            {
                num = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine( (num % 2 == 0) ? "Even" : "Odd" );
            }
            catch
            {
                Console.WriteLine("Error");
            }
            Console.Read();
        }
    }

Conditional operator ကိုသံုးရင္ေတာ့ ( ? ) ရဲ႕ေရွမွာ ကြၽန္ေတာ္စစ္ခ်င္တဲ႔ expression ကိုေရးရတယ္၊ ( ? ) ရဲ႕ေနာက္မွာ မွန္ရင္လုပ္ခ်င္တာေရး ( : ) ရဲ႕ေနာက္မွာေတာ့ မွားရင္ လုပ္ခ်င္တာကို ေရးရပါတယ္။

Selection statement တစ္ခုစီမွာ သူ႔ အားသာခ်က္နဲ႔ သူရွိၾကပါတယ္။ ဒီထဲမွာ အသံုးအမ်ားဆံုးကေတာ့ အားလံုးသိၾကတဲ႔ if … else statement ပါပဲ။ condition စစ္ခ်င္တဲ႔ေနရာ ေတာ္ေတာ္မ်ားမ်ားမွာ if – else ကိုသံုးၾကေလ႔ရွိပါတယ္။ if-else မွာ variable အခ်င္းခ်င္း တိုက္စစ္လို႔လည္း ရပါတယ္။ တခါတေလမွာေတာ့ ကြၽန္ေတာ္တို႔မွာ စစ္စရာ အေျခအေနေတြ အရမ္းမ်ားေနရင္ေတာ့ if-else နဲ႔စစ္ရတာ အလုပ္ရႈပ္တယ္ထင္ရင္ switch ကိုသံုးေလ႔ ရွိပါတယ္။ ဥပမာဗ်ာ.. ကြၽန္ေတာ္က user ဆီကေန ၀ ကေန ၁၀ အတြင္း number တစ္လံုးထည့္ေပးပါလို႔ေတာင္းမယ္။ ျပီး.. user ထည့္လိုက္တဲ႔ number ကို ျပန္စစ္မယ္ဆိုပါေတာ့။ ဒီလိုမ်ိဳးဆိုရင္ေတာ့ if-else ထက္ switch က ပိုျပီးသံုးလို႔ေကာင္းမွာပါ။ conditional operator နဲ႔ေရးရင္ေတာ့ အရမ္းကို ရႈပ္ေထြးသြားမွာပါ။ လံုး၀ အဆင္ေျပမွာမဟုတ္ပါဘူး။

if-else

                if(num == 0)
                    Console.WriteLine("Zero");
                else if (num == 1)
                    Console.WriteLine("One");
                else if (num == 2)
                    Console.WriteLine("Two");
                else if (num == 3)
                    Console.WriteLine("Three");
                else if (num == 4)
                    Console.WriteLine("Four");
                else if (num == 5)
                    Console.WriteLine("Five");
                else if (num == 6)
                    Console.WriteLine("Six");
                else if (num == 7)
                    Console.WriteLine("Seven");
                else if (num == 8 )
                    Console.WriteLine("Eight");
                else if (num == 9)
                    Console.WriteLine("Nine");
                else
                    Console.WriteLine("Greater than 9");

switch

switch(num)
{
    case 0:Console.WriteLine("Zero");
      	break;
    case 1:Console.WriteLine("One");
      	break;
    case 2:Console.WriteLine("Two");
      	break;
    case 3:Console.WriteLine("Three");
      	break;
    case 4:Console.WriteLine("Four");
      	break;
    case 5:Console.WriteLine("Five");
      	break;
    case 6:Console.WriteLine("Six");
      	break;
    case 7:Console.WriteLine("Seven");
      	break;
    case 8:Console.WriteLine("Eight");
      	break;
    case 9:Console.WriteLine("Nine");
      	break;
    default:Console.WriteLine("Greater than 9");
      	break;
}

Conditional Operator

Console.WriteLine((num == 0) ? "Zero" : (num == 1) ? "One" : 
(num == 2) ? "Two" : (num == 3) ? "Three" : (num == 4) ? "Four" : 
(num == 5) ? "Five" : (num == 6) ? "Six" : (num == 7) ? "Seven" : 
(num == 8 ) ? "Eight" : (num == 9) ? "Nine" : "Greater than 9");

Switch နဲ႔ေရးတာ အရွင္းဆံုးျဖစ္မယ္ထင္တယ္ေနာ္။ ဒါေပမယ့္ switch ရဲ႕ အားနည္းခ်က္ေတြကလည္း ေတာ္ေတာ္မ်ားတယ္ဗ်။ switch မွာ case ေနာက္မွာ constant expression ပဲေရးခြင့္ရွိတယ္။ variable ေတြနဲ႔စစ္လို႔ မရဘူး။ ဥပမာ

int val = 0;
if(num%2==val) // this is ok
switch(num%2)
{
     case val: // this is not ok ( coz : val is a variable name, not a constant )

Conditional operator ကိုေတာ့ သိပ္ရႈပ္ရႈပ္ေထြးေထြး စစ္စရာ မလိုတဲ႔ အခါမ်ိဳးမွာ သံုးတယ္။ statement တစ္ေၾကာင္းထဲမွာ condition စစ္တဲ႔ အပိုင္းကိုအျပီးအစီး ထည့္ေရးခ်င္ရင္ သံုးတာေပါ့။ ကဲ… Selection Control Statement ေကာင္းေကာင္း ေရးတက္ေလာက္ျပီ ထင္တယ္ :) မရွင္းတာေတြ ရွိရင္ ျပန္ေမးၾကပါ ခင္ဗ်။

August 3, 2009

basic of C# (part 11)

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

Parameter passing in C#
Parameter passing ဆိုတာဘာလဲ အရင္ေျပာျပမယ္ေနာ္၊ ကြၽန္ေတာ္တို႔ေရးထားတဲ႔ function ေတြကိုျဖစ္ေစ၊ library ထဲမွာ ရွိျပီးသား predefined function ေတြကိုျဖစ္ေစ ေခၚသံုးေတာ့မယ္ဆိုရင္ အဲ့ဒီ function definition မွာ ေၾကျငာထားတဲ႔ argument(parameger) ေတြကို ထည့္ေပးဖို႔လိုအပ္ပါတယ္။ အဲ့လို function call မွာ parameter ေတြထည့္ေပးတာကို parameter passing လို႔ ေခၚတာပါ။ C# မွာ parameter passing ၄ မ်ိဳးရွိပါတယ္။

1. Value Parameter
2. Reference Parameter
3. Output Parameter နဲ႔
4. Params Array တို႔ ျဖစ္ပါတယ္။

1) Value Passing
C# မွာ default parameter passing က value pass ပါ။ ဆိုလိုတာကေတာ့ ကြၽန္ေတာ္တို႔ function ထဲကို parameter ေပးလိုက္တာက actual value ကို copy တစ္ခုအေနနဲ႔ ထည့္ေပးလိုက္တာပါ။ ဒါေၾကာင့္ function အတြင္းမွာ အဲ့ဒီ value ကို ဘယ္လိုပဲ ေျပာင္းသည္ျဖစ္ေစ၊ နဂို value ေတာ့ ေျပာင္းသြားမွာ မဟုတ္ပါဘူး။ ဥပမာ..

   using System;
   class valuePassTest
   {
        static void Main()
        {
            int num = 10;
            Console.WriteLine("before : " + num);
            Inc(num);
            Console.WriteLine("after : " + num);
            Console.Read();
        }
        static void Inc(int num)
        {
            num = num + 1;
        }
   }

output

before : 10
after : 10

2) Reference Passing
ကြၽန္ေတာ္က Inc function ထဲမွာ ေျပာင္းလိုက္တဲ႔ တန္ဖိုးကို main function ထဲမွာပါ လိုက္ျပီး ေျပာင္းခ်င္တယ္ဆိုရင္ေတာ့ reference pass လုပ္ေပးရပါမယ္။ reference pass လုပ္ဖို႔အတြက္ဆိုရင္ ref keyword ေလးကို parameter ရဲ႕ေရွ႕မွာ ထည့္ျပီး ေရးေပးရမွာပါ။ တခ်ိဳ႕ reference data type(class, array) ေတြကို parameter ပို႔ရင္ေတာ့ ref keyword မထည့္ေပးလည္း ရပါတယ္။

   using System;
   class valuePassTest
   {
        static void Main()
        {
            int num = 10;
            Console.WriteLine("before : " + num);
            Inc(ref num);
            Console.WriteLine("after : " + num);
            Console.Read();
        }
        static void Inc(ref int num)
        {
            num = num + 1;
        }
   }

output

before : 10
after : 11

3) output parameter
Output parameter က reference parameter နဲ႔ ဆင္တူပါတယ္။ ဒါေပမယ့္ output parameter ဆိုတဲ႔ အတိုင္း သူ႔ကို output return ျပန္ခ်င္တဲ႔ variable ေတြအတြက္ သံုးေလ႔ရွိပါတယ္။ function အတြင္းမွာ out variable ထဲကတန္ဖိုးကို assign မလုပ္ေသးခင္ ယူတြက္လို႔မရပါဘူး။

using System;
class outputPassTest
{
   static void Main()
   {
     int num = 10; int result;
     Square(out result, num);
     Console.WriteLine("result : " + result);
     Console.Read();
   }
   static void Square(out int result, int num)
   {
     result = num * num;
   }
}

output

result : 100

4) Params Array
Function call မွာ ထည့္ေပးလိုက္တဲ႔ value ေတြကို array တစ္ခုအေနနဲ႔ လက္ခံခ်င္တယ္ဆိုရင္ params keyword ကိုသံုးရပါတယ္။ example ေလးပဲ ၾကည့္လိုက္ေတာ့ေနာ္။

 using System;
 class paramsTest
 {
    static void Main()
    {
       Show(1, 3, 5, 7, 9);
       Console.Read();
    }
    static void Show(params int[] arr)
    {
       foreach (int x in arr)
       {
          Console.Write(x + " ");
       }
    }
 }

output

1 3 5 7 9

ဒီ example ေလးမွာ ကြၽန္ေတာ္ေပးလိုက္တဲ႔ parameter 5 လံုးကို array အေနနဲ႔ လက္ခံသြားတာကို ေတြ႔ရမွာပါ။

basic of SQL Statement (part 5)

Filed under: SQL Commands — Tags: , , — sevenlamp @ 2:15 PM

အခု ကြၽန္ေတာ္တို႔ Select quary ေတြမွာ count တို႔ sum တို႔ စသည့္ျဖင့္ function ေတြထည့္သံုးၾကည္ရေအာင္။ အဲ့ဒီ function ေတြကို aggregate function ေတြလို႔ ေခၚတယ္။ ဒီလို aggregate function ေတြပါတဲ႔ query ေတြကိုလည္း aggregate query လို႔ေခၚပါတယ္။

Aggregate Functions List

AVG MIN
CHECKSUM SUM
CHECKSUM_AGG STDEV
COUNT STDEVP
COUNT_BIG VAR
GROUPING VARP
MAX

ဒီ function ေတြကို ကြၽန္ေတာ္တို႔ Select ရဲ့ ေနာက္မွာ ျဖစ္ေစ၊ Having ရဲ့ေနာက္မွာ ျဖစ္ေစ၊ သံုးနိုင္ပါတယ္။ ဥပမာ အခုကြၽန္ေတာ္က ItemTable ထဲမွာ ရွိတဲ႔ record အေရအတြက္ကို သိခ်င္တယ္ဆိုပါစို႔။ ဒါဆိုရင္ Count ဆိုတဲ႔ aggregate function ကိုသံုးျပီး ခုလိုေရးရမွာပါ။

SELECT count(*) AS ItemCount FROM item

Result:

ItemCount
6

Count ဆိုတဲ႔ function က ကြၽန္ေတာ္တို႔ Table ထဲမွာ ရွိတဲ႔ record အေရအတြက္ကိုစစ္ေပးပါတယ္။ အဲ့ဒီ function ထဲမွာ * ဒါမွမဟုတ္ ၾကိဳက္ရာ column name ထည့္ေပးရပါမယ္။ ဒီ function ေတြကိုသံုးရင္ ထြက္လာတဲ႔ result မွာ column name မရွိပါဘူး။ ဒါေၾကာင့္ ကြၽန္ေတာ္က column name ထည့္ေပးဖို႔အတြက္ AS keyword ကိုသံုးပါတယ္၊ AS ေနာက္မွာ ကြၽန္ေတာ္ေပးခ်င္တဲ႔ ItemCount ဆိုတဲ႔ နံမည္ေလးထည့္ေပးလိုက္ပါတယ္။ တျခား Function ေတြကိုလည္း စမ္းၾကည့္ရေအာင္။ ကြၽန္ေတာ္အခု စုစုေပါင္းကုန္လက္က်န္ကို သိခ်င္တယ္ဆိုရင္။

SELECT sum(balance) AS TotalBalance FROM item

Result:

TotalBalance
35

အခုကြၽန္ေတာ္တို႔ aggregate function ေတြကိုသံုးတဲ႔အခါမွာ table ထဲက record အားလံုးအတြက္ result တစ္ခုထဲထြက္တာကို ေတြ႔ရလိမ့္မယ္။ ဟုတ္ျပီ ဒါဆိုရင္ အခုကြၽန္ေတာ္က Category တစ္ခုစီအတြက္ ကုန္လက္က်န္ကို ၾကည့္ခ်င္တယ္ဆိုရင္ေရာ။ ဒီလိုလုပ္ဖို႔ဆိုရင္ေတာ့ ကြၽန္ေတာ္တို႔ Group By keyword နဲ႔ တြဲသံုးေပးဖို႔လိုလာပါျပီ။

SELECT categoryid,sum(balance) AS TotalBalance
FROM item
GROUP BY categoryid

Result:

categoryID TotalBalance
1 16
2 15
3 4

*** မွတ္ထားစရာ အခ်က္တစ္ခ်က္ရွိေသးတယ္ဗ်။ select ထဲမွာ aggregate function ေတြ ပါျပီဆိုရင္၊ Group By လုပ္ထားတဲ႔ column ေတြကလြဲျပီး တျခား column ေတြကို ထည့္ေရးခြင့္ မရွိပါဘူး***
အခု Having keyword အေၾကာင္းဆက္ေျပာရေအာင္။ ကြၽန္ေတာ္တို႔ေရးတဲ႔ query မွာ condition စစ္စရာေတြ ရွိရင္ where ကိုသံုးရတယ္ဆိုတာ သိျပီးၾကျပီေနာ္။ ဟုတ္ျပီ အဲ့ဒီ where keyword က database ထဲမွာ ရွိတဲ႔ column name ေတြကိုပဲ စစ္လို႔ရပါတယ္။ တခါတေလကြၽန္ေတာ္တို႔က aggregate function ကထြက္လာတဲ႔ result ေတြကို စစ္ဖို႔လိုလာျပီဆိုရင္ေတာ့ Having ကိုသံုးရေတာ့မွာပါပဲ။ ဥပမာ ကုန္လက္က်န္ စုစုေပါင္း ၁၀ ခုထက္နည္းနဲ႔ category ကိုၾကည့္ခ်င္တယ္ဆိုရင္။

SELECT categoryid,sum(balance) AS TotalBalance
FROM item
GROUP BY categoryid
HAVING sum(balance) < 10

Result:

categoryID TotalBalance
3 4

Having ကို where လိုမ်ိဳးလဲ column ေတြစစ္ဖို႔အတြက္ သံုးလို႔လည္း ရပါတယ္။ category id 1 ရဲ႕ totalbalance ကိုျပဖို႔အတြက္

SELECT categoryid,sum(balance) AS TotalBalance
FROM item
GROUP BY categoryid
HAVING categoryid = 1

Result:

categoryID TotalBalance
1 16

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

Follow

Get every new post delivered to your Inbox.