在VBA类对象中是否应该有重复的值? [英] Should I have to have duplicate values in VBA class objects?

查看:84
本文介绍了在VBA类对象中是否应该有重复的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在excel中使用VBA类模块已有一段时间了,但是我不确定是否正确创建了它们。我通常为类创建模块级变量,然后为属性let和get函数创建。例如:

I've been using VBA class modules in excel for a while now, but I'm not sure that I am creating them correctly. I normally create module level variables for the class and then property let and get functions. For example:

Private msRegion As String

Property Get Region() As String
    Region = msRegion 'Return the Region
End Property

Property Let Region(ByVal sRegionName As String)
    msRegion = sRegionName 'Set the Region
End Property

当我查看本地窗口中的对象时,我注意到对于每个属性,我最终得到模块作用域变量以及Let函数所需的变量。对我来说,这似乎是一个变量的重复。我担心如果我实例化了一个大型的对象集合,每个对象具有多个属性,那么在资源方面可能会很昂贵。

When I look at the objects in the local window I notice that for each property I end up with the module scoped variable and the variable required by the Let function. This seems like duplication of a variable to me. I'm concerned that if I instatiated a large collection of objects with several properties in each it could be costly in terms of resources.

我试图修改代码因此对象类中只有一个变量,但是到目前为止,我只为自己的烦恼而收到错误消息。

I've tried to modify the code so that only one variable is in the object class, but so far I've only got error messages for my pains.

有人知道在Windows中创建属性的方法吗?不会导致重复变量的类模块?

Does anyone know of a way to create properties in class modules that does not lead to duplicate variables?

编辑:

我刚刚又看了一下我的当地人窗口,并意识到属性let的参数不存在。这两个表达式似乎是私有存储的变量,并且属性获得了值,在这种情况下为msRegion和Region。符合Cor_Blimey的答案。

I have just had another look at my locals window and realised that argument to the property let isn't there. The 2 expressions appear to be the privately stored variable and the proprty get value, in this case msRegion and Region. That fits in with Cor_Blimey's answer.

推荐答案

您是否尝试在调用该类的代码中获取或设置msRegion属性? ?如果这样做,您将注意到它不可调用。

Have you tried to get or set the msRegion property in your code that is calling the class? If you do you will notice that it is not callable.

您正在正确构造该类。

You are structuring the class correctly.

本地窗口显示所有已声明的变量,包括一个类中的私有变量。

The locals window shows all declared variables, including private varibale within a class.

这篇关于在VBA类对象中是否应该有重复的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆