Wednesday, April 6, 2011

User inputs…

මුලින්ම, මගේ blog එකේ article කියවල මාව දිරිමත් කරපු හැම යළුවෙක්ටම ස්තූති කරනවා. මමත් කලින් C# කරලා නෑ.ඉගන ගන්න ගමන් තමයි මේ විදියට article ලියන්නේ. ඔයාට මගෙ ලිපියකින් පුංචි හරි දැනුමක් අලුතින් එකතු කර ගන්න ලැබුන නන් මගෙ මහන්සියේ ප්‍රථිඵල තියෙනව කියල සතු‍ටු වෙන්න පුලුවන්. හරි මචං ඉවරයි... අපි දැන් වැඩේට බහිමු.
මම මේ lesson එකෙන් කියල දෙන්න බලාපොරොත්තු වෙන්නේ console එක මගින් user input එකක් ලබාගන්නේ කොහොමද කියන එක ගැනයි. අපි කලින් lesson එකෙන් ඉගෙන ගත්තනෙ command line argument එකක් handle කරගන්නේ කොහොමද කියල. එහිදී කේතය execute කරගන්න අවස්ථාවේදීමයි inputs ලබා දීම කරන්නේ. අපි දැන් බලමු කේතය run කර ගත්තට පස්සේ console එකෙන් input එකක් ලබාගන්නේ කොහොමද කියලා.





මේ වැඩේ කරගන්න තියෙන  පහසුම විදිය වෙන්නේ System namespace එකේ තියෙන Console.ReadLine කියන method එක. code එකකින්ම වැඩේ විස්තර කරන්නන්.
using System;
 
namespace keyboard_input
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("~~~~~Multiplication Table~~~~~\n");
            Console.Write("Enter a number:-");
            string line = Console.ReadLine(); // Read string from console
            int value;
            if (int.TryParse(line, out value)) // Try to parse the string as an integer
            {
                int i = 0;
                while (i <= 12)
                {
                    Console.WriteLine("\t"+i+"*"+value+"\t= {0}",value*i);
                    // Multiply the integer and display it
                    i++;
                }
            }
            else
            {
                Console.WriteLine("Not an integer!");
            }
            Console.ReadLine();
        }
    }
}
          මෙහිදී අපි ලබාදෙන input read කරගන්නේ මම කලින් සදහන් කරපු Console.ReadLine කියන method එකෙන්. මේ method එක call කලාම අපි ලබාදුන් inputs ඒකෙන් return කරනවා. මේ method එකෙන් return කරන්නේ string value එකක්. ඒ හින්දා user inputs යොදාගෙන මොකක් හරි arithmatic operation එකක් කරගන්න ඕනෙ නන් අපි මේ return වෙන value එක integer එකකට convert කරගන්න ඕන.

ඒ සදහා මම කලින් lesson එකේ පැහැදිලි කරපු Parse method එක යොදාගන්න පුලුවන්. එමගින් ඔයා ලබාදෙන input එක numeric value එකකට convert කරගන්න පුලුවන්.නමුත් මම මෙහිදී යොදාගෙන තියෙන්නේ TryParse කියන method එක.
ඒකට පොඩි හේතුවක් තියෙනව.ඔයා input එකක් විදියට string character එකක් ලබා දුන්නොත් Parse method එකෙන් ඒක convert කරන්න යනකොට අපේ වැඩසටහන crash වෙනව.ඒක නැති කරගන්න තමයි මම TryParse method එක යොදාගත්තෙ. 
TryParse method එකට අපි parameters දෙකක් දෙන්න ඕන.පලවෙනි parameter එක විදියට console input එක method එකට parse කරනව. ඒ argument එක numeric type එකකට convert කරගන්න බැරි නන් false return කරගන්න තමයි දෙවෙනි parameter එක දෙන්නේ. දැන් ඔයාට තේරුනානෙ 13 line එකේ මම කරල තියෙන දේ.
එතකොට අපේ code එක crash වෙන් නැතුව error එක handle කරගන්න පුළුවන්. අපි string character එකක් දුන්නොත් else statement එක run වෙනව.දැන් ඔයා දන්නවනේ .net වල keyboard inputs ගන්නේ කොහොමද කියලා. ඔයත් තනියම coding කරල බලන්න.  comment එකක් දාල යන්න අමතක කරන්න එපා...

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

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