Wednesday, March 9, 2011

Command Line Argument




හරි දැන් ඔයා දන්නවා console එකට output එකක් ගන්නේ කොහොමද කියන එක.අපි ඊලගට බලමු C# වල Command line argument එකක් මගින් input ගන්නේ කොහොමද කියන එක.

Sample code 




using System;

public class Hello3
{
   public static void Main(string[] args)
   {
      Console.WriteLine("You entered the following {0} command line arguments:", args.Length );   
      for (int i=0; i < args.Length; i++)
      {
         Console.WriteLine("args[{0}] = {1}", i, args[i]); 
      }
   }
}

            

                  අපේ කේතය ගැන දැන් මම පුංචි පැහැදිලි කිරීමක් කරන්නන්. මෙහි ඇති විෂේශත්වය වන්නේ කේතය run කරන අවස්ථාවේම ඊට input ලබාදීමයි.

line 5 :-
            අපේ Main method එකට args කියන string array එක pass කරල තියෙනවා. (Arrays ගැන ඉදිරියේදී විස්තරාත්මකව බලමු)අපි ලබාදෙන commandline arguments තියාගන්නේ මේ "args"කියන array එක තුලයි. args වෙනුවට අපිට කැමති නමක් දෙන්න පුළුවන්. නමුත් එය string type වීම අනිවාර්ය වේ. C# කියන්නේ case sensitive programing language එකක් කියන එකත් , මේ Array එකේ elemenets index වෙන්නේ බිංදුවේ සිට කියන එකත් මතක තියාගන්න.



line 7:-
             මේ line එකේ කරල තියෙන්නේ අපේ command line arguments count කරගැනීමක්. Length property එකෙන් වෙන්නේ අපේ array එකේ length එක ලබා ගැනීමයි. මේක read-only property එකක් කියන එකත් මතක තියා ගන්න. {0} එකෙන් වෙන්නේ කොමා (,) එකෙන් පස්සේ තියෙන variable එකේ value එක ගන්න කියන එකයි. curly bracket එක ඇතුලේ පිලිවෙලින් 0 සිට index වන පරිදි variables වල values ලබාගන්න පුළුවන්. 

 
line 10:-
            ඉතාම කෙටියෙන් කිව්වොත් මේ line එකෙන් කරන්නේ අපේ array එකේ element line by line print කරගන්න එකයි. මෙහිදී {0} කියන්නේ first number එක. {1} කියන්නේ second number එක

සැ:යු:   C හා C++ වලදී මෙන් මෙහිදී කේතයේ නම පලමු command line argument එක ලෙස නොගනී. මම දැන් කියල දෙන්නන් අපේ code එක build කරල run කරගන්නේ කොහොමද කියල. එය ආකාර දෙකකින් කරගන්න පුළුවන්.



Visual Studio භාවිතයෙන් කේතය build කර run කිරීම

step1 :-
            solution එක open කරගන්න.(cmd_Line.sln)


step2 :-
           Solution Explorer වල තිබෙන project එක right click කර          ලැබෙන මෙනුවෙන් Set As StartUp Project click කරන්න.

step3 :-
           Solution Explorer වල තිබෙන project එක right click කර ලැබෙන මෙනුවෙන් properties click කරන්න.  


step4 :-
            ලැබෙන dialog box එකෙන් Debug tab එක මත click කරන්න.

step5 :-
            ඉන්පසු අදාල පරිදි command line arguments ලබාදෙන්න.


step6 :-
          menu bar එකේ ඇති Debug මෙනුවේ "Start Without Debugging" click කරන්න.(Ctrl+F5)                    

             එසේ නැතිනම් ඉහත පලමු උදාහරණයේ කේතය compile කර run කරගත් ආකාරයටම visual studio coommand prompt එක මගින් මෙය compile කර run කරගත හැකිය.
 

                   මම මුලින්ම ඔයාගෙන් පුංචි ප්රශ්නයක් අහන්නන්!!! මම ඔයාට පැහැදිලි කරපු කලින් කේතය මගින් අපිට arithmatic operation කරගන්න පුළුවන්ද? බැහැ නේද. ඒකට හේතුව අපේ command line arguments store වෙන්නේ string array එකක.

                    එහෙම නං මේ string array elements වෙනත් data type එකකට convert කරගන්නේ කොහොමද කියල අපි බලමු.


                    ඔයාට C# වලදි string argument එකක් numeric type එකකට convert කරගන්න Convert කියන class එක හෝ Parse කියන method එක යොදාගන්න පුළුවන්. අපි දැන් බලමු string argument එකක් long number එකකට convert කරන්නේ කොහොමද කියල.



long num = Int64.Parse(args[0]);
                    ඉහත statement එකෙන් args string array එකේ පලවෙනි element එක Int64 class එකේ තියෙන Parse method එක මගින් long number එකක් බවට convert කරනවා. Int64 වෙනුවට long කියලත් අපිට යොදාගන්න පුළුවන්.



long num = long.Parse(args[0]);



එමෙන්ම Convert class සහ ToInt64 method මගින්ද මේ වැඩේ කරගන්න පුළුවන්.



long num = Convert.ToInt64(args[0]);



                    දැන් ඔයාට string type දත්තයක් numeric type එකකට convert කරගන්නේ කොහොමද කියල අවබෝධයක් ලැබෙන්න ඇති කියල මම හිතනවා. පහත කේතය මගින් තවදුරටත් ඒ අවබෝධය පුළුල් වේවි. එම කේතය යම් යම් වෙනස්කම් සිදු කරමින් ස්වයං අධ්යයනයක යෙදෙමින් කේත ලිවීමට උත්සාහ ගන්න.


Sample Code 2




// Sameera Madushan
// 2011-02-23
// argument1 20 91 57 101 200 3 1 16
// argument2 1 2 3 4 sameera

using System;
namespace cast
{
    class Program
    {
        static void Main(string[] args)
        {
            try
                {
                int i = 0;
                int oddNum = 0;
                int evenNum = 0;
                if (args.Length == 0)
                {
                    Console.WriteLine("You haven't given any arguments!");
                    Console.WriteLine("Please enter some numeric values");
                }
                else
                {
                    while (i < args.Length)
                    {
                        
                            if (int.Parse(args[i]) % 2 == 0) evenNum++;

                            else oddNum++;
                            i++;
                    }
                        
                }
           
                    Console.WriteLine("you have entered {0} command line arguments", args.Length);
                    Console.WriteLine("{0} odd numbers", oddNum);
                    Console.WriteLine("{0} even numbers", evenNum);
                }
            catch (FormatException)
            {
                Console.WriteLine("You have entered string characters");
                Console.WriteLine("Try Again");
            }
         }
    }
}


output 1:

                    මෙහිදී අපේ කේතය හරියාකරව execute වී අප ලබා දුන් ඔත්තේ හා ඉරට්ට සංඛ්යා නිවැරදිව count කර ගනී. අපි argument කිසිවක් ලබා නොදුන් විට එයද මෙම කේතයෙන් නිරීක්ෂණය කරගත හැක. "args.Length" property එක මගින් එය capture කරගනී.



                   Parse method එක මගින් string argument "Integer " type එකට convert කරගැනීමෙන් පසු ඉතිරි නැතිව දෙකෙන් බෙදේද යන්න පරීක්ෂා කිරීමෙන් ඔත්තේ හා ඉරට්ටේ සංඛ්යා count කරගනී. Arithmatic operations පිලිබද ඔබට යම් දැනුමක් තිබෙන බැවින් මේ කේතය අවබෝධ කරගැනීම එතරන් අපහසු නොවනු ඇත.


output 2: 
                   මෙහිදී ඔබට error message එකක් ලැබෙනු ඇත. string character එකක් "Integer" type එකට convert කල නොහැකි නිසා මෙය සිදු වේ. මෙම දෝෂය capture කරගැනීම try - catch මගින් සිදු වේ. JAVA පිලිබද යම් දැනුමක් තිබේනම් ඔබට මෙය එතරම් නුපුරුදු දෙයක් නොවනු ඇත. Error Handling පිලිබදව ඉදිරියේදී ගැඹුරින් සාකච්ඡා කරමු.

try-catch ඉවත් කර මෙම කේතය run කරන්න.එවිට අපේ code එක crash වීමක් සිදු වේ.




4 comments:

ඉන්දික ගුණතිලක said... Best Blogger Tips

ela machan niyami.......
digatama karagena yamu........

සමීර මධුෂාන් said... Best Blogger Tips

thnx kollo...............

Dhanushka.. said... Best Blogger Tips

good work machan..
hodata karagena yamu machan...

සමීර මධුෂාන් said... Best Blogger Tips

thnx pothaa.....

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Sameera Madushan | Bloggerized by Sameera - Premium Blogger Themes | Hosted Desktop