VB语言

本类阅读TOP10

·Visual Basic 安装程序的制作!!
·VB中使用EXCEL输出
·一个简单的MP3播放器
·VB程序实现WindowsXP效果的界面!!
·VB打造超酷个性化菜单(六)
·透明位图
·平铺与拉伸MDI窗口的背景图 ~!~
·对《VB程序实现WindowsXP效果的界面》一文的补遗
·从Windows资源管理器中拖动文件
·VB打造超酷个性化菜单(一)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
全局变量的定义和使用

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

    在用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*函数。小弟经验有限,望各位大虾多多指教,同时欢迎来Email:[email protected]共同探讨。




相关文章

相关软件