VBA类实例化和范围

如何解决VBA类实例化和范围

我想创建旨在用作配置管理和服务助手的类。它将一些基本的连接设置存储在XML文件中,将其读取为它自己的属性,并服务于任何其他模块,表单,对象等。 我创建了一个如下所示的类:

Option Compare Database
Option Explicit

Private mstrSqlServerName As String
'...


Public Property Get SqlServerName() As String
   SqlServerName = mstrSqlServerName
End Property
'...

Private Sub Class_Initialize()
    readConfigFile
End Sub

Private Function loadConfigFile() As MSXML2.DOMDocument60
On Error GoTo FunctionError
Dim XMLFileName As String
Dim objRoot As IXMLDOMElement
Dim strMsg As String
Dim oXMLFile As MSXML2.DOMDocument60
 
'Open the xml file
Set oXMLFile = New MSXML2.DOMDocument60
XMLFileName = (Application.CurrentProject.Path & "\config.xml")
With oXMLFile
    .validateOnParse = True
    .SetProperty "SelectionLanguage","XPath"
    .async = False
    .Load (XMLFileName)
End With
    
Set loadConfigFile = oXMLFile

End Function


Public Sub readConfigFile()

    Dim oXMLFile As MSXML2.DOMDocument60
    
    Set oXMLFile = loadConfigFile

    mstrSqlServerName = oXMLFile.selectSingleNode("//config/database/SqlServerName").Text

End Sub

我已经在中间窗口中测试了我的课程,并且一切正常。然后 我创建了隐藏的表单来实例化该类,如下所示:

'frmYouShouldNotSeeMe
Option Compare Database
Option Explicit

Public configuration As clsConfiguration

Private Sub Form_Load()

    Set configuration = New clsConfiguration
    MsgBox Prompt:=configuration.SqlServerName

End Sub

现在,来自其他模块/表单/报告等。我想使用以下命令调用配置项:

MsgBox configuration.SqlServerName

但是当我尝试编译我的项目时,它说“编译错误,变量未定义”。简而言之:我找不到从数据库的其他对象使用实例化(命名)对象属性的方法。他们只是对它的存在一无所知。在中间窗口中,一切正常,我可以实例化对象并调用GET函数。我做错了什么?可能正是OOP设计带来的,但我对此并不了解。实现这个目标的最佳方法是什么?

Calling configuration object from some other form in Access DB

解决方法

您可以在类中添加两个公共函数。

赞:

Public Function SqlServerName() As String


  SqlServerName = oXMLFile.SelectSingleNode("//config/database/SqlServerName").Text


End Function

Public Function GetConfig(strNode As String,strValue As String) As String

  Dim strXPATH  As String
  strXMPATH = "//config/" & strNode & "/" & strValue
  
  
  GetConfig = oXMLFile.SelectSingleNode(strXMPATH).Text


End Function

现在在代码中,您可以使用:

 Msgbox configuration.SqlServerName

或者要获取任何配置,可以使用带有2个参数的辅助功能

Msgbox configuration("database","SqlServerName")

因此,班级的公共成员必须由您编写。它不会通过“魔术”或突然暴露出该类的信息。您可以使用公共功能,并且在创建此类公共功能时它们将显示为智能。 (它们成为类的属性)。因此,您的语法不起作用,因为您没有公开您想要的内容的公共函数(或属性get)。您可以使用属性let / get,但公共函数也可以如上所示很好地工作。

因此,可以为要公开的每个“事物”(节点)设置一个公共函数,或者使用上面的辅助函数,然后可以将key + node值与接受2个参数的函数一起传递。上面的两个都将成为该类的一部分,在编码过程中会显示智能感-您可以按照上面的说明随意向该类中添加更多公共成员。

#Edit

示例代码显示如下:

Set configuration = New clsConfiguration
MsgBox Prompt:=configuration.SqlServerName

必须是这样:

dim configuration as New clsConfiuration
MsgBox Prompt:=configuration.SqlServerName

您可以在应用程序启动时设置一个名为configueration的GLOBAL变量,这样就不必在表单代码中声明配置变量。

因此,您需要一个全局变量-在启动任何形式之前,请在启动代码中进行设置。

因此,在标准代码模块中,您需要:

Public configuration as new clsConfiguration

但是,您的代码屏幕快照缺少configuartion变量的创建。

您提供的代码包含以下内容:

Set configuration = New clsConfiguration
MsgBox Prompt:=configuration.SqlServerName

那么您在何时何地将配置定义为全局范围变量?

您必须对表单使用LOCAL范围(实际上是您的加载事件)

dim configuration as New clsConfiuration
MsgBox Prompt:=configuration.SqlServerName

或者,如上所述,您可以在starndard代码模块(不是Forms模块,当然也不是class模块)中将“配置”声明为公共全局变量。

如果您使用“ new”关键字,则可以使用此关键字:

Public configuration as New clsConfiuration

以上内容可以放置在任何标准代码模块中-范围是全局的。

在上面,然后在表单加载事件中,它将起作用:

 MsgBox Prompt:=configuration.SqlServerName
,

configuration中声明了frmYouShouldNotSeeMe以后,只要打开表单,就可以引用该表单来访问变量。

Debug.Print Forms("frmYouShouldNotSeeMe").configuration.SqlServerName

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-