使用 select enum 变量,会导致编译器检测到错误吗? VB6
在这种情况下,会发生什么?编译器会发现错误还是不会检测到错误?或者它甚至会导致错误吗?使用这样的选择我应该期望什么行为?
enum Age
over18 = 19
under18 = 17
end enum
...
...
Dim myAge As Age
Select case myAge
case over18
...
case under18
...
End Select
感谢您的帮助
In this scenario, What would happen? Would the compiler see an error or would it go undetected? Or would it even cause an error? What should I expect the behavior to be using a select like this?
enum Age
over18 = 19
under18 = 17
end enum
...
...
Dim myAge As Age
Select case myAge
case over18
...
case under18
...
End Select
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有测试过,但我认为你的代码会很好。
变量
myAge
可以设置为over18
或under18
,然后 select 语句将根据变量的值选择适当的分支。编译器不应该关心您的枚举名称与您分配给它们的值不对应,但是您的代码可能会让将来尝试维护它的任何人感到困惑。
I haven't tested it, but I would have thought your code would be fine.
The variable
myAge
could be set to eitherover18
orunder18
and then the select statement will choose the appropriate branch based on the variable's value.The compiler shouldn't care that that your enum names do not correspond to the values you have assigned to them, your code maybe confusing for anyone who tried to maintain it in the future though.
我支持 ipr101 的答案,但请注意,VB 不会神奇地知道
under18
枚举值应该匹配小于 18 的任何值,因此您需要检查 0 到 18。这也意味着它不较长适合枚举和选择案例结构,因此普通的
If
更适合。I second ipr101's answer, but note that VB doesn't magically know that the
under18
enum value should match anything less than 18 so you'd need to check for 0 to 18.This also means that it no longer fits an enum and a select case structure so a normal
If
would better suit.