Early and Late Binding (Visual Basic)


นานๆ จะมี case ที่เกี่ยวกับ VB เกิดขึ้น ครั้งนี้เกิดขึ้นจากน้องในทีมมีปัญหาเรื่อง VB Application ที่เขียนอยู่ใน Excel ซึ่งไม่ support กับ Windows 7 (ซะงั้น)

สาเหตุที่พบเนื่องจาก VB ที่น้องเขียนมีการ call web service ที่พวกเราเขียนกันขึ้นมา แล้ว VB Application ใน Excel ต้อง call SOAP XML แต่ขึ้นว่า DLL เกี่ยวกับ SOAP XML Error ผมเองก็แก้ไม่ได้

หลังจากผ่านไป 1 อาทิตย์ แล้วเทพก็มาจุติ...พี่จิ๋ว นี่เอง....(คนนี้ เทพมาก) พี่จิ๋วนั่งทำไม่ถึง 15 นาที
สิ่งที่พี่จิ๋วอธิบยให้ผม+น้อง+ทีมงาน TSD ฟัง ".......ให้เปลี่ยนจาก Early binding เป็น Late biding ....."
ทุกคนอิ้งไปพักหนึ่ง Zzz (-_-")  มีแต่ผมที่สบตาแล้วคุยกับพี่จิ๋วต่อ...

ผมเลยมาสรุปให้น้อง:
Early Binding
จากตัวอย่าง โดยปกติ เวลาเขียน VB เราจะเขียนประกาศตัวแปรพร้อมระบุ Object Type ดังตัวอย่าง Code ด้านล่าง

' Create a variable to hold a new object.
Dim FS As System.IO.FileStream
' Assign a new object to the variable.
FS = New System.IO.FileStream("C:\tmp.txt",  System.IO.FileMode.Open)
ตัวแปร FS ถูกระบุ Object Type เป็น System.IO.FileStream ซึ่ง Complier ของ VB จะทำการตรวจสอบและจองหน่วยความจำ และ compile ก่อนที่โปรแกรมจะ Execute ซึ่งยังไม่ทันทำอะไรมันก็ Error ซะแล้ว

Late Binding

จาก code ด้านล่าง จะเห็นได้ว่าเราจะแค่ประกาศตัวแปรเอาไว้ โดยไม่ระบุ Object Type

' To use this example, you must have Microsoft Excel installed on your computer.
' Compile with Option Strict Off to allow late binding.
Sub TestLateBinding()
   Dim xlApp As Object  
   Dim
xlBook As Object
   Dim xlSheet As Object
xlApp = CreateObject("Excel.Application")
' Late bind an instance of an Excel workbook.
xlBook = xlApp.Workbooks.Add
' Late bind an instance of an Excel worksheet.
xlSheet = xlBook.Worksheets(1)
xlSheet.Activate()
' Show the application.
xlSheet.Application.Visible = True
' Place some text in the second row of the sheet.
xlSheet.Cells(2, 2) = "This is column B row 2"
End Sub
เช่น
  
   Dim xlApp As Object

จากนั้นเมื่อเราต้องการใช้งาน object เราจะทำการ create new object ขึ้นมา assign ให้ ตัวแปรนั้นอีกที

    xlApp = CreateObject("Excel.Application")
ผลลัพธ์อาจจะไม่ต่างกันแต่ Microsoft recommend ว่าถ้าเป็นไปได้ให้เลือกใช้ Early binding เพราะ complier จะทำงานได้อย่างมีประสิทธิภาพมากกว่า (เนื่องจากมันรู้ขนาดของ Object ทีแท้จริงตั้งแต่ตอนประกาศ) และ Early binding ยังทำงานได้เร็วกว่า ง่ายในการอ่านโค้ด และ maintain ง่ายกว่า Late binding


Advantages of Early Binding
You should use early-bound objects whenever possible, because they allow the compiler to make important optimizations that yield more efficient applications. Early-bound objects are significantly faster than late-bound objects and make your code easier to read and maintain by stating exactly what kind of objects are being used. Another advantage to early binding is that it enables useful features such as automatic code completion and Dynamic Help because the Visual Studio integrated development environment (IDE) can determine exactly what type of object you are working with as you edit the code. Early binding reduces the number and severity of run-time errors because it allows the compiler to report errors when a program is compiled.

Note:
Late binding can only be used to access type members that are declared as Public. Accessing members declared as Friend or Protected Friend results in a run-time error.

Reference from :http://msdn.microsoft.com/en-us/library/0tcf61s1.aspx

Comments

Popular posts from this blog

Maximum request length exceeded

Review : HONDA HR-V Part 2

Mini review : SIGMA 85mm. f/1.4 EX DG