2012-01-11 33 views
1

ex。if语句的最佳做法是超出vb6中的10行延续限制

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语句只是嵌套多套的这些更好的办法?

+0

你也可以把多个条件上线,但可能伤害可读性。 – 2012-01-11 21:06:56

回答

3

这里有一个选项 - 一次做一个测试,跟踪最终结果为布尔值。当你全部完成时,只需测试布尔值。

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

+0

优雅,我喜欢那样。谢谢! – 2012-01-11 21:15:35

2

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

+0

嗯,这将需要把“做某事”功能之间的每一个。或更新每个标志。看起来好像比长期嵌套的ifs更多的代码。 – 2012-01-11 21:13:49

+0

@TonyRaymond这取决于条件是什么。如果您正在检查表达式是否与一组值相匹配,则“Select Case”是完美的。例如,要检查变量'a'是否具有一组值中的一个,请执行以下操作:'选择案例a:案例1,2,3,4,5,6,7,8,9,10:做一件可以漂亮的东西' – MarkJ 2012-01-12 11:56:55

1
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