VBCOM基础讲座之类的测试_VB.net_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

VBCOM基础讲座之类的测试

作者:黑客防线网安VB教程基地 来源:黑客防线网安VB教程基地 浏览次数:0

本篇关键词:测试之类讲座基础
黑客防线网安网讯:现在就来测试前面创建的类。    按F5运行程序;在弹出的属性对话框中,选中"WaitforComponentstoStart"(启动工程时等待创建部件),然后按[OK]按钮;  -align="right">->->->  这时,...
现在就来测试前面创建的类
  
  按F5运行程序;在弹出的属性对话框中选中"WaitforComponentstoStart"(启动工程时等待创建部件)然后按[OK]按钮;
  -align="right">->->->
  这时,类就会被激活,其他程序就可使用它的功能
  
  再次运行VisualBasic另一个实例;
  
  创建一个新的"StandardEXE"工程;
  
  选择"'Project"->"References"菜单;
  
  浏览对话框中可引用的列表项,可以发现一些额外的组件。
  
  选中"Northwind"列表项;
  
  Northwind就是前面创建的ActiveX工程。
  
  单击[OK]按钮;
  
  现在添加一些代码来使用上述工程:
  
  在Form1表单中添加一个命令按钮;为命令按钮添加下列代码:
  
  ->  DimTestAsCustomers
  SetTest=NewCustomers
  MsgBoxTest.CustomerID
  SetTest=Nothing->

  该代码首先创建一个新的Customers对象,然后显示CustomerID信息,最后将Test对象置为Nothing,并关闭它。
  
  按F5键运行测试程序;
  
  需要说明的是,当运行时出现"invalidreference"错误提示时,肯定哪些地方有问题。这时可按下面步骤重新来一次:
  
  (1)在测试工程中去掉Northwind引用;
  
  (2)重新启动Northwind工程;
  
  (3)在测试工程中添加Northwind引用,再运行!
  
  单击表单中的命令按钮;
  
  这时运行时可能需要几秒钟,毕竟还要做一些如数据库连接等工作。但是,除了一开始的停留外,后面的调用就快得多了。程序将显示包含"ALFKI"的消息对话框。
  
  关闭测试程序。
  
  现在,我们来看看程序背后究竟发生什么。
  
  将插入符移动到MsgBoxTest.CustomerID这条语句上;按F9;
  
  该语句显示为红色,用来标记一个断点。当代码运行时,它会停留在这里。按F8将单步运行此语句,并移动到下一句代码上。
  
  按F5再次运行测试程序;
  
  单击命令按钮;
  
  流程将停留在MsgBox这条命令上。
  
  按F8,慢慢单步执行各条语句;
  
  将会看到系统在两个VisualBasic中来回切换,显示出不同属性的处理过程。
  
  结束后,关闭测试程序。
  
  下面再对前面的工程进行测试。这一次,我们不仅获取CustomerID的值,而且还设置这个值。
  
  将命令按钮的代码改为:
  
  ->  DimTestAsCustomers
  SetTest=NewCustomers
  Test.CustomerID="KARLY"
  Test.Update
  MsgBoxTest.CustomerID
  SetTest=Nothing->

  该代码首先设置"CustomerID"字段,然后更新记录集,最后显示出CustomerID属性,其结果应该是设置的"KARLY"。
  
  假如愿意,仍然可以按F9高亮显示"Test.CustomerID="这条语句,然后按F8单步运行来查看其工作情况。
  
  至此,我们已经成功地创建并测试一个简单的基于数据库的类。但是,还没有对customerID的字符串长度作测试,如果其长度超过5个字符,看看会发生什么?
  
  下一步,我们将扩充并改进这个数据库类。
  
  首先添加类的几个特征:其他的属性、一些方法甚至一两个事件。其相应的代码如下:
  
  ->  DimWithEventsrsAsRecordset
  PublicEventRecordsetMove()
  PrivateSubClass_Initialize()
   Setrs=NewRecordset
   rs.ActiveConnection="Provider=Microsoft."&_"Jet.OLEDB.4.0;DataSource=C:ProgramFiles"&_"MicrosoftVisualStudioVB98Nwind.mdb;"&_"PersistSecurityInfo=False"
   rs.Open"select*fromcustomers",,adOpenKeyset,adLockOptimistic
  EndSub
  
  PrivateSubClass_Terminate()
   rs.Close
   Setrs=Nothing
  EndSub
  
  PublicPropertyGetCustomerID()AsString
   CustomerID=rs("CustomerID")
  EndProperty
  
  PublicPropertyLetCustomerID(NewValueAsString)
   'IfthelengthofNewValueisgreaterthanfive
   IfLen(NewValue)>5Then
  '...thenraiseanerrortotheprogram
  'usingthisclass,byrunning
  'Err.RaisevbObjectError OurErrorNumber
  Err.RaisevbObjectError 1,"CustomerID",_"CustomerIDcanonlybeuptofive"&_"characterslong!"
  
   Else
  '...otherwise,changethefieldvalue
  rs("CustomerID")=NewValue
   EndIf
  EndProperty
  PublicPropertyGetCompanyName()AsVariant
   CompanyName=rs("CompanyName")
  EndProperty
  
  PublicPropertyLetCompanyName(ByValNewValueAsVariant)
   rs("CompanyName")=NewValue
  EndProperty
  
  PublicPropertyGetContactName()AsVariant
   ContactName=rs("ContactName")
  EndProperty
  
  PublicPropertyLetContactName(ByValNewValueAsVariant)
  rs("ContactName")=NewValue
  EndProperty
  
  PublicPropertyGetContactTitle()AsVariant
   ContactTitle=rs("ContactTitle")
  EndProperty
  
  PublicPropertyLetContactTitle(ByValNewValueAsVariant)
   rs("ContactTitle")=NewValue
  EndProperty
  
  PublicPropertyGetAddress()AsVariant
   Address=rs("Address")
  EndProperty
  
  PublicPropertyLetAddress(ByValNewValueAsVariant)
   rs("Address")=NewValue
  EndProperty
  
  PublicPropertyGetCity()AsVariant
   City=rs("City")
  EndProperty
  
  PublicPropertyLetCity(ByValNewValueAsVariant)
   rs("City")=NewValue
  EndProperty
  
  PublicPropertyGetRegion()AsVariant
  Region=rs("Region")
  EndProperty
  
  PublicPropertyLetRegion(ByValNewValueAsVariant)
   rs("Region")=NewValue
  EndProperty
  
  PublicPropertyGetPostalCode()AsVariant
   PostalCode=rs("PostalCode")
  EndProperty
  
  PublicPropertyLetPostalCode(ByValNewValueAsVariant)
   rs("PostalCode")=NewValue
  EndProperty
  
  PublicPropertyGetCountry()AsVariant
   Country=rs("Country")
  EndProperty
  
  PublicPropertyLetCountry(ByValNewValueAsVariant)
   rs("Country")=NewValue
  EndProperty
  
  PublicPropertyGetPhone()AsVariant
  Phone=rs("Phone")
  EndProperty
  
  PublicPropertyLetPhone(ByValNewValueAsVariant)
   rs("Phone")=NewValue
  EndProperty
  
  PublicPropertyGetFax()AsVariant
   Fax=rs("Fax")
  EndProperty
  
  PublicPropertyLetFax(ByValNewValueAsVariant)
   rs("Fax")=NewValue
  EndProperty
  
  PublicSubAddNew()
   rs.AddNew
  EndSub
  
  PublicSubUpdate()
   rs.Update
  EndSub
  
  PublicSubCancelUpdate()
   Ifrs.EditMode=adEditInProgressOr_rs.EditMode=adEditAddThen
  rs.CancelUpdate
   EndIf
  EndSub
  PublicSubMoveNext()
   rs.MoveNext
  EndSub
  
  PublicSubMovePrevious()
   rs.MovePrevious
  EndSub
  
  PublicSubMoveFirst()
   rs.MoveFirst
  EndSub
  
  PublicSubMoveLast()
   rs.MoveLast
  EndSub
  
  PublicFunctionFindByCustomerID(CustomerIDAsString)AsBoolean
   'UsestheFindmethodtolocatecustomers
   'withamatchingCustomerID.
   'ReturnsTruevalueiscustomer(s)found
   DimvarBookmarkAsVariant
   rs.MoveFirst
   rs.Find("CustomerID='"&CustomerID&"'")
   Ifrs.EOF=TrueThen
   FindByCustomerID=False
   rs.Bookmark=varBookmark
   Else
   FindByCustomerID=True
   EndIf
  EndFunction
  
  PublicPropertyGetEOF()AsBoolean
  'Exampleofaread-onlyproperty
  NoPropertyLetshere
  EOF=rs.EOF
  EndProperty
  PublicPropertyGetBOF()AsBoolean
   'Anotherexampleofaread-onlyproperty
   BOF=rs.BOF
  EndProperty
  PrivateSubrs_MoveComplete(ByValadReasonAsADODB.EventReasonEnum,_
     ByValpErrorAsADODB.Error,adStatusAsADODB.EventStatusEnum,_
     ByValpRecordsetAsADODB.Recordset)
  
   'ReactstotherecordsetMoveComplete
   'method-raiseseventwitheachmove
   RaiseEventRecordsetMove
  EndSub->

  需要说明的是:迄今为止,我们仅仅是在一个类中添加代码。当然,也可以选择"Project"->"AddClass"菜单来向工程添加多个类,而且还可利用"collections"使这些类工作在一起。但是在这里,我们仍然想用一个类来处理一个数据表。
  
  将上述类的代码复制并粘贴到自己的类中,下一节将讨论该程序的编译。
  ->

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14621-1.html
网站维护教程更新时间:2012-04-03 01:10:55  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer