[英]VBA enum invalid qualifier
I'm writing some code to interface with my company's online timesheet system.我正在编写一些代码来与我公司的在线时间表系统交互。 I would like to use Enums or something similar to store values for the websites elements to make them easier to change or access.我想使用枚举或类似的东西来存储网站元素的值,使它们更容易更改或访问。 I've tried to reference the members in these enums using a qualifier but get an invalid qualifier error message.我尝试使用限定符引用这些枚举中的成员,但收到无效限定符错误消息。 I can reference the enums without a qualifier but then this removes my reasoning for using them.我可以在没有限定符的情况下引用枚举,但这消除了我使用它们的理由。 Is there any way around this?有没有办法解决这个问题?
'Add initial user
Public Enum FirstUser
User = 47
ID = 50
pass1 = 56
pass2 = 59
logBtn = 67
backbtn = 133
MenuChanged = 138
logOut = 141
End Enum
'Delete user
Public Enum delUser
User = 21
UserBtn = 43
Ok = 46
After = 58
End Enum
Sub mySub()
dim del as delUser
msgbox del.User
End Sub
When you define:当你定义:
dim del as delUser
... then del
can take one of the values of your Enum delUser
, and thus it is like an Integer
-- it does not have a structure-like data type. ...然后del
可以采用Enum delUser
的值之一,因此它就像一个Integer
——它没有类似结构的数据类型。 So this syntax -- which seems to suggest that del
has member properties -- is indeed invalid:所以这个语法——似乎表明del
具有成员属性——确实无效:
MsgBox del.User
You can do this:你可以这样做:
del = delUser.User ' You assign the value 47
MsgBox del
... which will show 47. ...这将显示 47。
Maybe you were looking for something else.也许你正在寻找别的东西。 You could for instance use Collection
:例如,您可以使用Collection
:
Dim del As New Collection
del.Add 47, "User"
del.Add 50, "ID"
MsgBox del("User") ' 47
Collections still have limitations.集合仍然有限制。 You could look into classes, which you could use to create (User) objects with all the properties you need.您可以查看类,您可以使用这些类来创建具有您需要的所有属性的(用户)对象。
Read about it with some examples here . 在这里阅读一些例子。
You need then to create a Class Module (separate from your sheet and workbook code), provide it a name (let's say "clsUser"), and provide the properties.然后您需要创建一个类模块(与您的工作表和工作簿代码分开),为其提供一个名称(比如说“clsUser”),并提供属性。 A very simple one could look like this:一个非常简单的可能如下所示:
Private sUserName As String
Property Let UserName(name As String)
sUserName = name
End Property
Property Get UserName() As String
UserName = sUserName
End Property
Then in your original module, you can write things like this:然后在你的原始模块中,你可以这样写:
Dim u As clsUser
Dim u2 As clsUser
Set u = New clsUser()
u.UserName = "Sarah"
Set u2 = New clsUser()
u2.UserName = "John"
MsgBox u.UserName & ", " & u2.UserName
Enums are quite limited, dim del as delUser
basically makes del
a vanilla Long
type variable (so no properties or methods, hence the error when you try to use a dot) with a "hint" to the compiler that its supposed to contain a value in the enum delUser
.枚举非常有限, dim del as delUser
基本上使del
成为一个普通的Long
类型变量(因此没有属性或方法,因此当您尝试使用点时会出现错误),并向编译器“提示”它应该包含一个值在枚举delUser
。 I say supposed to because there is no type checking whatsoever, the only real benefit is the clarity in the declaration and the intellisense support.我说应该是因为没有任何类型检查,唯一真正的好处是声明的清晰性和智能感知支持。
If you want the value of User
read it from the enum itself: MsgBox delUser.User
如果您希望User
的值从枚举本身读取它: MsgBox delUser.User
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.