vb6 枚举对象
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.
程序员通常不使用枚举(“枚举”的简写),但是当使用时,它们可能会非常有价值。
这些是什么? (What are they?)
枚举只是一种变量类型,例如字符串或整数,但是在这种情况下,您创建的变量包含有效元素列表。 这是一个例子:Private Enum MyEnum ‘ May also be Public
私人枚举MyEnum'也可能是公开的
FirstElement
第一元素
SecondElement
SecondElement
ThirdElement
ThirdElement
FourthElement
第四元素
‘etc.
'等等。
End Enum
结束枚举
You would place that code in the Declarations section (in other words at the top) of any type of form or module. Each value in the list is defined internally by VB as a Long Integer and by default the first element will be given a value of 0, the second a value of 1, and so on.
您可以将该代码放在任何类型的表单或模块的“声明”部分中(换句话说,在顶部)。 VB在内部将列表中的每个值定义为Long Integer,默认情况下,第一个元素的值将为0,第二个元素的值将为1,依此类推。
You might be saying to yourself that you could do that with constants like this
您可能对自己说,您可以使用这样的常量来做到这一点
Private Const FirstElement = 0
私有常量FirstElement = 0
Private Const SecondElement = 1
私有const SecondElement = 1
Private Const ThirdElement = 2
私有const ThirdElement = 2
Private Const FourthElement = 3
私有常量FourthElement = 3
and you'd be right, but there are several advantages to Enums that will become apparent as you read on. Two that I’ll mention now however are: they are faster and take up less memory than strings and Intellisense will be available when you use them as shown in this picture. If you’ve forgotten how to invoke Intellisense, you press Ctrl+Spacebar, and in this case it was done after the equal sign was entered.
没错,但是Enums有许多优点,当您继续阅读时,这些优点将变得显而易见。 但是,我现在要提到的两个是:与字符串相比,它们更快并且占用的内存更少,当您使用它们时,Intellisense将可用,如图所示。 如果您忘记了如何调用Intellisense,请按Ctrl +空格键,在这种情况下,它是在输入等号之后完成的。

You may not have realized it but you’ve probably run into Enums which are built into Visual Basic.
您可能没有意识到,但是您可能已经遇到了Visual Basic内置的Enums。

细节 (Details)
上面我提到,默认情况下,枚举中的FirstElement的值为零。 但是,您可以为元素提供任何喜欢的数值,包括负数或浮点数,在后一种情况下,VB将四舍五入到最接近的整数。 规则是,任何未明确指定值的元素都将采用前一个元素的值加一个。 所以给定这个枚举Private Enum Cars
私人枚举车
Ford = 1
福特= 1
Chevrolet
雪佛兰
Chrysler
克莱斯勒
Honda = 10
本田= 10
Subaru
斯巴鲁
Toyota
丰田汽车
Mazda = 20
马自达= 20
BMW
宝马
Mercedes
梅赛德斯
Tesla = 100
特斯拉= 100
End Enum
结束枚举
Chevrolet would be 2, Chrysler 3, Subaru 11 and I’m sure you can figure the rest out. You could also do something like
雪佛兰将是2,克莱斯勒3,斯巴鲁11,我相信您会发现其余的。 您也可以做类似的事情
Subaru = Honda + 2
斯巴鲁=本田+ 2
Which would give Subaru a value of 12. Like Consts however the values can only be changed in design mode and not by way of code.
斯巴鲁的值为12。但是,与Consts一样,这些值只能在设计模式下更改,而不能通过代码更改。
If you want to you can use names that contain spaces by placing square brackets around the names like this
如果愿意,可以通过在方括号周围放置像这样的名称来使用包含空格的名称
Private Enum Cars
私人枚举车
Ford
福特汽车
Chevrolet
雪佛兰
[Chrysler 300]
[克莱斯勒300]
[Honda Civic]
[本田思域]
Subaru
斯巴鲁
Toyota
丰田汽车
Mazda
马自达
BMW
宝马
Mercedes
梅赛德斯
Tesla
特斯拉
End Enum
结束枚举
使用中的枚举 (Enums in Use)
以下代码包含两个函数和两个Sub,它们说明了使用Enum的几种方法。Option Explicit
Private Enum Cars
Ford
Chevrolet
[Chrysler 300]
[Honda Civic]
Subaru
Toyota
Mazda
BMW
Mercedes
Tesla
End Enum
Private Function isAmerican(aCar As Integer) As Boolean
If aCar = Ford Or aCar = Chevrolet Or aCar = [Chrysler 300] Then
isAmerican = True
End If
End Function
Private Function isMadeIn(aCar As Integer) As String
Select Case aCar
Case Ford To [Chrysler 300], Tesla
isMadeIn = "America"
Case [Honda Civic] To Mazda
isMadeIn = "Japan"
Case BMW, Mercedes
isMadeIn = "Germany"
Case Else
isMadeIn = "Unknown"
End Select
End Function
Private Sub Example1()
If isAmerican(Cars.Ford) Then ' Or just Ford instead of Cars.Ford
MsgBox "Ford is an american made car"
End If
End Sub
Private Sub Example2()
MsgBox "Mazda is made in " & isMadeIn(Mazda)
End Sub
If the Enum values are sequential and you want to validate a potential use you can change the Enum to
如果枚举值是连续的,并且您想验证潜在用途,则可以将枚举更改为
Private Enum Cars
私人枚举车
[_First] = 1
[_First] = 1
Ford = 1
福特= 1
Chevrolet
雪佛兰
[Chrysler 300]
[克莱斯勒300]
[Honda Civic]
[本田思域]
Subaru
斯巴鲁
Toyota
丰田汽车
Mazda
马自达
BMW
宝马
Mercedes
梅赛德斯
Tesla
特斯拉
[_Last] = 10 ' This value should equal the value for the last element
[_Last] = 10'此值应等于最后一个元素的值
End Enum
结束枚举
and then do something like this.
然后做这样的事情。
Dim i As Long
Dim intCar As Integer
intCar = 5
For i = Cars.[_First] To Cars.[_Last]
If intCar = i Then
MsgBox "It's valid"
End If
Next
Note that the special [_First] and [_Last] values won’t be shown by Intellisense and that the square brackets are needed to make them valid names. You should also note that you can assign any numeric value to a variable of Cars type even if that value is not in the Enum declaration, so intCar = 999 is valid but it would have no related Enum element.
请注意,特殊的[_First]和[_Last]值不会被Intellisense显示,并且需要使用方括号使它们成为有效名称。 您还应注意,即使该数字不在Enum声明中,也可以将任何数字值分配给Cars类型的变量,因此intCar = 999有效,但没有相关的Enum元素。
The [_Last] value is handy if you are going to often add new elements to the Enum since all you need do is change the value of [_Last] and the validation shown above would still work.
如果您要经常向Enum中添加新元素,则[_Last]值很方便,因为您需要做的就是更改[_Last]的值,并且上面显示的验证仍然有效。
我最喜欢的枚举用法(不适用于VBA) (My Favorite Use of an Enum (Does not apply to VBA))
One of the hallmarks of a well-written program is self-documentation or readability. Lets take an example of some code that in my opinion is not well written. Assume that the program has a control array called txtName (thank goodness it's not called Text43) and it consists of three members. In that program the author writes this code.
编写良好的程序的特点之一是自说明性或可读性。 让我们以一些我认为编写
Dim strName As String
If txtName(1).Text = "" Then
strName = txtName(2).Text & ", " & txtName(0).Text
Else
strName = txtName(2).Text & ", " & txtName(0).Text & " " & txtName(1).Text
End If
MsgBox strName
A stranger to the program or even the author after a few months away from the program might have to take a minute or two to figure out what's going on there. How much easier and nicer it would be if the author had an Enum and code like this
对该程序的陌生人甚至是离开该程序几个月后的作者,可能不得不花一两分钟来弄清楚那里发生了什么。 如果作者拥有一个Enum和这样的代码,那将变得容易又好得多
Private Enum BuyerName
bnFirstName
bnMiddleInitial
bnLastName
End Enum
Dim strName As String
If txtName(bnMiddleInitial).Text = "" Then
strName = txtName(bnLastName).Text & ", " & txtName(bnFirstName).Text
Else
strName = txtName(bnLastName).Text & ", " & txtName(bnFirstName).Text & " " & txtName(bnMiddleInitial).Text
End If
MsgBox strName
I use Enums all the time and I think after you use one you'll find that you do too. Your future self and/or the people who maintain your code will appreciate that.
我一直都在使用Enums,我认为在您使用Enums之后,您也会发现自己也这样做。 您将来的自我和/或维护您代码的人员将对此表示赞赏。
If you find that this article has been helpful, please click the “thumb’s up” button below. Doing so lets me know what is valuable for EE members and provides direction for future articles. It also provides me with positive feedback in the form of a few points. Thanks!
如果您发现本文对您有所帮助,请单击下面的“竖起大拇指”按钮。 这样做可以让我知道对EE成员有价值的内容,并为以后的文章提供指导。 它还以几点的形式为我提供了积极的反馈。 谢谢!
翻译自: https://www.experts-exchange.com/articles/9669/Using-Enums-in-VB6-and-VBA.html
vb6 枚举对象