“if”的最佳实践Visual Basic 6.0 中超过 10 行连续限制的语句

发布于 2024-12-26 04:03:35 字数 306 浏览 1 评论 0原文

示例:

If condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or Then
    Do something
End If

假设我需要评估的条件超过这 10 个...是否有比嵌套多组 if 语句更好的方法?

Example:

If condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or Then
    Do something
End If

Say I have more than these 10 conditions I need to evaluate... Is there a better way than just nesting multiple sets of these if statements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

走野 2025-01-02 04:03:35

这里有一个选项——一次进行一个测试,用布尔值跟踪最终结果。完成后,只需测试布尔值即可。

Dim A As Long
Dim B As Long
Dim C As Long
Dim D As Long

Dim Result As Boolean

Result = True
Result = Result And (A > 10)
Result = Result And (B > 10)
Result = Result And (C > 10)
Result = Result And (D > 10)

If Result Then
    ' Do "something" here...
End If

如果 A、B、C 或 D 中的任何一个小于 10,Result 将翻转为 False 并从此保持这种状态。仅当所有测试都通过时,它才会为 True

Here's an option -- do one test at a time, tracking the final result in a boolean. When you're all done, just test the boolean.

Dim A As Long
Dim B As Long
Dim C As Long
Dim D As Long

Dim Result As Boolean

Result = True
Result = Result And (A > 10)
Result = Result And (B > 10)
Result = Result And (C > 10)
Result = Result And (D > 10)

If Result Then
    ' Do "something" here...
End If

If any of A, B, C, or D is less than 10, Result will flip to False and stay that way from then on. It will only be True if all of the tests pass.

情仇皆在手 2025-01-02 04:03:35

您可以使用 Case 语句。它比 if 更简洁:http://msdn.microsoft.com/en-us/library/cy37t14y%28v=vs.80%29.aspx

You could use Case statements. It's a little bit cleaner than ifs: http://msdn.microsoft.com/en-us/library/cy37t14y%28v=vs.80%29.aspx

浸婚纱 2025-01-02 04:03:35
Dim Result as boolean
result = false
If condition1 Then result = true
ElseIf condition2 Then result = true
ElseIf condition3 Then result = true
ElseIf condition4 Then result = true

If result Then debug.print "Success"

如果您想在条件不相同的情况下使用 select 语句,请使用:

Select Case True
    Case A=5,b=10,c="my answer",d=11
      ....
End Select
Dim Result as boolean
result = false
If condition1 Then result = true
ElseIf condition2 Then result = true
ElseIf condition3 Then result = true
ElseIf condition4 Then result = true

If result Then debug.print "Success"

If you wanted to use a select statement where conditions are not the same then use:

Select Case True
    Case A=5,b=10,c="my answer",d=11
      ....
End Select
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文