詳解VB.NET Component類
VB.NET有很多值得學習的地方,這里我們主要介紹VB.NET Component類,包括介紹直接使用COM組件是通過Add Reference來實現(xiàn)等方面。
組件其實是一段可以重用的代碼,通過遵循IComponent接口的標準來實現(xiàn)一個組件,所以有組件都是派生于Component類,由VB.NET Component類來實現(xiàn)IComponent接口。在組件中應正確使用函數(shù)的訪問級別來控制外部對其的訪問限制。
只要有足夠的權限就可以將組件放到自己的程序中而不用擔心組件會產生多大的錯誤,因為組件已經(jīng)經(jīng)過測試的。比如說可以把一段登錄的程序做成一個組件,或者把經(jīng)常使用到的一些功能也做成組件,這樣就可以減少開發(fā)中的錯誤,也可以縮短開發(fā)時間。組件之間也可以互相套用,如一個組件引用另一個組件,都是沒問題,但要先在Add Reference中添加對組件的引用,在.NET中是通過把組件放在程序集中來實現(xiàn)的,程序集中存放著這些組件所依賴的文件信息和所在路徑,因此CLR 就可以通過這些信息來確定組件所需要的其他程序集的位置。
在VS中創(chuàng)建組件:選建一個Project,再從模板中選Class Library,OK。接著再從Project菜單中Add Component,到些為止,組件的一個框架就呈現(xiàn)在眼前,平臺自動繼承了VB.NET Component類和構造函數(shù)。可以刪除原先創(chuàng)建類庫時自動生成的 Class1,看應用的需要。接著就可以在組件類里寫要實現(xiàn)的功能,***從Build(生成)菜單中選擇Build Solution(生成解決方案)來生成組件。如果生成成功的話,到應用程序的BIN目錄下會看到一個DLL文件。
引用組件:只要在Solution Explorer窗口中,添加對DLL的Reference就可以了。
- Imports loginValidator
- Imports System.Data
- Imports System.Data.SqlClient
- Public Class loginFormClass loginForm
- Inherits System.Windows.Forms.Form
- #Region " Windows 窗體設計器生成的代碼 "
- Public Sub New()Sub New()
- MyBase.New()
- '該調用是 Windows 窗體設計器所必需的。
- InitializeComponent()
- '在 InitializeComponent() 調用之后添加任何初始化
- End Sub
- '窗體重寫 dispose 以清理組件列表。
- Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- 'Windows 窗體設計器所必需的
- Private components As System.ComponentModel.IContainer
- '注意: 以下過程是 Windows 窗體設計器所必需的
- '可以使用 Windows 窗體設計器修改此過程。
- '不要使用代碼編輯器修改它。
- Friend WithEvents lblUserPwd As System.Windows.Forms.Label
- Friend WithEvents lblUserName As System.Windows.Forms.Label
- Friend WithEvents txtUserName As System.Windows.Forms.TextBox
- Friend WithEvents txtUserPwd As System.Windows.Forms.TextBox
- Friend WithEvents btnSubmit As System.Windows.Forms.Button
- Friend WithEvents btnExit As System.Windows.Forms.Button
- Friend WithEvents Label1 As System.Windows.Forms.Label
- Friend WithEvents Label2 As System.Windows.Forms.Label
- Friend WithEvents btnCancel As System.Windows.Forms.Button
- Friend WithEvents Label3 As System.Windows.Forms.Label
- <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()Sub InitializeComponent()- Dim resources As System.Resources.ResourceManager =
New System.Resources.ResourceManager(GetType(loginForm))- Me.lblUserPwd = New System.Windows.Forms.Label
- Me.lblUserName = New System.Windows.Forms.Label
- Me.txtUserName = New System.Windows.Forms.TextBox
- Me.txtUserPwd = New System.Windows.Forms.TextBox
- Me.btnSubmit = New System.Windows.Forms.Button
- Me.btnExit = New System.Windows.Forms.Button
- Me.Label1 = New System.Windows.Forms.Label
- Me.Label2 = New System.Windows.Forms.Label
- Me.btnCancel = New System.Windows.Forms.Button
- Me.Label3 = New System.Windows.Forms.Label
- Me.SuspendLayout()
關于VB.NET Component類:.NET可以向后兼容,并支持了COM和ActiveX對象等早期版本的應用程序。在.NET中使用COM等很有趣,.NET創(chuàng)建一個包將它們包裝起來,而它們之間的交互就是通過這個包裝來進行的,這個包就叫做運行時可呼叫包裝(RCW)??梢灾苯邮褂肅OM,也可以先轉換成.NET程序集后再使用。
1、直接使用COM組件是通過Add Reference來實現(xiàn)的,這種方法通過RCW來包裝。缺點:無法放到 GAC中,不能重用。
2、通過轉換成.NET程序集來使用,是通過利用 tlbimp 命令行工具來實現(xiàn)的。
***個為COM的名稱,第二個為要生成的.NET組件名稱,第三個為要生成的名稱空間,第四個為 版本號,第四個指定引用的文件名。
【編輯推薦】