转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
VB COM基础讲座之类的测试         ★★★★

VB COM基础讲座之类的测试

作者:闵涛 文章来源:闵涛的学习笔记 点击数:606 更新时间:2009/4/23 15:03:01
  现在就来测试前面创建的类。

  按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")
  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"使这些类工作在一起。但是在这里,我们仍然想用一个类来处理一个数据表。

  将上述类的代码复制并粘贴到自己的类中,下一节将讨论该程序的编译。


[VB.NET程序]GSM短信模块库函数,可以用VB,VC,调用简单实用  [办公软件]PowerPoint做交互课件之弃用VBA
[办公软件]VBA获取U盘、主板、CPU序列号和网卡MAC地址  [办公软件]VBA设置文件属性及加密源代码示例
[办公软件]VBA中初始化ADO连接的几种方法  [网络安全]“VB破坏者变种N”病毒摘要
[Web开发]ASP.NET上传文件到数据库VB版  [办公软件]在Excel中利用VBA实现多表单元格数据的读取与赋值…
[办公软件]使用Vba读取已关闭的Excel工作薄数据到当前工作表…  [办公软件]Excel编程基础之VBA文件操作详解
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台