Transcript int

แนะนำภำษำ C#
01204111 Computer and Programming
แผนกำร
•
•
•
•
ทำไมต้องเรียนหลำยภำษำ
ควำมแตกต่ำงระหว่ำง Python และ C#
รูจ้ กั กับ C# แบบคร่ำว ๆ
โปรแกรมทีข่ บั เคลือ่ นด้วยเหตุกำรณ์
ทำไมต้องเรียนหลำยภำษำ
• ภำษำโปรแกรมทีม่ ใี ช้อยูใ่ นปจั จุบนั มีหลำกหลำย และมี
ลักษณะแตกต่ำงกันค่อนข้ำงมำก
• ไพธอนเป็ นภำษำทีง่ ำ่ ยในกำรเริม่ ต้น แต่วำ่ อ่อนตัวของภำษำ
ทำให้ภำษำค่อนข้ำงแตกต่ำงกับภำษำโปรแกรมอืน่ ๆ ทีน่ ิสติ
อำจได้ใช้งำนในอนำคต
• นอกจำกนี้ เรำได้เลือกภำษำ 2 ภำษำทีม่ รี ปู แบบไวยำกรณ์ท่ี
ค่อนข้ำงแตกต่ำงกัน เพือ่ ให้นิสติ ได้เห็นไวยำกรณ์ในกำรเขียน
โปรแกรมหลำยแบบ
โปรแกรมแรกบน C#
using System;
โปรแกรมนี ้
ทำอะไร?
class Program
{
public static void Main(string[] args)
{
Random r = new Random();
int answer = 1 + (r.Next() % 100);
int g;
do {
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine());
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
}
using System;
ผ่ำตัดโปรแกรม
class Program
{
public static void Main(string[] args)
{
Random r = new Random();
int answer = 1 + (r.Next() % 100);
int g;
do {
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine());
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
}
โปรแกรมหลักอยู่ใน
ฟั งก์ ชัน (เมท็อด) ชื่อ
Main
using System;
ผ่ำตัดโปรแกรม
class Program
{
public static void Main(string[] args)
{
Random r = new Random();
int answer = 1 + (r.Next() % 100);
int g;
do {
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine());
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
}
ใช้ ค่ ู { } ในกำร
เปิ ด-ปิ ดขอบเขต
บล็อก
ผ่
ำ
ตั
ด
เมท็
อ
ด
Main
public static void Main(string[] args)
{
Random r = new Random();
int answer = 1 + (r.Next() % 100);
int g;
do {
สุ่มคำตอบ
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine()); ระหว่ ำง 1 - 100
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
ผ่
ำ
ตั
ด
เมท็
อ
ด
Main
public static void Main(string[] args)
{
Random r = new Random();
int answer = 1 + (r.Next() % 100);
int g;
do {
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine());
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
ทำซำ้ ไปเรื่ อย ๆ ถ้ ำ
g != answer
ผ่
ำ
ตั
ด
เมท็
อ
ด
Main
public static void Main(string[] args)
{
พิมพ์ ข้อควำม
และอ่ ำนค่ ำจำนวนเต็ม
Random r = new Random();
int answer = 1 + (r.Next() % 100); เก็บในตัวแปร g
int g;
do {
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine());
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
ผ่
ำ
ตั
ด
เมท็
อ
ด
Main
public static void Main(string[] args)
{
Random r = new Random();
int answer = 1 + (r.Next() % 100);
int g;
do {
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine());
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
เปรี ยบเทียบ
g กับ answer
แล้ วพิมพ์ คำใบ้
ผ่
ำ
ตั
ด
เมท็
อ
ด
Main
public static void Main(string[] args)
{
Random r = new Random();
int answer = 1 + (r.Next() % 100);
int g;
do {
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine());พิมพ์ ว่ำตอบถูกต้ อง
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
ผ่ำตัดโปรแกรม: กรอบภำยนอก
using System;
class Program
{
public static void Main(string[] args)
{
Random r = new Random();
int answer = 1 + (r.Next() % 100);
int g;
do {
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine());
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
}
ระบุให้ ใช้ ชุดคำสั่ง
มำตรฐำนในคลัง
using System;
ผ่ำตัดโปรแกรม
class Program
{
public static void Main(string[] args)
{
Random r = new Random();
int answer = 1 + (r.Next() % 100);
int g;
do {
Console.Write("Guess a number: ");
g = int.Parse(Console.ReadLine());
if(g > answer)
Console.WriteLine("Too large");
else if(g < answer)
Console.WriteLine("Too small");
} while(g != answer);
Console.WriteLine("That's correct");
}
}
ทุก ๆ เมท็อด จะต้ อง
อยู่ในคลำส (class)
กำรโปรแกรมต่ำงภำษำ
สวัสดีครับ
ขอข้ ำวผัดหนึ่งจำน
ขอนำ้ เปล่ ำด้ วย
ครับ
Hello,
May I have a
fried rice,
and water
please?
ในการเขียนโปรแกรมเดียวกัน เมือ
่ เปลีย
่ นภาษา โดยมากแลว
้
แนวทางการคิดและเรียบเรียงขัน
้ ตอนในการเขียนโปรแกรมจะไม่
ตางกั
น สิ่ งทีแ
่ ตกตางคื
อรูปแบบการเขียนเทานั
่
่
่ ้น
โครงสร้ำงของโปรแกรมภำษำ C#
using System;
namespace Sample
{
class Program
{
static void Main()
{
Console.WriteLine("Hello, world");
}
}
}
ภาษา C# จะใช้วงเล็บปี ก
กา { } ในการระบุ
ขอบเขตตาง
ๆ
่
โปรแกรมดานซ
้
้ายมีการ
ระบุขอบเขตของ:
1. namespace Sample
2. class Program
3. method Main
โครงสร้ำงของโปรแกรมภำษำ C#
using System;
namespace Sample
{
class Program
{
static void Main()
{
Console.WriteLine("Hello, world");
}
}
}
คาสั่ งในโปรแกรมภาษา
C# จะไมอยู
ๆ
่ ลอย
่
คำสั่งจะต้ องอยู่ในเมท็อด
เมท็อดจะต้ องอยู่ในคลำส
คลำสอาจจะอยู่ในเนมสเปส
เรำอำจจะไม่ ใช้ เนมสเปสก็ได้
จักรวำลของโปรแกรม
Namespace System
Class X
Class Console
WriteLine
Write
Class Float
Namespace
Sample
(เนมสเปสของ
โปรแกรมเรำ)
ReadLine
Class Integer
Parse
Class Random
Next
ToString
Class Y
Class
Program
Main
using System
• กำรสัง่ using System; ในตอนต้นโปรแกรมเป็ นกำรบอกว่ำเรำ
จะใช้โปรแกรมจำกเนมสเปส System เหมือนเป็ นของเรำเอง
using System;
namespace Sample
{
class Program
{
……
}
}
Namespace System
Class Console
WriteLine
Write
Class Float
Namespace
Sample
(เนมสเปสของ
โปรแกรมเรำ)
ReadLine
Class Integer
Parse
ToString
Class Random
Next
Class
Program
Main
โปรแกรมหลัก: เมท็อด Main
using System;
namespace Sample
{
class Program
{
static void Main()
{
เราจะเขียน
โปรแกรมหลักตรงนี้
}
}
}
โปรแกรมจะเริม
่
ทางานทีเ่ มท็อด
Main เสมอ
ขอละไว้
• คลำส (class) คืออะไร?
• แล้ว static void คืออะไร?
เรำขอละคำตอบของคำถำมเหล่ ำนีไ้ ว้
ในตอนนีใ้ ห้ นิสิตใช้ ตำมที่ระบุไปก่ อน
เมื่อถึงเวลำแล้ วเรำจะอธิบำยควำมหมำย
ของข้ อควำมเหล่ ำนีอ้ ีกทีหนึ่ง
ตัวอย่ำง: คำนวณระยะทำง
• วัตถุเคลือ่ นทีเ่ ป็ นเส้นตรง จำกสภำวะหยุดนิง่ ด้วย
ควำมเร่ง a เมตร/วินำที2 เป็ นเวลำ t วินำที
• วัตถุจะอยูห่ ำ่ งจำกจุดเริม่ ต้นกีเ่ มตร
กำรพัฒนำโปรแกรมภำษำ C#
• เรำจะใช้เครือ่ งมือทีเ่ รียกรวม ๆ ว่ำ Integrated
Development Environment หรือ IDE
–มีสว่ นสำหรับป้อน/แก้ไข ทดลอง และตรวจสอบโปรแกรม
–คล้ำย ๆ กับ WingIDE
• ในขัน้ แรกเรำจะสร้ำงโปรแกรมทีเ่ ป็ นตัวอักษรก่อน
(Console Application)
เริ่มต้นเขียนโปรแกรม
• เรำจะใช้โปรแกรม Sharp Develop ในกำรพัฒนำโปรแกรมภำษำ C#
• นิสติ สำมำรถใช้ Visual Studio Express ได้เช่นเดียวกัน
สร้ำง Solution
• จะเรียกโปรแกรมหนึ่ง ๆ
ว่ำ Solution
• สัง่ New Solution
– เลือก Console Application
– พิมพ์ช่อื solution
• เมือ่ สร้ำงเสร็จระบบจะเขียน
โครงของโปรแกรมให้โดย
อัตโนมัติ
หน้ ำจอ IDE
• ส่วนสำหรับพิมพ์และแก้ไข
โปรแกรม (editor) อยูต่ รง
กลำงหน้ำจอ
• ส่วนอื่น ๆ แสดงรำยละเอียด
เกีย่ วกับโครงงำนและ
โครงสร้ำงของโปรแกรม
using System;
namespace mecha1
{
class Program
{
public static void Main(string[] args)
{
double a, t, s;
โปรแกรมแรก
Console.Write("Enter a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Enter t: ");
t = double.Parse(Console.ReadLine());
s = a*t*t/2.0;
Console.WriteLine("Distance = {0}", s);
}
}
}
ลองสังเกตสิ่งที่เหมือน
และแตกต่ ำงของ
โปรแกรมเมื่อเทียบกับ
โปรแกรมภำษำไพธอน
using System;
namespace mecha1
{
class Program
{
public static void Main(string[] args)
{
double a, t, s;
โปรแกรมแรก
Console.Write("Enter a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Enter t: ");
t = double.Parse(Console.ReadLine());
s = a*t*t/2.0;
Console.WriteLine("Distance = {0}", s);
}
}
}
ทุก ๆ คำสั่งต้ องจบด้ วย
เครื่ องหมำย ;
(semi-colon)
using System;
namespace mecha1
{
class Program
{
public static void Main(string[] args)
{
double a, t, s;
โปรแกรมแรก
Console.Write("Enter a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Enter t: ");
t = double.Parse(Console.ReadLine());
s = a*t*t/2.0;
Console.WriteLine("Distance = {0}", s);
}
}
}
ก่ อนใช้ ตัวแปรใด ๆ
จะต้ องมีกำรประกำศก่ อน
และต้ องระบุแบบชนิด
(type) ข้ อมูลที่ตัวแปร
นัน้ เก็บด้ วย
ในทีน่ ี ้ double คือชนิ ดข้อมูลที ่
เก็บจำนวนจริ ง
using System;
namespace mecha1
{
class Program
{
public static void Main(string[] args)
{
double a, t, s;
โปรแกรมแรก
Console.Write("Enter a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Enter t: ");
t = double.Parse(Console.ReadLine());
s = a*t*t/2.0;
Console.WriteLine("Distance = {0}", s);
}
}
}
คำนวณผลลัพธ์ เก็บในตัว
แปร s
using System;
namespace mecha1
{
class Program
{
public static void Main(string[] args)
{
double a, t, s;
โปรแกรมแรก
Console.Write("Enter a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Enter t: ");
t = double.Parse(Console.ReadLine());
s = a*t*t/2.0;
Console.WriteLine("Distance = {0}", s);
}
}
}
คำสั่งแสดงผล
using System;
namespace mecha1
{
class Program
{
public static void Main(string[] args)
{
double a, t, s;
โปรแกรมแรก
Console.Write("Enter a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Enter t: ");
t = double.Parse(Console.ReadLine());
s = a*t*t/2.0;
Console.WriteLine("Distance = {0}", s);
}
}
}
อ่ ำนข้ อมูลเป็ นสตริง
using System;
namespace mecha1
{
class Program
{
public static void Main(string[] args)
{
double a, t, s;
โปรแกรมแรก
Console.Write("Enter a: ");
a = double.Parse(Console.ReadLine());
Console.Write("Enter t: ");
t = double.Parse(Console.ReadLine());
s = a*t*t/2.0;
Console.WriteLine("Distance = {0}", s);
}
}
}
แปลงสตริง
เป็ นจำนวนจริง
(double)
ควำมแตกต่ำง: ใช้ { } ในกำรระบุบล็อค
Python
def main():
n = int(input())
i = 1
while i <= n:
if i % 3 == 0:
print(i)
Python ใช้
กำรย่ อหน้ ำ
ใช้ กรอบของวงเล็บปี ก
กำในกำรระบุแทน
C#
//
// ละส่วนอื่นของโปรแกรมไว้
//
static void Main()
{
int n = int.Parse(Console.ReadLine());
int i = 1;
while(i <= n)
{
if(i % 3 == 0)
{
Console.WriteLine(i);
}
}
}
ควำมแตกต่ำง: ใช้ { } ในกำรระบุบล็อค
C#
//
// ละส่วนอื่นของโปรแกรมไว้
//
static void Main()
{
int
int
n =
n =
int.Parse(Console.ReadLine());
int.Parse(Console.ReadLine());
int
int
i =
i =
1;1;
while(i
while(i
<=<=
n)n)
{ {
if(iif(i
% 3 %
==30)
== 0)
{
{
Console.WriteLine(i);
Console.WriteLine(i);
}
}
} }
}
ยอหน
างานของโปรแกรม
่
้ าไมมี
่ ความหมายใด ๆ ตอการท
่
ใส่ไวเพื
่ ให้โปรแกรมอานง
ายเท
านั
้ อ
่
่
่ ้น
ควำมแตกต่ำง: ใช้ { } ในกำรระบุบล็อค
C#
//
// ละส่วนอื่นของโปรแกรมไว้
//
static void Main()
{
int nn == int.Parse(Console.ReadLine());
int.Parse(Console.ReadLine());
int
int ii == 1;
1;
int
while(i <=
<= n)
n)
while(i
{if(i % 3 == 0)
if(i
% 3 == 0)
Console.WriteLine(i);
}
{
Console.WriteLine(i);
}
}
}
สาหรับคาสั่ งควบคุม เช่น while หรือ if ถาไม
ใส
้
่ ่ { } จะ
ถือวาค
มคาสั่ งถัดไปหนึ่งคาสั่ งเทานั
่ าสั่ งดังกลาวควบคุ
่
่ ้น
ควำมแตกต่ำง: ต้องประกำศตัวแปร
Python
n = int(input())
total = 0
i = 0
while i < n:
x = int(input())
total += x
i += 1
print(total)
C#
//
// ละส่วนอื่นของโปรแกรมไว้
//
static void Main()
{
int n = int.Parse(Console.ReadLine());
int total = 0;
int i = 0;
int x;
while(i < n)
{
x = int.Parse(Console.ReadLine());
total += x;
i += 1;
}
}
กำรประกำศตัวแปร
• รูปแบบ:
ตัวเดียว
หลำยตัว
แบบชนิดข้อมูล ชือ่ ตัวแปร;
แบบชนิดข้อมูล ชือ่ ตัวแปร [, ชือ่ ตัวแปร, ชือ่ ตัวแปร..];
• ตัวอย่ำง:
double area;
int radius, counter;
bool isOkay;
• เรำสำมำรถกำหนดค่ำเริม่ ต้นให้กบั ตัวแปรได้ทนั ที
ตัวอย่ำง: int k = 200;
bool done = false;
แบบชนิดข้อมูลพืน้ ฐำนใน C#
Type
Size
Description
bool
char
byte
1 byte
1 byte
1 byte
Store truth value
Store one character
Store positive integer
short
int
2 byte
4 byte
Store integer
Store integer
long
8 byte Store integer
double 16 byte Store real number
string
N/A
Store sequence of
characters
Range
true / false
character code 0 – 255
0 – 255
-32,768 -- 32,767
-2.1 x 109 -- 2.1 x 109
-9.2 x 1018 -- 9.2 x 1018
± 5.0x10-324 -± 1.7x10308
N/A
หำผลรวม
• เขียนโปรแกรมรับรำยกำรของจำนวนเต็มบวกจำกผูใ้ ช้
จนกระทังผู
่ ใ้ ช้ป้อน -1 จำกนัน้ ให้แสดงผลรวม
หำผลรวม: Python
total = 0
num = 0
while num != -1:
num = int(input())
if num != -1:
total += num
print(total)
หำผลรวม: C#
• โปรแกรมหำผลรวม ในภำษำ C#
static void Main()
{
int total = 0;
int num = 0;
while(num != -1)
{
num = int.Parse(Console.ReadLine());
if(num != -1)
total += num;
}
Console.WriteLine(total);
}
total = 0
num = 0
while num != -1:
num = int(input())
if num != -1:
total += num
print(total)
สรุปควำมแตกต่ำง C# และ Python (1)
• ใช้ { } ในกำรระบุ block
• กำรใช้ตวั แปร
– ต้องประกำศตัวแปรโดยระบุ
แบบชนิด (type) ก่อนใช้เสมอ
– ตัวแปรหนึ่งตัวเก็บข้อมูลได้แบบ
ชนิดเดียว
• โดยทัวไปท
่ ำงำนได้เร็วกว่ำ
• ใช้ยอ่ หน้ำในกำรระบุ block
• กำรใช้ตวั แปร
– สำมำรถใช้ตวั แปรได้เลย
– ตัวแปรหนี่งตัวจะเก็บข้อมูลแบบ
ชนิดใดก็ได้ และสำมำรถ
เปลีย่ นไปมำได้ในโปรแกรม
• ทำงำนช้ำกว่ำ
รำคำและผลตอบแทน
• ในภำษำ C# เรำสูญเสียควำมคล่องตัวไปในหลำย ๆ
เรือ่ ง แต่นนก็
ั ่ ทำให้ระบบสำมำรถตรวจจับข้อผิดพลำด
ของโปรแกรมได้มำกขึน้
ตัวอย่ำงโปรแกรมภำษำไพธอน
x = int(input("Enter price: "))
if x <= 10:
print("You have to pay",x,"baht.")
else:
print("You have to pay",y*0.95,"baht.")
Enter price: 7
You have to pay 7 baht.
เห็นที่ผิดหรื อไม่ ?
เรำจะพบที่ผิดลักษณะนี ้
ในไพธอน
Enter price: 20
Traceback (most recent call last): เมื่อตอนโปรแกรมทำงำนเท่ ำนัน
้
File "<string>", line 5, in <fragment>
builtins.NameError: name 'y' is not defined
ตัวอย่ำงส่วนของโปรแกรมภำษำ C#
public static void Main(string[] args)
{
double x;
Console.Write("Enter price: ");
x = double.Parse(Console.ReadLine());
เห็นที่ผิดหรื อไม่ ?
if(x <= 10)
Console.WriteLine("You have to pay {0} baht.", x);
else
Console.WriteLine("You have to pay {0} baht.", y*0.95);
}
ใน C# โปรแกรมนีจ้ ะไม่ ผ่ำนกำรตรวจสอบ ระบบจะแจ้ งว่ ำ
The name 'y' does not exist in the current context.
ตัวอย่ำงโปรแกรมภำษำไพธอน
x = input("Enter price: ")
if x <= 10:
print("You have to pay",x,"baht.")
else:
print("You have to pay",x*0.95,"baht.")
เห็นที่ผิดหรื อไม่ ?
เกิดปั ญหำเนื่องจำกเรำพยำยำมจะเปรียบเทียบสตริงกับจำนวนเต็ม
Traceback (most recent call last):
เช่ นเดียวกับตัวอย่ ำงก่ อน
File "<string>", line 2, in <fragment>
่ ผิดลักษณะนี ้
builtins.TypeError: unorderable types:เรำจะพบที
str() <= int()
เมื่อตอนโปรแกรมทำงำนเท่ ำนัน้
ตัวอย่ำงส่วนของโปรแกรมภำษำ C#
public static void Main(string[] args)
{
String x;
Console.Write("Enter price: ");
x = Console.ReadLine();
เห็นที่ผิดหรื อไม่ ?
if(x <= 10)
Console.WriteLine("You have to pay {0} baht.", x);
else
Console.WriteLine("You have to pay {0} baht.", x*0.95);
}
ใน C# โปรแกรมนีจ้ ะไม่ ผ่ำนกำรตรวจสอบ ระบบจะแจ้ งข้ อผิดพลำด 2 ข้ อ คือ
1. Operator '<=' cannot be applied to operands of
type 'string' and 'int'.
2. Operator '*' cannot be applied to operands of
type 'string' and 'double'
ทำงเลือกของภำษำโปรแกรม
เข้ มงวด
อิสระ
เพื่อให้ สำมำรถพบควำม
ผิดพลำดที่ไม่ ได้ จงใจได้ ง่ำย
ก่ อนที่โปรแกรมจะทำงำน
เพื่อให้ พฒ
ั นำโปรแกรมได้
ง่ ำย รวดเร็ว และมีควำม
คล่ องตัวในกำรเขียน
โปรแกรมยำวขึน้ สร้ ำงควำม
ลำบำกระหว่ ำงพัฒนำ
กำรเขียนโปรแกรมมีข้อจำกัด
มำกขึน้
อำจพบข้ อผิดพลำดเมื่อ
โปรแกรมทำงำน
อำจเกิดปั ญหำเวลำโปรแกรมมี
ขนำดใหญ่ มำก
ทำงเลือกของวิชำนี้
• ในวิชำนี้เรำได้เลือกใช้ภำษำไพธอนทีง่ ำ่ ยในกำรเริม่ ต้น
• จำกนัน้ ในครึง่ หลังเรำได้เลือกใช้ภำษำ C# ทีเ่ พิม่
ข้อจำกัด ซึง่ จะทำให้เรำเขียนโปรแกรมได้มรี ะเบียบและ
มีวนิ ยั มำกขึน้
• ทัง้ หมดนี้เพือ่ ให้นิสติ ได้มคี วำมเข้ำใจในแนวคิดพืน้ ฐำน
ของกำรโปรแกรม โดยไม่ยดึ ติดกับรำยละเอียด
เฉพำะเจำะจงกับภำษำใดภำษำหนึ่ง
ทำงเลือกของคุณ
• มีภำษำโปรแกรมอีกจำนวน
มำกมำย
• อย่ำลืมว่ำสิง่ ทีส่ ำคัญทีส่ ดุ คือ
กำรเลือกเครือ่ งมือให้เหมำะ
กับงำน
กำรนำโปรแกรมไปทำงำน: ทบทวน
• เรำเขียนโปรแกรมด้วยภำษำระดับสูง
• แต่หน่วยประมวลผลเข้ำใจเฉพำะภำษำเครือ่ งเท่ำนัน้
• จะต้องมีกระบวนกำรแปลงโปรแกรมทีเ่ รำเขียนให้อยูใ่ น
รูปทีท่ ำงำนได้ในคอมพิวเตอร์
กำรนำโปรแกรม C# ไปทำงำน
โปรแกรมภำษำ
C#
คอมไพเลอร์ ภำษำ
C#
โปรแกรมที่ทำงำน
ได้
• โปรแกรมภำษำ C# ทีเ่ ขียนขึน้ จะถูกคอมไพล์ (compile) ให้
อยูใ่ นรูปของโปรแกรมทีพ่ ร้อมทำงำนได้บนคอมพิวเตอร์
• อย่ำงไรก็ตำม คอมพิวเตอร์ดงั กล่ำวจะต้องติดตัง้ ระบบ
ซอฟต์แวร์โครงสร้ำงพืน้ ฐำนบำงอย่ำงไว้ก่อน
โปรแกรมประยุกต์แบบกรำฟิกส์
• ระบบพัฒนำโปรแกรมของ C# ทำให้กำรพัฒนำ
โปรแกรมประยุกต์ทต่ี ดิ ต่อกับผูใ้ ช้แบบกรำฟิกส์สะดวก
ขึน้ มำก
• โปรแกรมกลุม่ ดังกล่ำวรวมเรียกว่ำโปรแกรมทีม่ ี GUI
(graphics user interface) หรือเรียกสัน้ ๆ ว่ำโปรแกรม
GUI
• เรำจะทดลองสร้ำงโปรแกรมง่ำย ๆ กัน
ลักษณะทัวไปของโปรแกรม
่
GUI
แกช
้ ่ องนี้
คลิก
๊ ตรงนี้
คลิก
๊ ตรงนี้
คลิก
๊ ตรงนี้
คลิก
๊ ส่วนนี้
คลิก
๊ ส่วนนี้
แกช
้ ่ องนี้
• ในกำรใช้งำน ผูใ้ ช้สำมำรถทำงำนกับโปรแกรมได้หลำยแบบ
• โปรแกรมลักษณะนี้มกั มีลำดับกำรทำงำนทีไ่ ม่แน่นอน เมือ่
เทียบกับโปรแกรมทีเ่ ขียนบน Console
กำรเขียนโปรแกรม GUI (1)
• พิจำรณำโปรแกรมทีอ่ ่ำนข้อมูล
หลำยตัวคล้ำย ๆ กัน
int w = int.Parse(Console.ReadLine());
int h = int.Parse(Console.ReadLine());
Console.Write("Choose unit: ");
string u = Console.ReadLine();
Console.Write("Choose background:");
string b = Console.ReadLine();
• เนื่องจำกลักษณะกำรทำงำน
ดังกล่ำว ทำให้กำรเขียนโปรแกรม
รูปแบบเดิมทีม่ ลี กั ษณะกำรทำงำน
เป็ นแบบเส้นตรงไม่คอ่ ยเหมำะสม
กับกำรพัฒนำโปรแกรม GUI
เท่ำใดนัก
กำรเขียนโปรแกรม GUI (2)
• เนื่องจำกกำรติดต่อกับผูใ้ ช้ของโปรแกรม GUI มีได้
หลำกหลำย กำรเขียนโปรแกรมจึงเปลีย่ นไปเป็ นแบบกำร
เขียนโปรแกรมกับเหตุกำรณ์ (event-driven) แทน
คลิ๊กตรงนี ้แล้ วจะทำอะไร?
คลิ๊กตรงนี ้แล้ วจะทำอะไร?
คลิ๊กตรงนี ้แล้ วจะทำอะไร?
แก้ ค่ำในนี ้แล้ วจะทำอะไร?
แก้ ค่ำในนี ้แล้ วจะทำอะไร?
กำรโปรแกรมกับเหตุกำรณ์
• เรำจะเขียนโปรแกรมสำหรับตอบสนองกับเหตุกำรณ์
• เมือ่ เหตุกำรณ์ทเ่ี รำสนใจเกิดขึน้ โปรแกรมในส่วนนัน้ จะถูก
เรียกทำงำน
• พิจำรณำโปรแกรมด้ำนล่ำง
โปรแกรมดังกลาวตอบสนองเหตุ
การณ ์
่
กดปุ่มดังนี้
ถ้ ำมีกำรกดปุ่ มนี ้:
ให้ เปลี่ยนข้ อควำมด้ ำนบนให้
เป็ นคำว่ำ "Sawaddee"
สร้ำง solution
• เลือก new solution
• เลือก Windows Application
• พิมพ์ชอ่ื solution
ส่วนออกแบบหน้ ำจอ
• ถ้ำเรำเลือกพัฒนำโปรแกรมประยุกต์แบบวินโดวส์แล้ว
• เรำจะสำมำรถเลือกแท็บ Design เพือ่ เข้ำสูส่ ว่ น
ออกแบบหน้ำจอติดต่อกับผูใ้ ช้ได้
เลือก Design
เลือก Tools
โปรแกรม Hello, world
จำกแถบ Tools เรำสำมำร
เลือกนำปุ่ มและป้ำยข้ อควำม
มำวำงบนหน้ ำต่ ำงพร้ อมทัง้
กำหนดคุณสมบัตใิ ห้ วัตถุแต่
ละอันได้ ผ่ำนทำงส่ วน
Properties
ระวังอย่ ำกด double-click ถ้ ำกดแล้ วหน้ ำจอเปลี่ยนเป็ น
โปรแกรมให้ กดแท็บ Design เพื่อกลับมำหน้ ำเดิม
วัตถุบนหน้ ำต่ำง
• สำหรับตัวอย่ำงนี้เรำได้วำงวัตถุไป 2 อัน
• ตัว IDE ได้กำหนดชือ่ เริม่ ต้นให้กบั วัตถุต่ำง ๆ ดังนี้
label1
button1
• เรำสำมำรถเปลีย่ นชือ่ ได้ แต่ในตัวอย่ำงนี้จะใช้ชอ่ื นี้ไปก่อน
วัตถุและคุณสมบัติ
• วัตถุบนหน้ำต่ำงแต่ละอันจะมีคุณสมบัติ
ทีเ่ รำสำมำรถปรับเปลีย่ นได้ ยกตัวอย่ำง
เช่น วัตถุ button1 มีรำยกำรคุณสมบัติ
บำงส่วนดังแสดงด้ำนขวำ
คุณสมบัติ
• เรำสำมำรถปรับแต่งฟอนต์ สี หรือข้อควำมของวัตถุบน
หน้ำต่ำงได้จำกแถบ Properties
เปลี่ยนฟอนต์โดยปรับคุณสมบัติ Font
เปลี่ยนข้ อควำมบนปุ่ มโดยปรับคุณสมบัติ Text
เริ่มโปรแกรม
• เรำจะเขียนโปรแกรมให้แสดงข้อควำมว่ำ Hello, world
ที่ label1 เมือ่ ผูใ้ ช้กดปุม่ button1
–เรำจะเขียนโปรแกรมทีจ่ ะทำงำนเมือ่ ผูใ้ ช้กดปุม่ ดังกล่ำว
–ให้กด double click ทีป่ มุ่ button1
–รูปหน้ำต่ำงจะหำยไป หน้ำจอ IDE จะเปลีย่ นมำเป็ นส่วนให้
พิมพ์โปรแกรม
เริ่มเขียนโปรแกรม
เรำจะเขียนโปรแกรมในเมท็อด
Button1Click นี ้
โปรแกรมตอบสนองเหตุกำรณ์
• เรำจะเขียนเมท็อดเพือ่ ตอบสนองเหตุกำรณ์ดงั ด้ำนล่ำง
void Button1Click(object sender, EventArgs e)
{
label1.Text = "Sawaddee";
}
เรียกใช้
Button1Click
Click
เปลี่ยนข้ อควำมบน label1 เป็ น
"Sawaddee"
กำรเชื่อมโยงเหตุกำรณ์กบั เมท็อด
• เรำจะกำหนดเมท็อด
ให้กบั เหตุกำรณ์ Click
ได้จำกหน้ำ Properties
• อย่ำงไรก็ตำม เมือ่ เรำ
กด double click ทีว่ ตั ถุ
เช่น ปุม่ IDE จะสร้ำง
เมท็อดสำหรับ
เหตุกำรณ์ Click ให้โดย
อัตโนมัติ
โปรแกรมบวกเลข
• เรำจะเขียนโปรแกรมรับจำนวนเต็ม 2 จำนวน แล้ว
แสดงผลบวก
• เรำจะเริม่ โดยกำรออกแบบส่วนติดต่อกับผูใ้ ช้กอ่ น ซึง่ จะ
ประกอบด้วย ปุม่ (button) กล่องข้อควำม (text box)
และป้ำยข้อควำม (label)
ออกแบบหน้ ำจอ
• สร้ำง Solution ใหม่ เลือก Windows Application
• แล้วลำกวัตถุมำวำงบนหน้ำต่ำงดังนี้
text box
label
text box
button
ชื่อของตัวแปรอ้ำงอิงถึงวัตถุ
• IDE จะกำหนดชือ่ ของตัวแปรทีอ่ ำ้ งอิงถึงวัตถุทเ่ี รำลำก
มำวำงให้โดยอัตโนมัติ ดังนี้
textBox1
label1
textBox2
button1
ปรับชื่อให้สื่อควำมหมำย
• อย่ำงไรก็ตำม ถ้ำเรำใช้ชอ่ื ตัวแปรตำมทีก่ ำหนดให้
โปรแกรมเรำจะอ่ำนยำกมำก
• ปรับชือ่ ของวัตถุให้เป็ นตำมรำยกำรด้ำนล่ำงนี้
xTextBox
resultLabel
yTextBox
ให้ ระวังตัวพิมพ์ เล็ก/พิมพ์ ใหญ่ ด้วย
addButton
ปรับชื่อวัตถุ
ปรับชื่อโดยแก้ คณ
ุ สมบัติ Name
ชื่อที่กำหนดเหล่ำนี ้จะเป็ นสิ่งที่เรำจะใช้ เพื่อ
อ้ ำงถึงวัตถุและข้ อมูลของนันในโปรแกรม
้
จัดกำรกับกำรกดปุ่ ม
• Double click ทีป่ มุ่ จำกนัน้ เขียนเมท็อดดังนี้
void AddButtonClick(object sender, EventArgs e)
{
int x = int.Parse(xTextBox.Text);
int y = int.Parse(yTextBox.Text);
int result = x + y;
resultLabel.Text = result.ToString();
}
มุมนักคิด
• ถ้ำเรำต้องกำรเพิม่ ปุม่ อีกปุม่ หนึ่งให้นำจำนวนเต็มทัง้
สองมำคูณกัน จะมีวธิ กี ำรอย่ำงไร?