![]() | วิชวลสตูดิโอ ดอทเน็ต (Visual Studio .NET) | ![]() |
|
ข้อมูลทั้งหมดเกี่ยวกับวิชวลสตูดิโอดอทเน็ต ได้จากการอ่าน ทดสอบ และใช้เวลาไปมากมายในแต่ละเรื่อง ..
ไม่มีเรื่องใดได้จากการนั่งทางใน .. แม้พยายามนั่งแล้วก็ตาม .. ถ้าเป็นไปได้ช่วยคัดลอก (Copy) ทุกอย่างที่ผมเขียนไปเผยแพร่ต่อด้วย .. จะได้ลดเวลาในการค้นหา และทดสอบ |
|
| ||||
| ||||
| ||||
| ||||
| ||||
| ||||
| ||||
| ||||
| |||||
| |||||
| ||||
| ||||||
| |||||
| ||||
| ||||
| ||||
| ||||
| ||||
| ||||
| |||||
| Structure | Property, GET, SET for private object [6]p.47 |
|
หัวข้อ - class library แสดงการควบคุม property ของตัวแปร v - การเรียกใช้ตัวแปร v ต้องดำเนินการผ่าน method (ยกเว้นตัวอย่างนี้) - ส่วนขยายแบบ property คู่กับ get และ set - ตัวอย่างนี้ไม่มีการประมวลผลก่อนรับ และคืนค่า - หากมีคลาสอื่นเรียกใช้ v ต้องเรียกผ่าน pub_v เท่านั้น |
Public Class Class1
Private v As Integer
Shared Sub main()
Dim c As New class1
With c
.v = 11
End With
Console.Write(c.v)
Console.Read()
End Sub
Public Property pub_v() As Integer
Get
Return v
End Get
Set(ByVal value As Integer)
v = value
End Set
End Property
End Class
|
| Structure | Property, GET, SET in Windows Forms Application [6]p.47 |
|
หัวข้อ - การเรียกใช้ v แบบ private ผ่าน namespace ClassLibrary1 - ต้องเรียกใช้ pub_v เพื่อให้เข้าถึง v |
Imports ClassLibrary1
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim c As New Class1
With c
.pub_v = 11
End With
MsgBox(c.pub_v)
End Sub
End Class
|
| Structure | constructor with 2 class in 1 source code of console application [6]p.269 |
| - การเรียกใช้ class ใน namespace เดียวกัน โดยเขียน 2 class ภายในแฟ้มเดียวกัน | |
|
หัวข้อ - การเรียกใช้ instance จาก class - การเปิดพื้นที่ในหน่วยความจำแยกกัน - ถ้าเปลี่ยน v1 เป็น shared จะทำให้ค่ามาจากตำแหน่งเดียวกัน - ผลคือ ค่าของ my_v.v1 และ c.v1 จะเป็นค่าเดียวกัน |
Public Class Class1
Shared my_v As New second_class
Shared Sub main()
Dim c As New second_class
c.v1 = 6
Console.Write(c.v1) '6
Console.Write(my_v.v1) '5
Console.Write(second_class.v2) '0
Console.Read()
End Sub
End Class
Public Class second_class
Public v1 As Integer
Public Shared v2 As Integer
Public Sub New()
v1 = 5
End Sub
End Class
|
| Structure | inherits, overload, shared in console application [6]p.209 |
| - การสืบทอด (inherite) ต่อกัน 3 รุ่น ของ 3 คลาส โดยทำงานใน console application | ||
|
หัวข้อ - ปู่มี 1 sub และ 1 function - พ่อทำการ overload method ของปู่ - ลูกสืบทอดจากปู่ แต่เรียกใช้ของพ่อ
|
Public Class Class1
Inherits grand_father ' ถ้าตรงนี้เป็นพ่อจะให้ผลต่างออกไป
Shared Sub main()
Dim g As New grand_father
Dim f As New father
Console.Write(g.my_v()) ' 10
Console.Write(f.my_v()) ' 11
g_sub() '12
Console.Read()
End Sub
End Class
Public Class father
Inherits grand_father
Public Overloads Function my_v() As Integer
Return 11
End Function
End Class
Public Class grand_father
Private v As Integer
Public Function my_v() As Integer
v = 10
Return v
End Function
Shared Sub g_sub()
Console.Write(12)
End Sub
End Class
| |
| Structure | use 2 class library of inheritance |
Imports ClassLibrary1 ' add reference required
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim g As New grand_father
Dim f As New father
MsgBox(g.my_v() & f.my_v()) ' 1011
grand_father.g_sub() '12
End Sub
End Class
+ winapp_call_grandfather.zip 96 KB
(มี solution ของ windows forms application และ .dll 2 แฟ้ม คือ grand_father.dll + father.dll)
Root namespace: Classlibrary1 Project name: grand_father Solution name: grand_father Class name: grand_father
Root namespace: Classlibrary1 Project name: father Solution name: father Class name: father
| |
| Structure | grand, father, child in console & windows forms application |
| - การเขียนโปรแกรมเพื่อแสดงการสืบทอดแบบต่าง ๆ ตั้งแต่ระดับปู่ ไปถึงลูก แล้วนำไปสืบทอดต่อใน form |
|
หัวข้อ - ปู่มี function, sub และ Property - instance มีแบบ private และ public |
Public Class Mygrand
Private g1 As Integer
Public g2 As String
Public Function cntg(ByVal x As String) As String
g2 = CStr(Len(x))
Return g2
End Function
Public Sub setg()
exg1 = 1
g2 = "two"
End Sub
Public Property exg1() As Integer
Get
If (g2 = "zero") Then Return 0 Else Return g1
End Get
Set(ByVal value As Integer)
If (g2 = "two") Then g1 = 2 Else g1 = value
End Set
End Property
End Class
' mygrand.rar (source + .dll)
|
|
หัวข้อ - พ่อสืบทอดจากปู่ - นำ sub มาทำ Overloads หรือเปลี่ยนการประมวลผล |
Public Class Myfather
Inherits mygrand_ns.Mygrand
Public Overloads Sub setg()
exg1 = 3
End Sub
End Class
' myfather.rar (source + .dll)
|
|
หัวข้อ - หลังสืบทอด รุ่นลูกก็ใช้ประโยชน์เต็มที่ - in_mychild เป็นการสร้าง class เพื่อ overload function |
' add reference mygrand_as & myfather_as
Module mychild
Sub Main()
Dim nation As New mygrand_ns.Mygrand
nation.exg1 = 7
Console.Write(nation.exg1) ' 7
Dim burin As New in_mychild
burin.setg()
Console.Write(burin.exg1) ' 3
burin.g2 = "zero"
burin.exg1 = 4
Console.Write(burin.exg1) ' 0
Console.Write(burin.cntg("two")) ' 2
Console.Read()
End Sub
End Module
Class in_mychild
Inherits myfather_ns.Myfather
Public Overloads Function cntg(ByVal x As String) As String
Dim o As String = ""
If (x = "one") Then o = "1"
If (x = "two") Then o = "2"
Return o
End Function
End Class
' mychild.rar (source + .exe)
|
|
หัวข้อ - สามารถทำ oveload ใน windows forms - ตัวอย่างนี้ แยก class ออกมาจาก form1 |
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim myform As New in_form
MsgBox(myform.cntg("three"))
End Sub
End Class
Class in_form
Inherits myfather_ns.Myfather
Public Overloads Function cntg(ByVal x As String) As String
Dim o As String = ""
If (x = "one") Then o = "1"
If (x = "two") Then o = "2"
If (x = "three") Then o = "3"
Return o
End Function
End Class
|
| Structure | inherit, property, overrides, console application |
| - การควบคุมตัวแปรแบบ private และการใช้ sub และ function ทำงานร่วมกับ property |
|
หัวข้อ - นำโปรแกรมก่อนหน้านี้มาปรับให้มีเนื้อหา - การ overload จะมีผลกับต่อลูก แต่ไม่มีผลย้อนกลับต่อปู่ - การ override จะมีผลย้อนกับต่อปู่ที่สืบทอดมาด้วย
|
Module Module1
Sub Main()
Dim chk As New checkid55
chk.set_pass_admin("nation")
chk.ex_id = 5401001
Console.Write(chk.ex_id) ' 5401001
Console.ReadLine()
End Sub
End Module
Public Class checkid54
Private id As Integer
Public admin_password As String
Public Overridable Function check_admin() As Boolean
If (admin_password = "pass") Then Return True Else Return False
End Function
Public Sub set_pass_admin(ByVal pass As String)
admin_password = pass
End Sub
Public Property ex_id() As Integer
Get
If (check_admin()) Then Return id Else Return 0
End Get
Set(ByVal get_id As Integer)
If (check_admin()) Then
Dim id12 As Integer = CInt(Mid(CStr(get_id), 1, 2))
If (Len(CStr(get_id)) = 7) Then
If (id12 >= 47 And id12 <= 54) Then
id = get_id
Else
id = 5400000
End If
Else
id = 5499999
End If
End If
End Set
End Property
End Class
Public Class checkid55
Inherits checkid54
Public Overrides Function check_admin() As Boolean
If (admin_password = "nation") Then Return True Else Return False
End Function
End Class
|
| Structure | abstract, overloads, overrides, overridable |
| - การประกาศ class แบบที่ยังไม่มี code หรือมี code ไม่สมบูรณ์ และจำเป็นต้องเขียน code ภายหลัง |
|
หัวข้อ - abstract หรือ mustinherit ระบุว่า class มี method ที่ไม่มี code - MustOverride ใช้ระบุว่าเป็น method ที่จะต้องเขียน code ภายหลัง - Overridable ใช้ระบุว่าเป็น method ที่อาจต้องเขียน code ใหม่ - Overrides ใช้เขียน code ให้ method ที่ประกาศเป็น overridable - Overloads ใช้เขียน code ทับ method ที่มี code แล้ว - การ inherits ความสัมพันธ์ของพ่อกับลูกเรียกว่า is-a relationship - การ implements interface ความสัมพันธ์ของพ่อกับลูกเรียกว่า has-a relationship [6]p.352 is-a, has-a relationship |
Module Module1
Sub Main()
Dim w1 As New work3
Console.Write(w1.x1 + w1.f1() + w1.f2())
Console.Read()
End Sub
End Module
Class work3
Inherits work2
Public Overrides Function f1() As Integer
Return 3
End Function
Public Overrides Function f2() As Integer
Return 2
End Function
Public Overloads Function f3() As Integer
Return 4
End Function
End Class
MustInherit Class work2
Inherits work1
Public x1 As Integer = 4 ' dim = private
End Class
MustInherit Class work1
Public MustOverride Function f1() As Integer
Public Overridable Function f2() As Integer
Return 1
End Function
Public Function f3() As Integer
Return 2
End Function
End Class
|
| Structure | overrides, overloads |
| - การแทนที่ method หลังสืบทอด หรือเปลี่ยนให้สามารถรับได้หลายรูปอย่าง polymorphism |
|
หัวข้อ - father สืบทอดจาก grand_father - father ทำทั้ง overloads และ overrides - ถ้ารับค่าเป็นเลขบ้าง อักษรบ้าง ผสมกันบ้าง ก็รับไหว |
Module Module1
Sub Main()
Dim child As New father
child.getnum(100, "sombat", "panu")
child.getnum(100, "sombat")
child.getnum(100)
Console.ReadLine()
End Sub
End Module
Class father
Inherits grand_father
Overloads Sub getnum(ByVal num As Integer, ByVal name As String, ByVal lastname As String)
Console.WriteLine(name & " " & lastname & " " & num)
End Sub
Overloads Sub getnum(ByVal num As Integer, ByVal name As String)
Console.WriteLine(name & " " & " " & num)
End Sub
Overrides Sub getnum(ByVal num As Integer)
Console.WriteLine("number is " & num)
End Sub
End Class
Class grand_father
Overridable Sub getnum(ByVal num As Integer)
Console.Write(num)
End Sub
End Class
|
| Structure | overrides, overloads |
| - การรับและส่งค่ากับฟังก์ชันที่มีการสืบทอด และทำงานกับ override และ overload |
|
หัวข้อ - split ด้วย space - overrides ต้องกำหนด overridable ไว้ก่อน - overloads จะเป็นการแทนที่ หรือปรับ parameter
|
Module Module1
Sub Main()
Dim g As New mycal55
Dim name As String, surname As String
name = Console.ReadLine()
surname = Console.ReadLine()
Console.WriteLine(g.getv(name, surname))
Dim t As String
t = Console.ReadLine()
Dim var1 As Integer, var2 As Integer
var1 = CInt(t.Split(" ")(0))
var2 = CInt(t.Split(" ")(1))
Console.WriteLine(g.getv(var1, var2))
g.mycredit()
Console.ReadLine()
End Sub
End Module
Class mycal55
Inherits mycal54
Overloads Function getv(ByVal i1 As Integer, ByVal i2 As Integer)
Return i1 + i2
End Function
Overrides Sub mycredit()
Console.WriteLine("nation")
End Sub
End Class
Class mycal54
Function getv(ByVal s1 As String, ByVal s2 As String)
Return s1 & " " & s2
End Function
Overridable Sub mycredit()
Console.WriteLine("burin")
End Sub
End Class
|
| Structure | overrides, overloads กับ tostring method |
| - การทำ overrides กับ method ของระบบ |
|
หัวข้อ - ปกติเราใช้ tostring อยู่แล้ว - หากจะทำ override กับ tostring ก็สามารถทำได้ |
Module Module1
Sub Main()
Dim data As New work
data.n = "แดง"
data.s = "ทองดี"
Console.WriteLine(data.ToString())
Console.WriteLine(data.ToString("คุณ"))
Console.ReadLine()
End Sub
End Module
Class work
Public n As String
Public s As String
Public Overrides Function ToString() As String
Return "นาย " & n & " " & s
End Function
Public Overloads Function ToString(ByVal k As String) As String
Return k & n & " " & s
End Function
End Class
|
| Structure | interface |
| - ประกาศ interface ชื่อ person หากจะสร้างวัตถุพนักงานใหม่ ก็ต้องให้รายละเอียดเข้าไป เป็นการบังครับ |
|
หัวข้อ - interface ของบุคคลมี 2 หน้าที่ - เวลา implements ต้องกำหนดการศึกษากับความสามารถพิเศษ - การ implements interface ความสัมพันธ์ของพ่อกับลูกเรียกว่า has-a relationship ตัวอย่าง - dbmgmt ต้องมี insert, delete, update, select - sale ต้องมี proid, quan, price |
Module Module1
Sub Main()
Dim boy As New sale
Console.WriteLine(boy.education("business administration"))
Console.WriteLine(boy.talent("speach in public"))
Console.WriteLine(boy.have_experience("NTU 5 years"))
Dim girl As New clerk
Console.WriteLine(girl.education("accounting"))
Console.WriteLine(girl.talent("certification in ms word"))
Console.WriteLine(girl.car_driving("yes"))
Console.ReadLine()
End Sub
End Module
Interface person
Function education(ByVal t As String) As String
Function talent(ByVal t As String) As String
End Interface
Class sale
Implements person
Function education(ByVal t As String) As String Implements person.education
Return t
End Function
Function talent(ByVal t As String) As String Implements person.talent
Return t
End Function
Function have_experience(ByVal t As String) As String
Return t
End Function
End Class
Class clerk
Implements person
Function education(ByVal t As String) As String
Implements person.education
Return t
End Function
Function talent(ByVal t As String) As String Implements
person.talent
Return t
End Function
Function car_driving(ByVal t As String) As String
Return t
End Function
End Class
|
| Structure | Generic of swapping [6]p.405 |
Public Class Class1
Public Shared Sub main()
Dim c As New Class1, x As Integer = 5, y As Integer = 6
c.swapi(x, y)
Console.Write(x & y) ' 65
c.swapint(x, y)
Console.Write(x & y) ' 56
Dim a As String = "A", b As String = "B"
c.swapint(a, b)
Console.Write(a & b) ' BA
Console.Read()
End Sub
Sub swapi(ByRef x As Integer, ByRef y As Integer)
Dim z As Integer = x
x = y : y = z
End Sub
Sub swapint(Of any_type)(ByRef x As any_type, ByRef y As any_type)
Dim z As any_type = x
x = y : y = z
End Sub
End Class
|
| Database | mysqlconnection, wampp, console, select one record => class, module, windows forms |
' add reference C:\Program Files\MySQL\MySQL Connector Net 6.5.4\Assemblies\v4.0\MySql.Data.dll ' in xampp (99 MB), start mysql 1. ติดตั้ง xampp เพื่อเปิดบริการ mysql ได้พอร์ท 3306 2. ติดตั้ง MySQL Connector Net 6.5.4 เพื่อเชื่อมต่อกับ MySQL 3. add reference mysql.data.dll C:\Program Files\MySQL\MySQL Connector Net 6.5.4 \Assemblies\v4.0\MySql.Data.dll 4. มีระบบฐานข้อมูล mysql ซึ่งมีข้อมูลพร้อมทดสอบ ได้แก่ db:mysql,tb:time_zone_name,fld:time_zone_id,name 5. ตัวอย่าง console application ที่ประมวลผลแบบ sub main ใต้ class Imports MySql.Data.MySqlClient Public Class selectone Private Shared conn As New MySqlConnection Private Shared cmd As New MySqlCommand Private Shared opt As String Public Shared Sub main() Console.WriteLine(select_zone(1000000)) Console.WriteLine(select_zone(101)) Console.WriteLine(select_zone(102)) Console.Read() End Sub Public Shared Function select_zone(ByVal zid As Integer) As String Dim zname As String = "not found" Dim connstring As String = "server=127.0.0.1;user id=root;password=;" connstring &= "database=mysql;charset=tis620;" conn.ConnectionString = connstring cmd.Connection = conn conn.Open() cmd.CommandText = "select time_zone_id,name from time_zone_name " cmd.CommandText &= "where time_zone_id = " & zid Dim r As MySqlDataReader = cmd.ExecuteReader() If r.Read() Then zname = r(1) conn.Close() Return zname End Function End Class 6. ตัวอย่าง console application ที่ประมวลผลแบบ sub main ใต้ module ซึ่งแบบนี้ไม่ต้องกำหนดฟังก์ชันแบบ shared Imports MySql.Data.MySqlClient Module module1 Sub main() Dim s As New selectone Console.WriteLine(s.select_zone(1000000)) Console.WriteLine(s.select_zone(101)) Console.WriteLine(s.select_zone(102)) Console.Read() End Sub End Module Public Class selectone Private conn As New MySqlConnection Private cmd As New MySqlCommand Private opt As String Public Function select_zone(ByVal zid As Integer) As String Dim zname As String = "not found" Dim connstring As String = "server=127.0.0.1;user id=root;password=;" connstring &= "database=mysql;charset=tis620;" conn.ConnectionString = connstring cmd.Connection = conn conn.Open() cmd.CommandText = "select time_zone_id,name from time_zone_name " cmd.CommandText &= "where time_zone_id = " & zid Dim r As MySqlDataReader = cmd.ExecuteReader() If r.Read() Then zname = r(1) conn.Close() Return zname End Function End Class 7. เปลี่ยน class ที่พัฒนาไปเป็นแบบ class library จะได้แฟ้ม selectone_as.dll + selectone_as.dll 12 KB 8. ตัวอย่าง console application แบบเรียกใช้จาก class library จะต้อง add reference mysql.data.dll และ selectone_as.dll Imports selectone_ns Module Module1 Sub Main() Dim s As New selectone Console.WriteLine(s.select_zone(1000000)) Console.WriteLine(s.select_zone(101)) Console.WriteLine(s.select_zone(102)) Console.Read() End Sub End Module 9. การทำงานกับ windows forms application จะต้อง add reference mysql.data.dll และ selectone_as.dll Imports selectone_ns Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As New selectone TextBox1.Text = s.select_zone(102) End Sub End Class | |
| Database | mysqlconnection, wampp, console, select |
' add reference C:\Program Files\MySQL\MySQL Connector Net 6.5.4\Assemblies\v4.0\MySql.Data.dll ' in xampp (99 MB), start mysql Imports MySql.Data.MySqlClient Public Class Class1 Public Shared Sub main() Dim conn As New MySqlConnection conn.ConnectionString = "server=127.0.0.1;user id=root;password=;database=mysql" Dim cmd As New MySqlCommand cmd.Connection = conn cmd.CommandText = "select name,time_zone_id from time_zone_name" Dim s As String = "" conn.Open() Dim r As MySqlDataReader = cmd.ExecuteReader() While r.Read() s &= r(0) & r(1) & Chr(10) ' 685 records ' If r("time_zone_id") = 3 Then Exit While End While conn.Close() Console.Write(s) Console.Read() End Sub End Class | |
| Database | select, insert, delete, update in console |
' add reference C:\Program Files\MySQL\MySQL Connector Net 6.5.4\Assemblies\v4.0\MySql.Data.dll
' in xampp, start mysql
Imports MySql.Data.MySqlClient
Public Class Class1
Private Shared conn As New MySqlConnection
Private Shared cmd As New MySqlCommand
Private Shared opt As String
Public Shared Sub main()
conn.ConnectionString = "server=127.0.0.1;user id=root;password=;database=mysql;charset=tis620;"
cmd.Connection = conn
Console.WriteLine("sa,s,i,d,u of time_zone_id,name in time_zone_name")
opt = Console.ReadLine()
conn.Open()
If opt = "s" Or opt = "sa" Then select_zone()
If opt = "i" Then insert_zone()
If opt = "d" Then delete_zone()
If opt = "u" Then update_zone()
conn.Close()
Console.Read()
End Sub
Public Shared Sub select_zone()
cmd.CommandText = "select time_zone_id,name from time_zone_name"
Dim r As MySqlDataReader = cmd.ExecuteReader()
Dim i As Integer = 1
While r.Read()
Console.WriteLine(r(0) & r(1)) ' 1684 records
i = i + 1
If i > 100 And opt <> "sa" Then
Console.ReadLine()
i = 1
End If
End While
End Sub
Public Shared Sub insert_zone()
Dim zid As String = Console.ReadLine()
Dim zname As String = Console.ReadLine()
cmd.CommandText = "insert into time_zone_name (time_zone_id,name)values(" & zid & ",'" & zname & "')"
Dim r As Integer = cmd.ExecuteNonQuery()
End Sub
Public Shared Sub delete_zone()
Dim zid As String = Console.ReadLine()
Dim zname As String = Console.ReadLine()
cmd.CommandText = "delete from time_zone_name where time_zone_id = " & zid
Dim r As Integer = cmd.ExecuteNonQuery()
End Sub
Public Shared Sub update_zone()
Dim zid As String = Console.ReadLine()
Dim zname As String = Console.ReadLine()
cmd.CommandText = "update time_zone_name set name = '" & zname & "' where time_zone_id = " & zid
Dim r As Integer = cmd.ExecuteNonQuery()
End Sub
End Class
| |
| Database | select, insert, delete, update in console + class library |
Imports MySql.Data.MySqlClient
Public Class Class1
Private Shared conn As New MySqlConnection
Private Shared cmd As New MySqlCommand
Private Shared opt As String
Public Shared Sub main()
Console.WriteLine("s,i,d,u of time_zone_id,name in time_zone_name")
opt = Console.ReadLine()
Dim zid As String
Dim zname As String
If opt = "s" Then
Dim l1 As Integer = Console.ReadLine()
Dim l2 As Integer = Console.ReadLine()
Console.Write(select_zone(l1, l2))
End If
If opt = "i" Then
zid = Console.ReadLine()
zname = Console.ReadLine()
insert_zone(zid, zname)
Console.Write(select_zone(1680, 1750))
End If
If opt = "d" Then
zid = Console.ReadLine()
delete_zone(zid)
Console.Write(select_zone(1680, 1750))
End If
If opt = "u" Then
zid = Console.ReadLine()
zname = Console.ReadLine()
update_zone(zid, zname)
Console.Write(select_zone(1680, 1750))
End If
Console.Read()
End Sub
Public Shared Sub open_conn()
If Len(conn.ConnectionString.ToString) = 0 Then
conn.ConnectionString = "server=127.0.0.1;user id=root;password=;database=mysql;charset=tis620;"
End If
cmd.Connection = conn
conn.Open()
End Sub
Public Shared Function select_zone(ByVal l1 As Integer, ByVal l2 As Integer) As String
open_conn()
cmd.CommandText = "select name,time_zone_id from time_zone_name limit " & (l1 - 1) & "," & (l2 - 1)
Dim r As MySqlDataReader = cmd.ExecuteReader()
Dim s As String = ""
While r.Read()
s &= r(0) & r(1) & Chr(10) ' 1684 records
End While
conn.Close()
Return s
End Function
Public Shared Sub insert_zone(ByVal zid As String, ByVal zname As String)
open_conn()
cmd.CommandText = "insert into time_zone_name (time_zone_id,name)values(" & zid & ",'" & zname & "')"
Dim r As Integer = cmd.ExecuteNonQuery()
conn.Close()
End Sub
Public Shared Sub delete_zone(ByVal zid As String)
open_conn()
cmd.CommandText = "delete from time_zone_name where time_zone_id = " & zid
Dim r As Integer = cmd.ExecuteNonQuery()
conn.Close()
End Sub
Public Shared Sub update_zone(ByVal zid As String, ByVal zname As String)
open_conn()
cmd.CommandText = "update time_zone_name set name = '" & zname & "' where time_zone_id = " & zid
Dim r As Integer = cmd.ExecuteNonQuery()
conn.Close()
End Sub
End Class
+ mysql_sidu2.zip
| |
| Database | using class library, mysql in windows form application |
Imports ClassLibrary1.Class1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox5.Text = ClassLibrary1.Class1.select_zone(TextBox3.Text, TextBox4.Text)
TextBox5.Refresh()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = 1700
TextBox2.Text = "ดวงดาว"
TextBox3.Text = 1670
TextBox4.Text = 1750
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ClassLibrary1.Class1.insert_zone(TextBox1.Text, TextBox2.Text)
TextBox5.Refresh()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ClassLibrary1.Class1.delete_zone(TextBox1.Text)
TextBox5.Refresh()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ClassLibrary1.Class1.update_zone(TextBox1.Text, TextBox2.Text)
TextBox5.Refresh()
End Sub
End Class
+ mysql_form2.zip
| |
| Command | shell และ process และ file writing |
| - การเรียก shell command แบบต่าง ๆ รวมถึงการสร้าง batch file ผ่าน file writing |
|
หัวข้อ - การใช้ select case - การเรียกคำสั่งภายนอกผ่าน shell - การอ้างอิง process id และสั่ง taskkill - การใช้งาน process object เบื้องต้น - try catch ใช้ป้องกันผู้ใช้ปิด process เอง - การทำงานกับ filestream - การ convert จาก string เป็น byte array 2 แบบ - ใน case 4 เป็นตัวอย่างที่ไม่ดี ให้ทำ code optimization - System.Text.Encoding.UTF8.GetBytes ใช้แปลง string เป็น byte array | Imports System.IO
Module Module1
Dim p = New Process
Sub Main()
Dim mypro As Integer
Console.Write("option: ")
mypro = Console.ReadLine
Select Case mypro
Case 1
Shell("c:\windows\notepad.exe", AppWinStyle.NormalFocus)
Shell("explorer.exe http://www.google.com", AppWinStyle.NormalFocus)
Case 2
Dim pid1 As Integer
pid1 = Shell("c:\windows\notepad.exe", AppWinStyle.NormalFocus)
Console.Write(pid1)
Console.ReadLine()
Shell("c:\windows\system32\taskkill.exe /pid " & pid1)
Case 3
start_process("C:\Windows\System32\Notepad.exe")
Console.Write("start in process")
Console.ReadLine()
Try
p.kill()
Catch ex As Exception
Console.Write(ex)
Console.ReadLine()
End Try
Case 4
Dim data(0) As Byte
Dim fw As FileStream
fw = New FileStream("c:\x.bat", FileMode.Create, FileAccess.Write)
Dim cmd As String = "ipconfig"
ReDim data(cmd.Length)
For i = 0 To cmd.Length - 1: data(i) = Asc((cmd.Substring(i, 1))) :Next
fw.Write(data, 0, cmd.Length)
'
cmd = Chr(13) + Chr(10)
ReDim data(cmd.Length)
For i = 0 To cmd.Length - 1: data(i) = Asc((cmd.Substring(i, 1))) :Next
fw.Write(data, 0, cmd.Length)
'
cmd = "ping www.google.com > y.txt"
ReDim data(cmd.Length)
For i = 0 To cmd.Length - 1 : data(i) = Asc((cmd.Substring(i, 1))) : Next
fw.Write(data, 0, cmd.Length)
'
cmd = Chr(13) + Chr(10)
ReDim data(cmd.Length)
data = System.Text.Encoding.UTF8.GetBytes(cmd)
fw.Write(data, 0, cmd.Length)
'
cmd = "pause | type y.txt"
data = System.Text.Encoding.UTF8.GetBytes(cmd)
fw.Write(data, 0, cmd.Length)
'
fw.Close()
Shell("c:\x.bat")
End Select
End Sub
Sub start_process(ByVal c As String)
p.StartInfo.FileName = c
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal
p.Start()
End Sub
End Module
|
| Command | try catch finally |
| - การตรวจจับความผิดพลาด และดำเนินการกำกับดูแล |
Module Module1
Sub Main()
Dim ar(4) As Byte
For i As Integer = -1 To 4
Try
Select Case i
Case 1
Console.WriteLine(0 / 0) ' NaN (Not a Number)
Console.WriteLine(1 / 0) ' Infinity
Shell("c:\windows\system32\note_pad.exe")
Console.WriteLine("lost 1")
Case 2
Console.WriteLine("i = 2")
Throw New Exception("Throw exception") ' jump to exception
Console.WriteLine("lost 2")
Shell("lost 3")
Case 3
IO.File.Delete("c:\windows") ' jump to exception
Console.WriteLine("lost")
Case Else
Console.WriteLine(ar(i))
End Select
Catch ex As IndexOutOfRangeException
Console.WriteLine("Exception :" & "index over 2")
Catch ex As IO.FileNotFoundException
Console.WriteLine("Exception 1 :" & "should be notepad.exe")
Catch ex As UnauthorizedAccessException
Console.WriteLine("Exception 3 : " & ex.Message)
Catch ex As Exception
' ex.message = "Throw exception"
Console.WriteLine("Exception 2 : " & ex.Message)
Finally
Console.WriteLine("--- end " & i & " ---")
End Try
Next
Console.Read()
End Sub
End Module
| ![]() |
| Thread | แต่ละ Thread ทำงานของตนเองจนเสร็จแล้วปิดตัวเองไป |
| - ใช้ Task manager ตรวจจำนวน Thread ที่เพิ่ม และวัดเวลาการทำงานของแต่ละเทรดได้ .. ไม่จำเป็นที่ Thread แรกต้องทำงานเสร็จก่อน |
| Dployment | publish & ftp to server for online installation |
| - การสร้าง installer เพื่อ publish สำหรับนำไปใช้ กรณีศึกษา บทความในชีวิตประจำวัน |
รายละเอียด การสร้าง installer เพื่อ publish ผลงานที่ทำการ build จาก vs.net 2010 แล้วเผยแพร่ให้กลุ่มเป้าหมาย ไม่ซับซ้อนเมื่อใช้ vs.net (ใน nsis หรือ android ซับซ้อนกว่า) จึงได้ทำ demo ตัว installer โดยมีขึ้นตอนดังนี้ 1. โจทย์ หรือความต้องการ มีโจทย์ว่าจะทำโปรแกรมแสดงบทความไอทีในชีวิตประจำวันให้ผู้อ่านเปิดอ่านแบบ offline ได้ ซึ่งปกติเผยแพร่ไว้ที่ thaiall.com/itinlife แล้วเคยทำแบบนี้ในรูป android apps เผยแพร่ใน play store หรือ thaiall.com/android จึงคัดลอก script ส่วนของ array ที่เป็นภาษา C มาปรับเป็นภาษา Basic 2. การออกแบบโปรแกรม ทำใน vs.net แบ่งเป็น 2 ส่วนคือ windows forms application และ class library โดยเนื้อหาบทความอยู่ใน .dll แต่โปรแกรม form ที่ใช้ทำงานจริง จะเรียกใช้เนื้อหาจาก library หากจะเพิ่ม-ลบบทความก็เพียงแต่ปรับ .dll แล้วถ้าปรับการแสดงผลก็ต้องไปปรับ form ซึ่งแยกให้เห็นการทำงานที่แตกต่างกัน 2 ส่วน 3. เปิดให้ download 3 แบบ 3.1 source code ของ class library ได้รวมเป็น .rar ไว้ที่ http://www.thaiall.com/itinlife/vs2010_dll_itarticles.rar ถ้ามีเฉพาะ library จะประมวลผลไม่ได้ เพราะผมไม่ได้ทำ main() ทิ้งไว้ 3.2 source code ของ windows forms application ได้รวมเป็น .rar ไว้ที่ http://www.thaiall.com/itinlife/vs2010_form_itarticles1.rar ใน form นี้ ผม add reference ไว้แล้ว สามารถทดสอบประมวลผลได้ 3.3 execute file ที่เกิดหลัง build และ publish ผมใช้ ftp ส่งแฟ้มในห้อง publish ไปเผยแพร่แบบออนไลน์ไว้ที่ http://www.thaiall.com/itinlife/publish/publish.htm ถ้ากดปุ่ม install หรือ run setup.exe ก็จะได้โปรแกรมไว้ในเครื่อง แต่ในการติดตั้งจะ download จาก net เพราะตัว setup.exe กับใน publish\Application Files นั้นแยกกันชัดเจน |
|
| Question - Answer |
คำถาม คำสั่งทั่วไปเกี่ยวกับ VB มีอะไรบ้าง คำตอบ http://www.thaiall.com/vbnet/msvb.htm |
คำถาม อ่านค่า Mac Address ซึ่งเป็นเลขประจำตัวของ Network Card อย่างไร คำตอบ ใช้บริการของ System.Net.NetworkInformation [ อ้างอิง ]
|
คำถาม อ่านค่า Serial Number ของ Harddisk อย่างไร
คำตอบ ใช้ค่าจาก CreateObject("Scripting.FileSystemObject") [ อ้างอิง ]
|
คำถาม Class library แบบ Console application กำหนดให้ startup อย่างไร คำตอบ สร้าง method แล้วก็ใช้คำว่า console สั่งเขียน หรืออ่าน
|
คำถาม พบคำว่า "A project with an Output Type of Class Library cannot be started directly" หมายถึงอะไร คำตอบ ถ้าสร้าง Class Library และกำหนด Application type เป็น Class Library จะกำหนด Start up ไม่ได้ หากต้องการประมวลผล ต้องสร้าง project แล้วเพิ่ม reference จาก .dll แล้วเรียกใช้ class ใน method ที่สร้างขึ้นมา ถ้าจะ startup Class ก็ต้องเปลี่ยน Application type เป็น Console Application แล้วกำหนด startup method |
คำถาม ใน class library แบบ console application เมื่อสั่ง Start debugging หรือ F5 แล้วได้แฟ้ม .exe เก็บไว้ที่ไหน คำตอบ C:\Documents and Settings\burin\My Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1\bin\Debug พบ 4 แฟ้มคือ ClassLibrary1.exe ClassLibrary1.pdb ClassLibrary1.vshost.exe ClassLibrary1.xml |
คำถาม ใน class library แบบ class library เมื่อเปลี่ยน configuration manager เป็น release แล้ว build จะเกิดอะไรขึ้น คำตอบ C:\Documents and Settings\burin\My Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1\bin\Debug พบ 3 แฟ้มในห้อง release คือ ClassLibrary1.dll ClassLibrary1.pdb ClassLibrary1.xml หากสลับไปมาระหว่าง console application และ class library ก็ไม่พบปัญหา แต่ทำงานแบบ two in one ไม่ได้ |
คำถาม using คืออะไร คำตอบ
|
คำถาม การเรียกใช้ class library ที่เพิ่ม reference แล้วเขียน code อย่างไร คำตอบ
|
คำถาม ถ้า add reference แล้วจะ add ซ้ำไม่ได้ หากต้องการ add ซ้ำก็ต้องลบของเก่าก่อน ต้องทำอย่างไร คำตอบ การ remove reference ให้ double click ที่ my project หรือเรียก properties ของ project ให้คลิ๊ก tab : reference แล้วคลิ๊กที่ชื่อ reference ที่ต้องการลบ แล้วคลิ๊ก remove |
|
เอกสารอ้างอิง
[1] เรวัตร ธรรมาอภิรมย์, "เจาะลึกเทคโนโลยีใหม่ Microsoft .net Framework", บริษัท เอส.พี.ซี.พริ้นติ้ง จำกัด, กรุงเทพฯ, 2544. [2] พร้อมเลิศ หล่อวิจิตร, "คู่มือเรียน Visual Basic 2005", บริษัท โปรวิชั่น จำกัด., กรุงเทพฯ, 2549. [3] ธวัชชัย สุริยะทองธรรม, "พัฒนาเว็บแอพพลิเคชั่น ASP.NET", บริษัท ซัคเซส มีเดีย จำกัด., กรุงเทพฯ, 2548. [4] ยุทธนา ลีลาศวัฒนกุล, "คู่มือการเขียนโปรแกรมและใช้งาน Visual C++.NET", สำนักพิมพ์อินโฟเพรส, กรุงเทพฯ, 2546. [5] กิตติ ภักดีวัฒนะกุล, จำลอง ครูอุตสาหะ, "ASP ฉบับโปรแกรมเมอร์", บริษัท เคทีพี คอมพ์ แอนด์ คอนซัลท์ จำกัด., กรุงเทพฯ, 2543. [6] ศุภชัย สมพานิช, "การเขียนโปรแกรมอย่างมืออาชีพด้วย .NET Framework", บริษัท ไอดีซี พรีเมียร์ จำกัด., นนทบุรี, 2554. |