类定义如何在 VBA 中工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21503604/
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-12 01:41:31  来源:igfitidea点击:

How do Class definitions work in VBA

vbaclass

提问by ExoticBirdsMerchant

As i am trying to learn VBA, Classes are blasting me some heavy duty pain!!!

当我正在尝试学习 VBA 时,课程让我感到非常痛苦!!!

Here is something from one of my VBA books (VBA and Macros Microsoft Excel 2010 - Bill Jelen at page 497) which is absolutely bizzare and peculiar:

这是我的一本 VBA 书籍(VBA 和宏 Microsoft Excel 2010 - Bill Jelen,第 497 页)中的一些内容,这绝对是古怪而奇特的:

  1. We open a class module in VBE

  2. And we are writing the 4 properties and 1 method of the custom object in the class module clsEmployee:

    'Properites   
    Public EmpName As String             
    Public EmpID As String   
    Public EmpRate As Double         
    Public EmpWeeklyHrs As Double
    
    'Methods   
    Public Function EmpWeeklyPay() As Double  
        EmpWeeklyPay = EmpRate * EmpWeeklyHrs  
    End Function
    
  3. The book writes that the custom object is now complete

  4. Now the book goes to open a simple module (not a class module) to reference the custom object from another module, so it goes:

    Dim Employee as clsEmployee
    
    Set Employee = New clsEmployee
    
  5. Not it presents all the code block. Really awkward thing here is that the Dim Employee as clsEmployeeis outside of the sub procudure! Why?(So this could be seen as the minor question)

    Option Explicit
    
    Dim Employee as clsEmployee 'why is this outside of the code block?
    
    Sub EmpPay()
        Set Employee = New clsEmployee
    
        With Employee
            .EmpName = "Tracy Syrstad"
            .EmpID = "1651"
            .EmpRage = 25
            .EmpWeeklyHrs = 40
    
            MsgBox .EmpName & " earns $" & .EmpWeeklyPay & " per week."
        End With
    
    End Sub
    
  6. The book graciously says:

  1. 我们在 VBE 中打开一个类模块

  2. 我们正在类模块中编写自定义对象的 4 个属性和 1 个方法clsEmployee

    'Properites   
    Public EmpName As String             
    Public EmpID As String   
    Public EmpRate As Double         
    Public EmpWeeklyHrs As Double
    
    'Methods   
    Public Function EmpWeeklyPay() As Double  
        EmpWeeklyPay = EmpRate * EmpWeeklyHrs  
    End Function
    
  3. 书中写道,自定义对象现已完成

  4. 现在这本书打开一个简单的模块(不是类模块)来从另一个模块引用自定义对象,所以它是:

    Dim Employee as clsEmployee
    
    Set Employee = New clsEmployee
    
  5. 不是它展示了所有的代码块。真的在这里尴尬的事情是,在昏暗的员工为clsEmployee外面的分procudure的!为什么?(所以这可以看作是次要问题)

    Option Explicit
    
    Dim Employee as clsEmployee 'why is this outside of the code block?
    
    Sub EmpPay()
        Set Employee = New clsEmployee
    
        With Employee
            .EmpName = "Tracy Syrstad"
            .EmpID = "1651"
            .EmpRage = 25
            .EmpWeeklyHrs = 40
    
            MsgBox .EmpName & " earns $" & .EmpWeeklyPay & " per week."
        End With
    
    End Sub
    
  6. 书中亲切地说:

The procedure declares an object Employeeas a new instance of clsEmployee. It then assigns values to the four properties of the object and generates a message box displaying the employee name and weekly pay. The object's method, EmpWeeklyPay, is used to generate the displayed pay.

该过程将一个对象声明Employee为 的新实例clsEmployee。然后,它为对象的四个属性赋值并生成一个消息框,显示员工姓名和每周工资。对象的方法EmpWeeklyPay用于生成显示的薪酬。

I'm stuck here people

我被困在这里人们

As far as i know when we put (an original VBA-made and not a custom one from the class module) a property after an object for example
Application.Name
...VBA returns a String value that represents the name of the object. OK, so VBA somehow knows how to do this. But I just cannot understand howis it possible in the books paradigm to create a customproperty justby typing with nothing hard-coded

据我所知,当我们在对象之后放置(一个原始的 VBA 制作的而不是类模块中的自定义属性)一个属性时,例如
Application.Name
……VBA 返回一个表示对象名称的字符串值。好的,所以 VBA 以某种方式知道如何做到这一点。但我就是无法理解如何在书籍范式中通过键入而不进行任何硬编码来创建自定义属性

Public EmpName As String
Public EmpID As String
Public EmpRate As Double
Public EmpWeeklyHrs As Double

...just as Name property differs from Value property, what creates up there 4 different properties? I mean how are their mechanics are different? What makes property EmpNamedifferent to EmpID???For all I know they are just variables declared in the class module. How can we create custom object properties just by declaring variables in the class module??? Shouldn't we somehow input the mechanics, a demarcation, the chassis if you will, so when I use/set EmpIDit will go to look the employees ID and not his name. What is actually assuring me that when I call the EmpRateit won't go for the EmpWeeklyHrsit's the same data type and there is no other type of code in their "blueprint" the class module to stop things from getting unreliable. There's surely something hardcoded that makes Valueand Nameproperty different how can we only declare variables in the custom made object? it makes no sense to me

...正如 Name 属性与 Value 属性不同,是什么创建了 4 个不同的属性?我的意思是他们的机制有什么不同?是什么让财产EmpNameEmpID???据我所知,它们只是在类模块中声明的变量。我们如何仅通过在类模块中声明变量来创建自定义对象属性???如果你愿意的话,我们不应该以某种方式输入机械、分界、底盘,所以当我使用/设置时,EmpID它会去查看员工 ID 而不是他的名字。实际上是什么让我确信当我打电话给EmpRate它时它不会去EmpWeeklyHrs它是相同的数据类型,并且在他们的“蓝图”类模块中没有其他类型的代码来阻止事情变得不可靠。肯定有一些硬编码使ValueName属性不同,我们如何只能在自定义对象中声明变量?对我来说完全是无稽之谈

thank you for watching my long long long question

谢谢你看我长长的问题

采纳答案by NoChance

Based on my understanding of what you are asking, here is my answer:

根据我对您所问问题的理解,这是我的答案:

First question:

第一个问题:

Dim Employee as clsEmployee 'why is this outside of the code block?

Dim Employee as clsEmployee '为什么这在代码块之外?

When you do this, you can reference Employee from different parts (subs) of your code. It becomes accessible to the following code. This is optional. You could use the above line only when required by specifying in inside the sub you want. This concept is usualy discussed under the concept of variable reference scope. It is not special to class objects but any variable you use follow the same rule. Take a look at: Scope of variables in Visual Basic for Applications.

执行此操作时,您可以从代码的不同部分(子)引用 Employee。以下代码可以访问它。这是可选的。您可以仅在需要时通过在您想要的 sub 中指定来使用上面的行。这个概念通常在变量引用范围的概念下讨论。类对象并不特殊,但您使用的任何变量都遵循相同的规则。看一看:Visual Basic for Applications 中的变量范围

Second question:

第二个问题:

What is actually assuring me that when i call the EmpRate it won't go for the EmpWeeklyHrs it's the same data type and there is no other type of code in their "blueprint" the class module to stop things from getting unreliable.

实际上是什么让我确信当我调用 EmpRate 时它不会去 EmpWeeklyHrs 它是相同的数据类型并且在他们的“蓝图”类模块中没有其他类型的代码来阻止事情变得不可靠。

Compilers and interpreters are very specific about variable names. Naming is fundametal aspect of coding ever since registers were used in Assembler. When you specify Employee.EmpRate, then data in the address assigned to Employee.EmpRate will be used, I see no possibility it could confuse Employee.EmpRate with Employee.EmpWeeklyHrs. The two variables/properties have different names and hence will have 2 distinct storage locations.

编译器和解释器对变量名非常具体。自从在汇编程序中使用寄存器以来,命名是编码的基本方面。当您指定 Employee.EmpRate 时,将使用分配给 Employee.EmpRate 的地址中的数据,我认为它不可能将 Employee.EmpRate 与 Employee.EmpWeeklyHrs 混淆。这两个变量/属性具有不同的名称,因此将具有 2 个不同的存储位置。

Edit to explain with a VERY VERY simplified illustration (this is now what happens of course, but it is meant to illustrate the idea): enter image description hereNow, when you code

编辑以非常非常简化的插图来解释(这当然是现在发生的事情,但它是为了说明这个想法): 在此处输入图片说明现在,当你编码时

Doc = Employee.EmpName 

VBA goes to the storage location of the instance called "Employee" and finds that the property called 'Name" is stored with a displacement of 8 chars from the base address of value 1000 of the object. It then goes and assigns the string "Scott" found at location 1008 to the variable Doc.

VBA 去到名为“Employee”的实例的存储位置,发现名为“Name”的属性是从对象的值 1000 的基地址偏移了 8 个字符存储的,然后去分配字符串“Scott " 在变量 Doc 的位置 1008 处找到。