SlideShare a Scribd company logo
1 of 3
Download to read offline
VB .NET 2005 By Warawut Khangkhan
การใชตวแปรแบบคงท&ใน VB.NET 2005 express Edition
                  '



                 การใชตวแปรแบบคงทใน VB .NET 2005 Express Edition
        ในการเข&ยนโปรแกรม เรามกใชตวแปรเพ7'อเก9บค:าต:าง ๆ ท&'ม&การเปล&'ยนแปลงในโปรแกรม แต:ในบางคร=งเราอาจพบว:า
ม&ค:าบางอย:าง ซ@'งไม:ม&การเปล&'ยนแปลงใด ๆ เลยตลอดโปรแกรม เช:น ช7'อบรCษท, อตราภาษ&มGลค:าเพC'ม หร7อแมกระท'งค:าคงท&'
ทางคณCตศาสตรM เช:น ค:า pi (ประมาณ 3.14) เปSนตน
        ดงน=นการนTาเอาค:าคงท&' (Constant) มาใชเก9บค:าท&'ไม:ม&การเปล&'ยนแปลงตลอดโปรแกรม จะช:วยใหเราเข&ยนและดGแล
รกษาโปรแกรมไดง:ายข@=น การประกาศค:าคงท&'จะคลายกบการประกาศตวแปร แต:ต องนTาหนาดวยค&ยเวCรMด Const โดยม&รGป
แบบดงน&=

        ร)ปแบบ
                 Const <ชอตวแปร> [As <ชนดขอมล>] = <คาคงท>

        เราสามารถระบWชนCดขอมGลหร7อไม:กได เช:น
                                         9
               Const varPI = 3.14             'ประกาศค:าคงท&'ช'อ varPI
                                                               7
        หร7อ
               Const varPI As Single = 3.14   'ประกาศค:าคงท&'ช'อ varPI ใหเปSนชนCด Single
                                                                 7




                                                       -1-
VB .NET 2005 By Warawut Khangkhan
การใชตวแปรแบบคงท&ใน VB.NET 2005 express Edition
                  '


ตวอย,าง การเขยนโปรแกรมการใชตวแปรแบบคงท
        ตวอย:างท&จะใชน&=เปSนตวอย:างท&เ' ก&'ยวกบการคTานวณหาเสนรอบวงกลม โดยใชสGตร 2∏r ซ@'งค:า ∏ ม&ค:าเท:ากบ 3.14
                 '


                                                                      2
                                                                          3



                                                     4                5

ตาราง กTาหนด Properties ท&'สาคญของแต:ละ Control
                            T
 No.       Control                    Properties                              Value
  1 Form                      Name                 Form1
                              Text                 โปรแกรมหาเสนรอบวง
                              StartPosition        CenterScreen
                              MaximizeBox          False
                              MinimumBox           False
  2 TextBox                   Name                 txtRadius
                              TextAlign            Center
  3 TextBox                   Name                 txtLineRadius
                              ReadOnly             True
                              TextAlign            Center
  4 Button                    Name                 btnCalcualtor
                              Text                 คTานวณ
  5 Button                    Name                 btnExit
                              Text                 ออก




                                                     -2-
VB .NET 2005 By Warawut Khangkhan
การใชตวแปรแบบคงท&ใน VB.NET 2005 express Edition
                  '


Coding:

Public Class Form1

  Const pi As Single = 3.14 'pi is constant variable

  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
     Application.Exit()
  End Sub

  Private Sub txtRadius_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRadius.Enter
     txtRadius.SelectionStart = 0
     txtRadius.SelectionLength = txtRadius.Text.Length
  End Sub

  Private Sub txtRadius_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Handles txtRadius.KeyDown
     If e.KeyCode = Keys.Enter Then
       btnCalculator_Click(sender, e)

       txtRadius.SelectionStart = 0
       txtRadius.SelectionLength = txtRadius.Text.Length
    End If
  End Sub

  Private Sub txtRadius_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Handles txtRadius.KeyPress
     If (e.KeyChar < "0" Or e.KeyChar > "9") And e.KeyChar <> "." Then e.Handled = True
  End Sub

  Private Sub btnCalculator_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnCalculator.Click
     txtLineRadius.Text = Format((2 * pi * Convert.ToDouble(txtRadius.Text)), "0.00")
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     txtRadius.Text = "0"
     txtLineRadius.Text = "0.00"
  End Sub
End Class




                                                       -3-

More Related Content

Similar to การใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editor

หน่วยที่ 4 การสร้างแบบสอบถาม
หน่วยที่ 4 การสร้างแบบสอบถามหน่วยที่ 4 การสร้างแบบสอบถาม
หน่วยที่ 4 การสร้างแบบสอบถามkruthanyaporn
 
Programming VB.NET 2005 Express Editor
Programming VB.NET 2005 Express EditorProgramming VB.NET 2005 Express Editor
Programming VB.NET 2005 Express EditorWarawut
 
การสร้างแบบสอบถาม
 การสร้างแบบสอบถาม การสร้างแบบสอบถาม
การสร้างแบบสอบถามkruthanyaporn
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1Little Tukta Lita
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีNattapon
 

Similar to การใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editor (11)

หน่วยที่ 4 การสร้างแบบสอบถาม
หน่วยที่ 4 การสร้างแบบสอบถามหน่วยที่ 4 การสร้างแบบสอบถาม
หน่วยที่ 4 การสร้างแบบสอบถาม
 
Programming VB.NET 2005 Express Editor
Programming VB.NET 2005 Express EditorProgramming VB.NET 2005 Express Editor
Programming VB.NET 2005 Express Editor
 
การสร้างแบบสอบถาม
 การสร้างแบบสอบถาม การสร้างแบบสอบถาม
การสร้างแบบสอบถาม
 
Java intro
Java introJava intro
Java intro
 
02 basic
02 basic02 basic
02 basic
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
 
3.Expression
3.Expression3.Expression
3.Expression
 
Check box
Check boxCheck box
Check box
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
 

More from Warawut

Database design
Database designDatabase design
Database designWarawut
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4Warawut
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10Warawut
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9Warawut
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8Warawut
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7Warawut
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6Warawut
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6Warawut
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5Warawut
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4Warawut
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5Warawut
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3Warawut
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3Warawut
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2Warawut
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemWarawut
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4Warawut
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1Warawut
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MISWarawut
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3Warawut
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2Warawut
 

More from Warawut (20)

Database design
Database designDatabase design
Database design
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information System
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MIS
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2
 

การใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editor

  • 1. VB .NET 2005 By Warawut Khangkhan การใชตวแปรแบบคงท&ใน VB.NET 2005 express Edition ' การใชตวแปรแบบคงทใน VB .NET 2005 Express Edition ในการเข&ยนโปรแกรม เรามกใชตวแปรเพ7'อเก9บค:าต:าง ๆ ท&'ม&การเปล&'ยนแปลงในโปรแกรม แต:ในบางคร=งเราอาจพบว:า ม&ค:าบางอย:าง ซ@'งไม:ม&การเปล&'ยนแปลงใด ๆ เลยตลอดโปรแกรม เช:น ช7'อบรCษท, อตราภาษ&มGลค:าเพC'ม หร7อแมกระท'งค:าคงท&' ทางคณCตศาสตรM เช:น ค:า pi (ประมาณ 3.14) เปSนตน ดงน=นการนTาเอาค:าคงท&' (Constant) มาใชเก9บค:าท&'ไม:ม&การเปล&'ยนแปลงตลอดโปรแกรม จะช:วยใหเราเข&ยนและดGแล รกษาโปรแกรมไดง:ายข@=น การประกาศค:าคงท&'จะคลายกบการประกาศตวแปร แต:ต องนTาหนาดวยค&ยเวCรMด Const โดยม&รGป แบบดงน&= ร)ปแบบ Const <ชอตวแปร> [As <ชนดขอมล>] = <คาคงท> เราสามารถระบWชนCดขอมGลหร7อไม:กได เช:น 9 Const varPI = 3.14 'ประกาศค:าคงท&'ช'อ varPI 7 หร7อ Const varPI As Single = 3.14 'ประกาศค:าคงท&'ช'อ varPI ใหเปSนชนCด Single 7 -1-
  • 2. VB .NET 2005 By Warawut Khangkhan การใชตวแปรแบบคงท&ใน VB.NET 2005 express Edition ' ตวอย,าง การเขยนโปรแกรมการใชตวแปรแบบคงท ตวอย:างท&จะใชน&=เปSนตวอย:างท&เ' ก&'ยวกบการคTานวณหาเสนรอบวงกลม โดยใชสGตร 2∏r ซ@'งค:า ∏ ม&ค:าเท:ากบ 3.14 ' 2 3 4 5 ตาราง กTาหนด Properties ท&'สาคญของแต:ละ Control T No. Control Properties Value 1 Form Name Form1 Text โปรแกรมหาเสนรอบวง StartPosition CenterScreen MaximizeBox False MinimumBox False 2 TextBox Name txtRadius TextAlign Center 3 TextBox Name txtLineRadius ReadOnly True TextAlign Center 4 Button Name btnCalcualtor Text คTานวณ 5 Button Name btnExit Text ออก -2-
  • 3. VB .NET 2005 By Warawut Khangkhan การใชตวแปรแบบคงท&ใน VB.NET 2005 express Edition ' Coding: Public Class Form1 Const pi As Single = 3.14 'pi is constant variable Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Application.Exit() End Sub Private Sub txtRadius_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRadius.Enter txtRadius.SelectionStart = 0 txtRadius.SelectionLength = txtRadius.Text.Length End Sub Private Sub txtRadius_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRadius.KeyDown If e.KeyCode = Keys.Enter Then btnCalculator_Click(sender, e) txtRadius.SelectionStart = 0 txtRadius.SelectionLength = txtRadius.Text.Length End If End Sub Private Sub txtRadius_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRadius.KeyPress If (e.KeyChar < "0" Or e.KeyChar > "9") And e.KeyChar <> "." Then e.Handled = True End Sub Private Sub btnCalculator_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculator.Click txtLineRadius.Text = Format((2 * pi * Convert.ToDouble(txtRadius.Text)), "0.00") End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load txtRadius.Text = "0" txtLineRadius.Text = "0.00" End Sub End Class -3-