在返回索引项的 VBA 类中创建容器属性 (Excel VBA 2003)

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

Creating a Container Property in a VBA Class which returns Indexed Items (Excel VBA 2003)

excel-vbaexcel-2003vbaexcel

提问by user2456814

I started learning VBA for my job at the end of last summer, and I can proudly say this is the first time I haven't be able to find the answer on Google. I started teaching myself about Classes this week, and I have come across a situation where I would like to be able to identify an "indexed property" for my class.

我在去年夏天结束时开始为我的工作学习 VBA,我可以自豪地说,这是我第一次无法在 Google 上找到答案。这周我开始自学关于类,我遇到了一种情况,我希望能够为我的类确定一个“索引属性”。

Since that probably isn't the clearest explanation, here is a hypothetical example:

由于这可能不是最清楚的解释,这里是一个假设的例子:

The class which I have created for my super awesome sandwich shop (clsSASS) contains properties for Calories, Weight in Grams, Price, and Ingredients. The first three are variables with very straight forward let and get statements. I.E.:

我为超级棒的三明治店 (clsSASS) 创建的类包含卡路里、克重、价格和成分的属性。前三个是具有非常直接的 let 和 get 语句的变量。IE:

Public pCal As Integer 

Public Property Get Calories() As Integer
    Calories= pCal
End Property

Public Property Let Calories(Value As Integer)
    pCal = Value
End Property

Ingredients however is designed to contain, in order of entry, the list of ingredients. My initial instinct was to do something like this:

然而,成分旨在包含按条目顺序排列的成分列表。我最初的直觉是做这样的事情:

Public pIngd  As Collection

Public Property Get Ingredients(Value As Integer) As Collection
    Ingredients = pIngd(Value)
End Property

Public Property Set Ingredients(Object As Collection)
    Set pIngd = Object
End Property

So if Bacon were the first ingredient in the list (and let's be honest it always would be), something like clsNewSandwich.Ingredients(1) would return the string 'Bacon'.

因此,如果培根是列表中的第一个成分(老实说它总是如此),像 clsNewSandwich.Ingredients(1) 这样的东西会返回字符串“培根”。

The problem arose when I added a container property to a class, and then couldn't figure out how to identify the individual items in the container. So this may just be a simple syntax issue that has nothing to do with classes whatsoever.

当我将容器属性添加到类时,问题就出现了,然后无法弄清楚如何识别容器中的各个项目。所以这可能只是一个简单的语法问题,与类无关。

Many Thanks!

非常感谢!

*edited for clarity/continuity

*为清晰/连续性进行了编辑

回答by Tim Williams

OK - I will retract my advice about always naming let/set and Get the same, since in this caseyou cannot, since the "input" and "output" types are not the same. So, in the sample below I've named the property which just returns one ingredient as Ingredient

好的 - 我将收回我关于总是命名 let/set 和 Get 相同的建议,因为在这种情况下你不能,因为“输入”和“输出”类型不一样。因此,在下面的示例中,我将仅返回一种成分的属性命名为Ingredient

Class "clsSASS":

类“clsSASS”:

Dim pIngd As Collection

Property Set Ingredients(c As Collection)
    Set pIngd = c
End Property

Property Get Ingredient(v As Integer) As String
    Ingredient = pIngd(v)
End Property

Regular module:

常规模块:

Sub Tester()
Dim c As New Collection
Dim s As New clsSASS

    c.Add "bacon"
    c.Add "lettuce"
    c.Add "tomato"

    Set s.Ingredients = c

    Debug.Print s.Ingredient(1) 'bacon
    Debug.Print s.Ingredient(2) 'lettuce
    Debug.Print s.Ingredient(3) 'tomato

End Sub