วันศุกร์ที่ 27 สิงหาคม พ.ศ. 2553
[VB.NET Tips] AddHandler ,Addressof
AddHandler ใช้ในการ "ผูก" อีเว้นต์ของอ๊อบเจ็กต์ กับ procedure ที่ใช้เป็น "event handler"
นอกจาก AddHandler แล้ว ยังมี RemoveHandler ด้วยนะครับ
ตัวอย่างเช่น
[codevb]AddHandler Button2.Click, AddressOf Button1_Click[/codevb]
ทำให้เมื่อคลิก Button2 จะไปทำงานที่ Sub Button1_Click ด้วย
(สมมุติว่าคุณมี Sub procedure ดังกล่าวอยู่แล้ว)
AddressOf เป็นคีย์เวิร์ดที่ให้ค่า reference ของ procedure ครับ
ใช้ในหลายเรื่อง นอกจากใช้กับประโยค AddHandler และ RemoveHandler แล้ว
ยังใช้มากในเรื่องเกี่ยวกับ Threading และ Asynchronous ด้วยครับ
reference : http://greatfriends.biz/webboards/msg.asp?id=11972
วันพฤหัสบดีที่ 26 สิงหาคม พ.ศ. 2553
[OOP] ความแตกต่างระหว่าง interface กับ abstract class
- abstract class เป็นคลาสที่มี abstract method อย่างน้อยหนึ่งเมธอด และส่วนใหญ่จะมี concrete method (เมธอดที่มีส่วน implementation แล้ว) ด้วยก็ได้ นอกจากนั้น abstract class ยังสามารถมี state (พวก attribute หรือ instance variable ต่างๆ) ได้ด้วย
- concrete class ใดๆที่ได้ inherit จาก abstract class หรือได้ implement interface ใดๆไป ก็ย่อมต้อง implement พวก abstract method เหล่านั้นให้ครบถ้วนสมบูรณ์ จึงจะถือว่า class นั้นเป็น concrete class และสามารถสร้าง instance จาก class นั้นได้
- class ใดๆก็ตามจะสามารถ inherit จาก abstract class ได้เพียงหนึ่งเท่านั้น แต่จะสามารถ implement interface ได้มากกว่าหนึ่ง (เคยสงสัยมั้ยครับว่าเพราะอะไร นี่แหล่ะครับที่เป็นส่วนหนึ่งที่ทำให้ concept ในการใช้งานระหว่าง interface กับ abstract class แตกต่างกันโดยสิ้นเชิง)
- รถยนต์ทุกคันต่างก็หยุดจอดที่สี่แยกเมื่อเห็นสัญญาณไฟแดง เพราะอะไร? เพราะรถยนต์คันนั้น (อันที่จริงคือคนขับรถยนต์คนนั้น) เข้าใจว่า สัญญาณไฟสีแดง หมายถึง ให้หยุดรถ นั่นแสดงว่า รถยนต์มีความเข้าใจที่ตรงกันกับป้ายสัญญาณไฟจราจรหรือสามารถติดต่อสื่อสาร กันได้ สมมติว่ารถยนต์คันใดก็ตามที่จะเข้าใจสัญญาณไฟจราจรได้จะต้องเข้าใจ interface ITrafficLight (หรือ implement ITrafficLight นั่นเอง) ทีนี้เมื่อรถยนต์ต่างๆวิ่งมาถึงสี่แยกที่มีป้ายสัญญาณไฟจราจรอยู่ ป้ายสัญญาณไฟจราจรเหล่านั้นก็จะส่ง message ไปบอกรถยนต์ต่างๆว่าตอนนี้ไฟจราจรเป็นสีอะไร message ที่ส่งไปนี้ส่งไปโดยใช้ interface ITrafficLight เพราะฉะนั้นรถยนต์คันใดก็ตามที่ implement ITrafficLight นี้ก็จะเข้าใจและหยุดรถเมื่อเห็นสัญญาณไฟสีแดง ในทางกลับกันหากรถยนต์คันไดที่ไม่ได้ implement ITrafficLight นี้ ก็ย่อมจะไม่เข้าใจใน message ที่ส่งมา และแน่นอนย่อมส่งผลให้ขับฝ่าไปแดงไปและถูกหมาต๋าซิวไปในที่สุด
- เมื่อเข้าหน้าหนาว(เหมือนอย่างตอนนี้) อากาศจะหนาว อุณหภูมิจะลดลง ร่างกายคุณย่อมจะรู้สึกว่าหนาว ขนลุก ตัวสั่น เป็นเวลาเดียวกันกับที่ สารปรอทในเทอร์โมมิเตอร์หดตัวลง เพราะอะไร? แน่นอนเพราะคุณและสารปรอทต่างก็เข้าใจว่าอุณหภูมิมันลดลง สมมติว่าออปเจคต์ใดก็ตามที่จะเข้าใจได้ว่าอุณหภูมิลดลงจะต้อง implement ITemperature แสดงว่าทั้งคุณและปรอทในเทอร์โมมิเตอร์ต่างก็ implement ITemperature นี้เหมือนๆกัน และที่คุณและปรอทรู้ว่าอุณหภูมิได้ลดลงแล้วก็เพราะว่าออปเจคต์ Weather ได้ส่ง message มาบอกคุณและปรอทผ่านทาง interface ITemperature นี้เอง ซึ่งคุณและปรอทต่างก็เข้าใจมันได้ดี แต่จะตอบสนองต่อ message นี้อย่างไรก็ขึ้นอยู่กับออปเจคต์มันเอง เช่น คุณอาจตอบสนองโดยการ ตัวสั่น ปากซีด ขนลุก แต่อีกทางหนึ่ง ปรอทตอบสนองต่อ message นี้ด้วยการหดตัว พันธะระหว่างอะตอมแน่นหนาขึ้น ระยะพันธะหนสั้นลง เป็นต้น
- ออปเจคต์หนึ่งๆอาจถูกสร้างขึ้นมาเพื่อให้สามารถติดต่อสื่อสารกับออปเจคต์ใดๆโดยผ่านทาง interface หนึ่งๆ
(ออปเจคต์คนไทยคนหนึ่งติดต่อสื่อสารกับออปเจคต์คนไทยอีกคนหนึ่งโดยผ่าน interface IThaiLang)
(ออปเจคต์รถยนต์สามารถเข้าใจ message จากป้ายสัญญาณไฟจราจรได้ผ่านทาง ITrafficLight)
>ผมควรมี interface สำหรับการ ฝาก-ถอน
>หรือ
>ผมควรทำเป็น abstract method ?
ซึ่งมีเฉพาะเมธอด "ฝาก" กับ "ถอน" เท่านั้น
ซึ่งมีเมธอดอื่นๆเพิ่มเติมจาก "ฝาก" กับ "ถอน" บางเมธอดอาจจะเป็น concrete บางเมธอดอาจจะเป็น abstract
ซึ่งทุกเมธอดที่สืบทอดมาจะมีการ implement ให้เป็น concrete หมด
วันพุธที่ 25 สิงหาคม พ.ศ. 2553
[SQL] เกี่ยวกับ UNION
environment ที่ลองคือ SQL Server 2005
สรุปคือ UNION นั้น จะมีการ Distinct ข้อมูลซ้ำออกไปด้วยโดยอัตโนมัติ
ถ้าไม่ต้องการให้ลบข้อมูลซ้ำออกไป ให้เพิ่มคำว่า ALL ตามท้าย
เช่น
UNION ALL
[VB.NET Tips] ผูก Enum ใดๆ ไปกับ Combo Box
Bind a Combo Box to an Enumeration in Visual Studio .NET
Provided by: Jim Ferguson, Project Manager
An enumeration is a related set of constants. The enumeration members between the Enum and End Enum statements are initialized to constant values. Enumeration is used to group a set of constants under a common name, so the value can be referred to by a string value rather than the numeric value. For example, this enumeration provides a list of constants for the provided names:
Private Enum PeopleNames
bob
mary
jane
joe
fred
End Enum
By default, the first enumerator has a value of 0, and each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator. The value for "bob" is 0, "mary" is 1, and so on to "fred" which is 4.
It is possible to bind a combo box or a list box to the values in the enumeration so that the values in the dropdown list are the string names, but the underlying constant values can be retrieved from the selected item. The Enum class provides a static method called "GetValues" which returns an array of all items in the enumeration. You can bind a combo box to this enumeration with code such as the following:
Me.ComboBox1.DataSource = _
System.Enum.GetValues(GetType(PeopleNames))
The argument to GetValues is a type value for the enumeration you wish to retrieve. GetType returns a type object based on the name of the enumeration. Change this value to the name of the enum you are interested in.
After this line of code executes the list portion of the combo box contains the names "bob", "mary", "jane" etc. If you just want to know which name was selected you can use the .SelectedText property of the combobox. However if you want to know the constant value associated with the name, you need to use the .SelectedValue property (which is a generic object) and cast it to the type of enum you are interested in. In this example:
Dim nameid As PeopleNames = CType(ComboBox1.SelectedValue, PeopleNames)
The variable "nameid" contains a 0, or 1, or 2 etc. depending on the value of the enumerated item that is currently selected.
Reference : http://www.fmsinc.com/free/NewTips/NET/NETtip4.asp
วันศุกร์ที่ 20 สิงหาคม พ.ศ. 2553
[โจทย์ SQL] Selecting the one maximum row from each group
Target : Show minimum price of each group variety
Database
+--------+------------+-------+
| type | variety | price |
+--------+------------+-------+
| apple | gala | 2.79 |
| apple | fuji | 0.24 |
| apple | limbertwig | 2.87 |
| orange | valencia | 3.59 |
| orange | navel | 9.36 |
| pear | bradford | 6.05 |
| pear | bartlett | 2.14 |
| cherry | bing | 2.55 |
| cherry | chelan | 6.33 |
+--------+------------+-------+
Result
+--------+----------+-------+
| type | variety | price |
+--------+----------+-------+
| apple | fuji | 0.24 |
| orange | valencia | 3.59 |
| pear | bartlett | 2.14 |
| cherry | bing | 2.55 |
+--------+----------+-------+
====================================================
Solution1
select f.type, f.variety, f.price
from (
select type, min(price) as minprice
from fruits group by type
) as x inner join fruits as f on f.type = x.type and f.price = x.minprice;
Reference : http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
วันพุธที่ 18 สิงหาคม พ.ศ. 2553
[SQL] EXISTS กับ IN ต่างกันอย่างไร
ต่างกันยังไง?
EXISTS
ใช้เทียบว่า sub query นั้น มี record บ้างหรือไม่ (ไม่สนใจว่า value คืออะไร)
ถ้ามี เป็น TRUE
IN
ใช้เทียบว่าค่าที่ return จาก sub query นั้นเทียบกับ query นอก ตรงกันหรือไม่
ถ้าตรงกัน เป็น TRUE โดยเทียบทุก value
ความเห็น :
จริงๆแล้ว เป้าหมายของ 2 keyword นี้ต่างกัน แต่หากเราจะนำมาใช้เขียนเพื่อหาผลลัพท์เดียวกัน
มันก็ทำได้
แต่เท่าที่ศึกษามา ถ้าเป็นไปได้ ให้เลี่ยงการใช้ IN ซะ เพราะช้า ให้ใช้ EXISTS แทนครับ
วันพฤหัสบดีที่ 12 สิงหาคม พ.ศ. 2553
Data rate units
ในระบบ telecommunication นั้น ความสามารถในการส่งข้อมูลระหว่างอุปกรณ์ต่างๆ จะเทียบในรูปของ bits
ดังนั้น เราจึงมักจะได้ยินคำว่า "bit per second" หรือ bps
แล้ว bps หรือ mbps ที่เราเห็นกันประจำ ตอนจะเลือกใช้ package internet ซักอัน มันแปลว่าอะไร
ก่อนอื่น ต้องรู้ก่อนว่า bit กับ byte สัมพันธ์กันอย่างไร
8 bit = 1 byte
k = 1000
m = 1000 x 1000
ตัวอย่าง
1) โมเด็ม 56 k (kbps) โหลดได้ 56 x 1000 = 56000 bit ต่อวินาที
เป็น byte = 56000 / 8 = 7000 byte ต่อวินาที หรือ 7 kbytes / s
2) ADSL ยุคแรก 256 k (kbps) โหลดได้ 256 x 1000 = 256000 bit ต่อวินาที
เป็น byte = 256000 / 8 = 32000 byte ต่อวินาที หรือ 32 kbytes / s
3) ADSL สมัยนี้(วันที่ 13/08/2010) 6 เมก (mbps) โหลดได้ 6 x 1000 x 1000 = 6000000 bit ต่อวินาที
เป็น byte = 6000000 / 8 = 857,142.85 byte ต่อวินาที หรือ 857 kbytes /s หรือ วินาทีละ 0.8 mbytes /s -*- (วิละเกือบเมก)
จบครับ