全局变量的定义和使用 |
|
www.nanhushi.com 佚名 不详 |
在用VB开发软件时,经常需要在不同的窗体间共享数据,但在过多的使用全局变量时不便于软件的调试和修改。通常有两种解决方法。 第一种方法:定义全局变量,然后在各个窗体中直接使用,例如 Public strCustomerID As String Public strCustomerName As String 第二种方法:添加一个模块专门用于定义变量,然后定义一些Public过程或函数来设置和获取这些数据,例如 Dim strCustomerID As String Dim strCustomerName As String Public Sub SetCustomerID(CustomerID As String) strCustomerID = CustomerID End Sub Public Function GetCustomerID() As String GetCustomerID = strCustomerID End Function Public Sub SetCustomerName(CustomerName As String) strCustomerName = CustomerName End Sub Public Function GetCustomerName() As String GetCustomerName = strCustomerName End Function 考试大提醒:其实这两种方法都有缺点,使用第一种方法时,当过程或函数中过于频繁的使用全局变量时会给调试和修改程序带来很大的工作量,有时会产生灾难性的后果。使用第二种方法时,若共享数据很多,则需要定义很多的Set*和Get*函数。
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: VB压缩Access数据库代码 下一篇文章: visualbasic6.0遍历文件夹下所有文件代码 |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|