工具软件   办公软件   操作系统   网络安全   设计在线   程序开发   教程宝典   软件下载   软件论坛
您的位置:软件 > 开发者网络 > 开发工具 > 开发专栏 > VB > 正文
VB COM基础讲座之类的测试
[文章信息]
作者:ADDING
时间:2004-09-11
出处:天极网
责任编辑:方舟
[文章导读]
按F5运行程序;在弹出的属性对话框中
advertisement
热点推荐
· 真没想到VB也可以这样用之指针技术
· 禁止QQ登录的方法
· 给你的XML文件做个数字签名
· ImageReady制作“焰火”小动画
· Java加密和数字签名编程快速入门
[正文]
  现在就来测试前面创建的类。

  按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实现Win2000用户限时登录
  • Visual Basic内嵌汇编语言解决方案
  • VB COM基础讲座之添加属性和方法
  • VB COM基础讲座之创建第一个COM对象
  • Visual Basic不可能消失
  • 民营家电商排队造手机 设备商全面杀入
  • 英特尔澄清杨旭任职传闻 官方没宣布此消息
  • 国资委河北密制联通拆分方案
  • 垃圾邮件害人害企害国 清除垃圾邮件不手软
  • 中兴携手阿尔卡特 全球逐鹿CDMA
  • 用友总裁王文京:誓将ERP变成“大众消费”
  • 香港消费者委员会:数码相机最贵未必最好
  • 外电称中兴正评估西门子手机业务 或能并购
  • Advertisement

    天极无线


    奇妙科幻|美好风光|清风车影|漫画卡通|星座生肖|明星写真|动物世界
    老鼠爱大米
    挥着翅膀的女孩
    女人味
    栀子花开
    白月光
    刚刚好
    江南
    快乐崇拜
    亲爱的你怎么不在我身边
    小薇
    2002年的第一场雪
    有多少爱可以重来
    我的地盘
    七里香
    情人
     
    老鼠爱大米 老板电话
    冲动的惩罚 七里香
    我不是黄蓉 女生撒娇
    盛夏的果实 坚持到底
    孤单北半球 眉飞色舞
    挪威的森林 可爱女人
    最浪漫的事 老板电话

    CSEEK搜索