Myanmar IT Resource Forum
Myanmar IT Resource Forum
Myanmar IT Resource Forum

You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Creating a function in C# [Tutorial] Empty Creating a function in C# [Tutorial] 17th May 2010, 9:28 am

soeminaung

soeminaung
MITR New User



ကၽြန္ေတာ္ ပထမ အပုဒ္မွာ မျပည့္စံုခဲ့တဲ့ ရွင္းလင္းခ်က္ေတြကို ထပ္ေရးပါမယ္။

  • Parsing String to Others
C#
မွာ ၀င္လာတဲ့ တန္ဖိုးကို label control လို အရာမ်ဳိးနဲ႔ တန္းၿပီး
ညီမွ်ခ်င္းခ်လို႔ရမပါဘူး။ ဒါက VB နဲ႔ မတူတဲ့အခ်က္ပါ။ VB မွာ အဲဒီလိုမ်ဳိး
assign လုပ္လို႔ ျပသနာမရိွပါဘူး။
ဂဏန္းတန္ဖိုးေတြကို label မွာ ေပၚေစခ်င္တယ္ဆိုရင္ တန္ဖိုးကို variable တစ္ခုထဲ ထည့္၊ ၿပီးမွ ToString() method နဲ႔ ရိုက္ထုတ္ရမွာပါ။ဥပမာ -

  • int FirstNumber;
  • int SecondNumber;
  • int Result;
  • FirstNumber=int.parse(txtFirstNumber.text);
  • SecondNumber=int.parse(txtSecondNumber).text;
  • Result=FirstNumber*SecondNumber;
  • lblAnswer.text=Result.ToString();ျဖစ္ပါတယ္။ ဒါကေတာ့ Visual Studio 2008 မွာ ဂဏန္းေပါင္းတဲ့ program ရဲ႕ ရွင္းလင္းခ်က္ျဖစ္ပါတယ္။
txtFirstNumber ဆိုတဲ့ control ထဲမွာ ရိုက္လိုက္လို႔၀င္လာမယ့္ အရာေတြကို စာေတြလို႔ .net ကေနမွတ္ယူပါတယ္။ Parsing မလုပ္ပဲ တန္းထည့္ရင္ error ျပပါလိမ့္မယ္။ ဒါေၾကာင့္ parse function ကိုသံုးၿပီး တန္ဖိုးကို ေျပာင္းပါတယ္။

တကယ္လို႔ int ကိုေျပာင္းခ်င္ရင္ေတာ့ int.parse() ပါ။ double ကိုေျပာင္းခ်င္တယ္ဆိုရင္ double.parse() လို႔ေရးရပါမယ္။ ေျမွာက္လို႔ရတဲ့အေျဖကို result variable ထဲကိုထည့္ပါတယ္။ အေျဖလိုခ်င္လို႔ label object ကေနေဖာ္ျပတဲ့အခါမွာက်ေတာ့ ကိန္းဂဏန္းနဲ႔ စာတြဲလို႔မရတဲ့အတြက္ toString() method နဲ႔ စာသားအျဖစ္ျပန္ေျပာင္းပါတယ္။ အဲဒီအခါက်ေတာ့မွ label ေပၚမွာ စာေပၚလာပါလိမ့္မယ္။

Create a new Function

အခုတစ္ခါ ေထာင့္မွန္စတုဂံတစ္ခုရဲ႕ ဧရိယာကို function တစ္ခုေဆာက္ၿပီး တြက္ယူပါမယ္။

partial class Rectangle : Form
{
int width;
int height;

public Rectangle()
{
width = 5;
height = 10;
}

public Rectangle(int w, int h)
{
this.width=w;
this.height=h;
}

public int DisplayArea(int rectangleWidth, int rectangleHeight)
{
return rectanglewidth * rectangleheight;
}

Rectangle rec = new Rectangle();
lblArea.Text="Area of Rectangle is" + rec.DisplayArea().toString();

Rectangle rec1 = new Rectangle(30,40);
lblArea.Text="Area of Rectangle is" + rec1.DisplayArea().toString();

}



Last edited by solakoko on 17th May 2010, 10:39 am; edited 1 time in total (Reason for editing : Tag)

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum

 

Free forum | ©phpBB | Free forum support | Report an abuse | Forumotion.com