手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

VB COM基础讲座之类的测试

来源:互联网 作者:西部数码 时间:2008-04-09
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
  现在就来测试前面创建的类。

  按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 Studio\VB98\Nwind.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")

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!