top
Loading...
VBCOM基礎講座之類的測試
現在就來測試前面創建的類。

按F5運行程序;在彈出的屬性對話框中,選中"Wait for Components to Start"(啟動工程時等待創建部件),然后按[OK]按鈕;

這時,類就會被激活,其他程序就可使用它的功能。

再次運行Visual Basic另一個實例;

創建一個新的"Standard EXE"工程;

選擇"'Project"->"References"菜單;

瀏覽對話框中可引用的列表項,可以發現一些額外的組件。

選中"Northwind"列表項;

Northwind就是前面創建的ActiveX工程。

單擊[OK]按鈕;

現在添加一些代碼來使用上述工程:

在Form1表單中添加一個命令按鈕;為命令按鈕添加下列代碼:

Dim Test As Customers
Set Test = New Customers
MsgBox Test.CustomerID
Set Test = Nothing

該代碼首先創建一個新的Customers對象,然后顯示CustomerID信息,最后將Test對象置為Nothing,并關閉它。

按F5鍵運行測試程序;

需要說明的是,當運行時出現"invalid reference"錯誤提示時,肯定哪些地方有問題。這時可按下面步驟重新來一次:

(1) 在測試工程中去掉Northwind引用;

(2) 重新啟動Northwind工程;

(3) 在測試工程中添加Northwind引用,再運行!

單擊表單中的命令按鈕;

這時運行時可能需要幾秒鐘,畢竟還要做一些如數據庫連接等工作。但是,除了一開始的停留外,后面的調用就快得多了。程序將顯示包含"ALFKI"的消息對話框。

關閉測試程序。

現在,我們來看看程序背后究竟發生什么。

將插入符移動到MsgBox Test.CustomerID這條語句上;按F9;

該語句顯示為紅色,用來標記一個斷點。當代碼運行時,它會停留在這里。按F8將單步運行此語句,并移動到下一句代碼上。

按F5再次運行測試程序;

單擊命令按鈕;

流程將停留在MsgBox這條命令上。

按F8,慢慢單步執行各條語句;

將會看到系統在兩個Visual Basic中來回切換,顯示出不同屬性的處理過程。

結束后,關閉測試程序。

下面再對前面的工程進行測試。這一次,我們不僅獲取CustomerID的值,而且還設置這個值。

將命令按鈕的代碼改為:

Dim Test As Customers
Set Test = New Customers
Test.CustomerID = "KARLY"
Test.Update
MsgBox Test.CustomerID
Set Test = Nothing

該代碼首先設置"CustomerID"字段,然后更新記錄集,最后顯示出CustomerID屬性,其結果應該是設置的"KARLY"。

假如愿意,仍然可以按F9高亮顯示"Test.CustomerID =" 這條語句,然后按F8單步運行來查看其工作情況。

至此,我們已經成功地創建并測試一個簡單的基于數據庫的類。但是,還沒有對customerID的字符串長度作測試,如果其長度超過5個字符,看看會發生什么?

下一步,我們將擴充并改進這個數據庫類。

首先添加類的幾個特征:其他的屬性、一些方法甚至一兩個事件。 其相應的代碼如下:

Dim WithEvents rs As Recordset
Public Event RecordsetMove()
Private Sub Class_Initialize()
Set rs = New Recordset
rs.ActiveConnection = "Provider=Microsoft." & _"Jet.OLEDB.4.0;Data Source=C:Program Files" & _"Microsoft Visual StudioVB98Nwind.mdb;" & _"Persist Security Info=False"
rs.Open "select * from customers", , adOpenKeyset, adLockOptimistic
End Sub

Private Sub Class_Terminate()
rs.Close
Set rs = Nothing
End Sub

Public Property Get CustomerID() As String
CustomerID = rs("CustomerID")
End Property

Public Property Let CustomerID(NewValue As String)
'If the length of NewValue is greater than five
If Len(NewValue) > 5 Then
'... then raise an error to the program
'using this class, by running
'Err.Raise vbObjectError + OurErrorNumber
Err.Raise vbObjectError + 1, "CustomerID", _"Customer ID can only be up to five " & _ "characters long!"

Else
'... otherwise, change the field value
rs("CustomerID") = NewValue
End If
End Property
Public Property Get CompanyName() As Variant
CompanyName = rs("CompanyName")
End Property

Public Property Let CompanyName(ByVal NewValue As Variant)
rs("CompanyName") = NewValue
End Property

Public Property Get ContactName() As Variant
ContactName = rs("ContactName")
End Property

Public Property Let ContactName(ByVal NewValue As Variant)
rs("ContactName") = NewValue
End Property

Public Property Get ContactTitle() As Variant
ContactTitle = rs("ContactTitle")
End Property

Public Property Let ContactTitle(ByVal NewValue As Variant)
rs("ContactTitle") = NewValue
End Property

Public Property Get Address() As Variant
Address = rs("Address")
End Property

Public Property Let Address(ByVal NewValue As Variant)
rs("Address") = NewValue
End Property

Public Property Get City() As Variant
City = rs("City")
End Property

Public Property Let City(ByVal NewValue As Variant)
rs("City") = NewValue
End Property

Public Property Get Region() As Variant
Region = rs("Region")
End Property

Public Property Let Region(ByVal NewValue As Variant)
rs("Region") = NewValue
End Property

Public Property Get PostalCode() As Variant
PostalCode = rs("PostalCode")
End Property

Public Property Let PostalCode(ByVal NewValue As Variant)
rs("PostalCode") = NewValue
End Property

Public Property Get Country() As Variant
Country = rs("Country")
End Property

Public Property Let Country(ByVal NewValue As Variant)
rs("Country") = NewValue
End Property

Public Property Get Phone() As Variant
Phone = rs("Phone")
End Property

Public Property Let Phone(ByVal NewValue As Variant)
rs("Phone") = NewValue
End Property

Public Property Get Fax() As Variant
Fax = rs("Fax")
End Property

Public Property Let Fax(ByVal NewValue As Variant)
rs("Fax") = NewValue
End Property

Public Sub AddNew()
rs.AddNew
End Sub

Public Sub Update()
rs.Update
End Sub

Public Sub CancelUpdate()
If rs.EditMode = adEditInProgress Or _rs.EditMode = adEditAdd Then
rs.CancelUpdate
End If
End Sub
Public Sub MoveNext()
rs.MoveNext
End Sub

Public Sub MovePrevious()
rs.MovePrevious
End Sub

Public Sub MoveFirst()
rs.MoveFirst
End Sub

Public Sub MoveLast()
rs.MoveLast
End Sub

Public Function FindByCustomerID(CustomerID As String) As Boolean
'Uses the Find method to locate customers
'with a matching CustomerID.
'Returns True value is customer(s) found
Dim varBookmark As Variant
rs.MoveFirst
rs.Find ("CustomerID='" & CustomerID & "'")
If rs.EOF = True Then
FindByCustomerID = False
rs.Bookmark = varBookmark
Else
FindByCustomerID = True
End If
End Function

Public Property Get EOF() As Boolean
'Example of a read-only property
No Property Lets here
EOF = rs.EOF
End Property
Public Property Get BOF() As Boolean
'Another example of a read-only property
BOF = rs.BOF
End Property
Private Sub rs_MoveComplete(ByVal adReason As ADODB.EventReasonEnum, _
ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, _
ByVal pRecordset As ADODB.Recordset)

'Reacts to the recordset MoveComplete
'method - raises event with each move
RaiseEvent RecordsetMove
End Sub

需要說明的是:迄今為止,我們僅僅是在一個類中添加代碼。當然,也可以選擇"Project"->"Add Class"菜單來向工程添加多個類,而且還可利用"collections"使這些類工作在一起。但是在這里,我們仍然想用一個類來處理一個數據表。

將上述類的代碼復制并粘貼到自己的類中,下一節將討論該程序的編譯。

作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗