在 VBA 类中使用全局常量、类型和函数是一个好习惯吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9494882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:23:24  来源:igfitidea点击:

Is it a good practice to use global constants, types and functions inside VBA classes?

vba

提问by bigeyes

I have started using VBA classes and I have always tried to write my code such that each class is "independent", that is, it has everything it needs--constants, functions, etc--inside. Lately, though, this approach has resulted in code duplication since, instead of calling public functions in different modules, I copied some code from the "outside world" (in the same project) into a class just to maintain its "self-sufficiency".

我已经开始使用 VBA 类并且我一直试图编写我的代码,使得每个类都是“独立的”,也就是说,它在内部拥有它需要的一切——常量、函数等。不过,最近这种方法导致了代码重复,因为我没有在不同模块中调用公共函数,而是将一些代码从“外部世界”(在同一项目中)复制到一个类中,以保持其“自给自足” .

I am considering changing a few classes so that they will be able to access functions, constants, types, etc. from other modules just like any other module can, but something in me is telling me this might not be a good practice. Can somebody tell me that what the little voice is saying is wrong? Is there a better approach?

我正在考虑更改一些类,以便它们能够像任何其他模块一样访问其他模块中的函数、常量、类型等,但我的某些想法告诉我这可能不是一个好的做法。谁能告诉我那个小声音说的是错误的吗?有没有更好的方法?

Thanks.

谢谢。

Updates:

更新:

My apologies for not providing details earlier. Here's a sample code:

我很抱歉没有提前提供详细信息。这是一个示例代码:

'-------------------------------------
'Module 1
'-------------------------------------

Public Const INITIAL_VALUE As String = "Start"
Public Const FINAL_VALUE As String = "End"

'more constants ...

Public Type SheetLoc
   SheetName As String
   SheetRow As Long
   SheetColumn As Long
End Type

'more types ...

'-------------------------------------
'Module 2
'-------------------------------------

Public Function GetLocation(key As String) As SheetLoc

   Dim myLoc As SheetLoc

   'some codes here...
   '
   With myLoc
      .SheetName = someValue
      .SheetColumn = anotherValue
      .SheetRow = stillAnotherValue
   End With

   GetLocation = myLoc

End Function

'more module functions

'-------------------------------------
'Class Module
'-------------------------------------

'some codes...

Public Sub SaveResults()

   Dim currLoc As SheetLoc      '<==== using a type defined outside of the class

   'more declarations ....
   'some codes here ....

   If currentValue = INITIAL_VALUE Then      '<=== referring to external constant 
      currLoc = GetLocation(someKey)         '<=== calling an external function
   ElseIf currentValue = FINAL_VALUE Then    '<=== referring to an external constant 
      currLoc = GetLocation(anotherKey)
   Else
      currLoc = GetLocation(defaultKey)
   End If

   'populate data ...
   'save results ...

End Sub

Note the codes with "<====" in the comments; the class uses types, functions and constants defined outside of the class, and this is what makes me wonder if it's a good practice or if there's a better option. I guess I just don't fully get the encapsulation concept.

注意注释中带有“<====”的代码;该类使用在类外定义的类型、函数和常量,这让我想知道这是一个好的做法还是有更好的选择。我想我只是没有完全理解封装的概念。

采纳答案by Dick Kusleika

I never put public constants in a class module. All, and I mean all, of my public variables and constants are in a standard module called MGlobals. That has two benefits. First, you and I know both know where to find them - they're somewhat dangerous and need to be findable. Second, if that module ever gets more than a few lines, I know I'm being lazy and need to refactor.

我从不将公共常量放在类模块中。我的所有公共变量和常量都在一个名为 MGlobals 的标准模块中。这有两个好处。首先,你我都知道在哪里可以找到它们——它们有点危险,需要可以找到。其次,如果该模块超过几行,我知道我很懒惰并且需要重构。

It's a good practice to try to keep your class modules modular. But don't go nuts with it. Good programming will never be dropping a selection of modules into a project and having them just work. There is, and should be, integration to do on any project of significance.

尝试保持类模块模块化是一个很好的做法。但不要为此发疯。好的编程永远不会将选择的模块放入项目中并让它们正常工作。任何重要的项目都应该进行整合。

回答by klobster

This is just so wrong. Op asked my own question perfectly, and the answer is just so flat.

这简直是​​大错特错。Op完美地问了我自己的问题,答案非常平淡。

IME, the answer OP was looking for (back in 2012!), was that they should forward the needed values through the ClassName.Module(optional thing as var,). Then they can send in the specific values for the presumably repeatable variants of a request, and receive a new answer for a request that matches other requests on other machines and can be used in another project without changing the class.While it is true that integration requires change, it should never be on the class side. If you destroy the integrity of the class it becomes a module.

IME,OP 正在寻找的答案(早在 2012 年!),是他们应该通过 ClassName.Module(可选的东西作为 var,)转发所需的值。然后他们可以发送请求的可能可重复的变体的特定值,并接收与其他机器上的其他请求匹配的请求的新答案,并且可以在另一个项目中使用而无需更改类。虽然集成确实需要改变,但它永远不应该在类方面。如果你破坏了类的完整性,它就会变成一个模块。

Outside INI files on the other hand...

另一方面,在 INI 文件之外...