1 / 46

VB.NET

VB.NET. The most powerful language for developer. 昇級 Visual Basic 技術到 Visual Basic. NET. 高文雄 台灣微軟 特約資深講師 .NET 技術 專欄 / 自由作家. 講師簡介. 現任 : 源碼科技 技術長 台灣微軟 特約資深講師 曾任 : 科技島股份有限公司 專案開發部門 經理 資訊技術部門 副理

wyman
Télécharger la présentation

VB.NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VB.NET The most powerful language for developer

  2. 昇級Visual Basic 技術到Visual Basic. NET 高文雄台灣微軟 特約資深講師 .NET技術 專欄/自由作家

  3. 講師簡介 現任: 源碼科技 技術長 台灣微軟 特約資深講師 曾任: 科技島股份有限公司 專案開發部門 經理 資訊技術部門 副理 專長: 主從式架構、多層式應用程式架構設計、 資料庫理論、系統分析、專案管理。 經歷: MCSD、 MCSE、 MCDBA、 MCPS。 Visual Studio.net 上市發表會--特約講師 微軟大型技術研討會--特約講師。 自由作家/譯者 專案開發經理 取得聯繫的方式: justin@sourcecode.com.tw.

  4. 議程大綱 • VB6 、VB.NET語言的差異 • VB.NET新功能介紹及範例 • 昇級現有的 VB 6 專案程式

  5. VB6 v.s VB.NET • 資料型態 • 使用變數 • 副程式與函式 • 例外處理 • 物件方式

  6. 資料型態 • Common Type System • VB.NET 新增的資料型態類型 • VB.NET 與VB6 不同的資料型別 • 使用Ctype 轉換不同的資料型別

  7. Common Type System 1/2 • 所有.NET語言的共通型別系統 • 提供型別安全(Type-Safety) • 建立在System.Object Class (.NET Framework Class Library) • 實值型別 vs.參考型別 (Value-Type vs. Reference-Type Variables)

  8. Common Type System 2/2

  9. New Data Type Storage Size Value Range 2 bytes 0-65535 Char -32768 至32767 Short 2 bytes Decimal 12 bytes 28digits(包含小數點前後) VB.NET 新的資料型態

  10. VB.NET與VB6不同的資料型別 VB6 VB.NET Integer(16bit) Short Long(32bit) Integer Long(64bit) Variant Not Supported: use Object Currency Not Supported: use Decimal Not Supported String(fixed length)

  11. 資料型態轉換 • VB6的資料型態轉換方式 CInt()、 CStr()、… • 使用新的Ctype() 轉換方式 語法:Ctype(expression, type_name) • 物件的資料型態轉換方式 s=i.toString i=Integer.Parse(s)

  12. 型態轉換--範例 • VB 6 Dim i as integer,s as string i=10 s=Cstr(i) • VB.NET Dim i as integer, s as string i=10 s=Cstr(i) ‘VB6延用的使用方式 s=Ctype(i,string) ‘CType的使用方式 s=i.ToString ‘物件的使用方式 i=Integer.Parse(s) ‘字串轉數字

  13. 使用變數 • 宣告及初始化變數(及陣列) • 變數範圍 • 建立 Data Structures • 編譯選項設定 • 使用Shorthand 方式指定變數值

  14. 宣告及初始化變數 • 同步宣告及初始化變數 Dim x as integer = 10 • 多個變數宣告 Dim x , y, z as integer

  15. 宣告陣列 • 陣列的基底為 0 • 不能指定 lower bound To upper bound方式 • Redim陣列

  16. 陣列使用--範例 1/2 • VB 6 Dim month(1 to 6) as Integer • VB.NET Dim month(5) As Integer‘0,1,2,3,4,5 共6個註標 ‘Length=6 ‘Upper Bound=5 Dim month() As Integer = {1, 2, 3, 4, 5, 6}

  17. VB6 陣列使用(Redim) --範例 2/2 Dim x () as string ‘在vb6 正確的Redim方式 ReDim x(5)as string Dim y (2) as string ‘在vb6 錯誤的Redim方式 ReDim y(5) as string • VB.net Dim x () as string ‘在vb.net正確的Redim方式 ReDim x(5)as string Dim y (2) as string ‘在vb.net 允許的Redim方式 ReDim Preserve y(5) as string

  18. 變數範圍 • Procedure Scope --在整個程序中變數都是可見的 • Block Scope --適用在If … Else … End If, For … Next, Do … Loop, While … End While, Select … Case … End Select

  19. 變數範圍--範例 Sub AddTen() Dim I as integer ‘Procedure Scope ‘變數只有在區塊中是可見的 Msgbox(i.ToString) ‘成功,變數在程序範圍內 Msgbox(x.ToString) ‘錯誤, 變數已超出範圍外 End Sub • For i= 0 to 10 • dim x as integer ‘Block Scope • x = i • Next

  20. 建立 Data Structures • Data Structures 取代VB6的使用者自訂型態(UDT)功能

  21. VB 6.0 Private Type myCustomer Name As String Address As String Age As Integer End Type VB .NET Structure myCustomer Dim Name As String Dim Address As String Dim Age As Integer End Structure Data Structure--範例

  22. 編譯選項設定 • Option Explicit (default on 指定變數必須經過宣告) • Option Strict On:不允許物件進行「晚期連結」 Off:可以允許物件的「晚期連結」 Default: Strict off

  23. Option Strict—範例 • VB.NET • Option Strict on ‘不允許物件進行「晚期連結」 • …略 • Dim x as object • X=“1234” • Msgbox(x.chars(3) ) ‘錯誤,string物件的chars方法只有在 • ‘ run-time時期可視,在Strict on情況 • ‘ 不允許物件進行 Late binding.

  24. 使用Shorthand 方式指定變數值 • 簡化變數指定運算式: • *= • /+ • += • -= • &=

  25. 使用Shorthand ---範例 • Dim z as integer =0 • Dim s as string =“First” • z += 1 ‘z= z+1 • s &= “Second” ‘s= s & “Second”

  26. 副程式與函式 • 不論是否有回傳值都必須使用括號()帶入函式的參數值 • 引數預設是「傳值(byVal)」 • 選項參數(Optional)必須有預設值 • 使用return 回傳Function 的回傳值

  27. Option Parameter—範例 • VB 6.0 : Function add(Optional ByVal X as integer)As Integer If IsMissing(X) Then … End Sub • VB .NET : Function add(Optional ByVal X As Integer = 0) As Integer … End Function

  28. Return回傳值---範例 • VB6 Function getData () as string …略 getData=“Result Data” End Function • VB.NET Function getData () as string …略 getData=“Result Data” ‘或者是 Return “Result Data” End Function

  29. 使用參數陣列(ParamArrays) • VB .NET 中的 ParamArrays 參數 • 使用 ByVal 傳遞( VB 6.0 是 ByRef ) • 可以為任何型態( VB 6.0 是 Variant ) • 不可以與 Optional 共用

  30. 參數陣列---範例 Sub Test(ByVal ParamArray X( ) As Object) Dim I As Integer For I = X.GetLowerBound(0) To X.GetUpperBound(0) MsgBox(X(I)) Next End Sub

  31. 例外處理(一) • 結構化的例外處理機制 • 使用 Try…Catch 捕捉錯誤訊息 • System.Exception Class • 提供產生例外的相關訊息(ex:Message,Source…) • Throw 例外

  32. 例外處理---範例 2/2 • Try...Catch Dim i1, i2, i3 As Decimal i1 = 22 i2 = 0 Try i3 = i1 / i2 MsgBox(i3.ToString) Catch eExcption As Exception MsgBox(eExecption.Message) Finally MsgBox("the end") End Try

  33. 例外處理—範例 2/2 • Throw 例外 …略 • Try • If i2 = 0 Then • Throw New Exception("i2 equals zero") • Else • Throw New Exception("i2 does not equal zero") • End If • Catch eException As Exception • MsgBox("Message: " & eException.Message) • Finally • MsgBox("finally block") • End Try

  34. 物件使用 • 物件指派方式 VB 6.0: Set objX = TextBox1 VB .NET: objX = TextBox1 • 大多數物件不再支援「預設屬性」功能 VB 6.0 : TextBox1 = “1234” VB .NET: TextBox1.Text = “1234”

  35. Visual Basic.NET 新功能 • 主要的語言功能加強 • 完整「Object-Oriented Programming」 • 提供「Structured exception handling」 • 與.NET Framework 整合能力 • 開發Multithreaded • Garbage Collection • 提供Web 應用程式開發能力 • Web Forms • Web Services

  36. 表單繼承 • 為你的應用程式建立基礎表單 • 繼承「基礎表單」並加入新功能 • 基礎表單修改時所有的子表單一併修改 • 加速應用程式開發以及簡化共通程式設計

  37. 基礎表單 繼承表單 表單繼承

  38. Multithread • .NET Framework 提供新的類別功能 • System.Threading.Thread • 使用Start方法開始一個Thread物件 • 使用Abort方法結束一個Thread物件 • 使用Priority屬性設定Thread執行順序

  39. Multithreaded

  40. 昇級現有的 VB 6 專案程式 1/3 • 昇級前的準備 • 將VB1 至VB5專案無法直接昇級VB.NET -必須先昇級到VB6的專案型態 • 以ADO取代RDO、DAO資料存取方式 -ADO.NET不支援RDO、DAO的data binding to controls,data controls,或者是RDO 的User Connection Notes:DHTML、ActiveX Document等VB6專案型態無法昇級至VB.NET(但可以共存)。另外,建議將不需要進行Distributed Transactions的 COM/COM+ 元件以手動的方式昇級成為Web Service 元件型式(.NET middle-tier Component)

  41. 昇級現有的 VB 6 專案程式 2/3 • 建議在VB6中進行程式碼修改的部份 • 物件以Early-Binding取代Late-Binding • 「日期型態」資料改以date資料型態定義 -不要使用Double資料型態;VB.NET不支援Date/Double兩者之間直接進行資料轉換的動作 • 避免使用物件的「預設屬性」設計方式 -特別是當物件是使用late-binding時會造成在VB.NET的編譯錯誤 • 修改不適合的陣列 宣告/使用 方式 -Dim x (1 to 6) as integer (修正為 Dim x(6) as integer) -取消任何Option Base 1的編譯選項設定

  42. 昇級現有的 VB 6 專案程式 3/3 • 使用昇級精靈 • 建立一個新的VB.NET專案 -原有的VB6專案會被保留 • 將VB6的表單、類別物件更換為新的.NET 物件 -VB6 Forms 將轉換為Windows Forms • 在新的專案中建立昇級報告 -詳列出所有可能的錯誤或未完成昇級的部份

  43. 使用昇級精靈

  44. 總結 • 使用VB.NET新的語言能力 Object Oriented Programming • 充分理解.NET Framework 應用Base Class library • 昇級你的VB6應用程式專案 使用昇級精靈

  45. 相關資訊 MSDN(美國微軟) htt://msdn.microsoft.com http://msdn.microsoft.com/net MSDN(台灣「技術開發人員社群」網站) http://www.microsoft.com/taiwan/msdn/community

  46. Question

More Related