使用 select enum 变量,会导致编译器检测到错误吗? VB6

发布于 2024-12-05 16:18:19 字数 259 浏览 1 评论 0原文

在这种情况下,会发生什么?编译器会发现错误还是不会检测到错误?或者它甚至会导致错误吗?使用这样的选择我应该期望什么行为?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹短裙飘 2024-12-12 16:18:19

我还没有测试过,但我认为你的代码会很好。

变量 myAge 可以设置为 over18under18,然后 select 语句将根据变量的值选择适当的分支。

编译器不应该关心您的枚举名称与您分配给它们的值不对应,但是您的代码可能会让将来尝试维护它的任何人感到困惑。

I haven't tested it, but I would have thought your code would be fine.

The variable myAge could be set to either over18 or under18 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.

他不在意 2024-12-12 16:18:19

我支持 ipr101 的答案,但请注意,VB 不会神奇地知道 under18 枚举值应该匹配小于 18 的任何值,因此您需要检查 0 到 18。

Select case myAge   
case over18 to 999
...   
case 0 to under18  
... 
End Select 

这也意味着它不较长适合枚举和选择案例结构,因此普通的 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.

Select case myAge   
case over18 to 999
...   
case 0 to under18  
... 
End Select 

This also means that it no longer fits an enum and a select case structure so a normal If would better suit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文