A97 ......不重要,只是好奇(2个如果或选择案例) [英] A97... not important, just curious (2 if's or select case)

查看:66
本文介绍了A97 ......不重要,只是好奇(2个如果或选择案例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下......

Dim A as Date

A =#7/24/2005#


我希望将A的值与其他2个值进行比较:

1)8/1/2005

2)2005年9月1日


哪个更好,为什么......

第一个:

如果A< #8/1/2005#然后

MsgBox"日期早于2005年8月1日 

退出Sub

结束如果

如果A< #9/1/2005#然后

MsgBox日期早于2005年9月1日

退出Sub

结束如果


或者第二名:

选择案例A

案例是< #8/1/2005#

MsgBox"日期早于2005年8月1日 

Case Is< #9/1/2005#

MsgBox日期早于2005年9月1日

结束选择

退出Sub

Suppose the following...
Dim A as Date
A=#7/24/2005#

I wish to compare value of A against 2 other values:
1) 8/1/2005
2) 9/1/2005

Which is better and why...
First:
If A < #8/1/2005# Then
MsgBox "Date is earlier than 8/1/2005"
Exit Sub
End If
If A < #9/1/2005# Then
MsgBox "Date is earlier than 9/1/2005"
Exit Sub
End If

Or Second:
Select Case A
Case Is < #8/1/2005#
MsgBox "Date is earlier than 8/1/2005"
Case Is < #9/1/2005#
MsgBox "Date is earlier than 9/1/2005"
End Select
Exit Sub

推荐答案

MLH写道:
假设以下......
Dim A as Date
A = #7/24/2005#

我希望将A的值与其他2个值进行比较:
1)2005年8月1日
2)9/1/2005

哪个更好,为什么......
首先:
如果A< #8/1/2005#然后
MsgBox日期早于2005年8月1日
退出Sub
结束如果
如果A< #9/1/2005#然后
MsgBox日期早于2005年9月1日
退出Sub
结束如果

或第二:
选择案例A
案例是< #8/1/2005#
MsgBox日期早于2005年8月1日
案例是< #9/1/2005#
MsgBox日期早于2005年9月1日
结束选择
退出子
Suppose the following...
Dim A as Date
A=#7/24/2005#

I wish to compare value of A against 2 other values:
1) 8/1/2005
2) 9/1/2005

Which is better and why...
First:
If A < #8/1/2005# Then
MsgBox "Date is earlier than 8/1/2005"
Exit Sub
End If
If A < #9/1/2005# Then
MsgBox "Date is earlier than 9/1/2005"
Exit Sub
End If

Or Second:
Select Case A
Case Is < #8/1/2005#
MsgBox "Date is earlier than 8/1/2005"
Case Is < #9/1/2005#
MsgBox "Date is earlier than 9/1/2005"
End Select
Exit Sub




技术上Case在这种情况下效率更高,因为它只需要评估一次A值的b
。每个If块分别评估A.仅仅使用变量就不是很大的b $ b,但请考虑以下示例。


如果(一些大的长复杂表达式)< #8/1/2005#然后

MsgBox"日期早于2005年8月1日 

退出Sub

结束如果


如果我连续有几个If-Then块,那么为什么

Case语句会更有效率变得更加明显。它只需要评估

表达式一次,然后简单地比较每个Case行中的结果。一个系列

的If-Thens将不得不一遍又一遍地评估表达。


-

我不喜欢检查此邮件附带的电子邮件帐户

。发送给... ...

在Hunter dot com的RBrandt



Technically Case is more efficient in this case because it only has to evaluate
the value of A one time. Each If block is evaluating A separately. Not a big
deal when just using a variable as you are, but consider the following example.

If (some big long complex expression) < #8/1/2005# Then
MsgBox "Date is earlier than 8/1/2005"
Exit Sub
End If

If I had several If-Then blocks in a row then it becomes more obvious why the
Case statement would be more efficient. It would only have to evaluate the
expression once and then simply compare the result in each Case line. A series
of If-Thens would have to evaluate the expression over and over.

--
I don''t check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com


抱歉,Rick。这是一个微妙的观点。注意每个

If声明中的Exit Sub。这阻止了对第一次发生的进一步处理

的真实比较。所以,如果有4次比较和

为了论证,最后一个评估为True和其他人

False,不会两者都是如果'和'序列的选择'

每个都有4个完整的比较'?


- 或 -


正如在我的例子中,BOTH比较评估为True,不是
这两种方法只需要进行1次比较吗?似乎

第一个选择停止'是真的(即使下一个

,如果要进行评估,将评估为True)。 />
Sorry, Rick. It was a subtle point. Notice the Exit Sub buried in each
If statement. That prevents further processing on the FIRST occurrence
of a True comparison. So, if there were 4 comparisons being made and
for argument''s sake, the last one evaluating to True and the others
False, wouldn''t both the chain of If''s and the sequence of Select''s
each have 4 complete comparison''s to make?

- OR -

As in my example where BOTH comparisons evaluate to True, don''t
both approaches require only 1 comparison to be made? It seems
the Select stops on the first one that''s True (even though the next
one would evaluate to True if it were to be evaluated).

技术上Case在这种情况下效率更高,因为它只需要评估A的值一次。每个If块分别评估A.当你只是使用一个变量时,并不是一个很大的交易,但请考虑以下示例。

如果(一些大的长复杂表达式)< #8/1/2005#然后
MsgBox日期早于2005年8月1日
退出Sub
结束如果

如果我有几个If - 然后连续阻塞,为什么
Case语句会更有效率变得更加明显。它只需要评估
表达式一次,然后简单地比较每个Case行中的结果。一系列的If-Thens必须反复评估表达式。

Technically Case is more efficient in this case because it only has to evaluate
the value of A one time. Each If block is evaluating A separately. Not a big
deal when just using a variable as you are, but consider the following example.

If (some big long complex expression) < #8/1/2005# Then
MsgBox "Date is earlier than 8/1/2005"
Exit Sub
End If

If I had several If-Then blocks in a row then it becomes more obvious why the
Case statement would be more efficient. It would only have to evaluate the
expression once and then simply compare the result in each Case line. A series
of If-Thens would have to evaluate the expression over and over.






MLH写道:
对不起,里克。这是一个微妙的观点。注意每个
If语句中的Exit Sub。这阻止了对真实比较的第一次发生的进一步处理。所以,如果为了论证而进行了4次比较,那么最后一次评估为True而其他人则判断为错误,并不是If'和'的链条。 Select'的序列
每个都有4个完整的比较'?
Sorry, Rick. It was a subtle point. Notice the Exit Sub buried in each
If statement. That prevents further processing on the FIRST occurrence
of a True comparison. So, if there were 4 comparisons being made and
for argument''s sake, the last one evaluating to True and the others
False, wouldn''t both the chain of If''s and the sequence of Select''s
each have 4 complete comparison''s to make?




我不是在谈论比较。如果最后一个测试是真的那么两个都是

执行相同数量的测试。我在谈论评估正在测试的价值

。如果再看一下我之前的帖子,那就是我所说的

(一些很长的复杂表达式)。如果我在一系列If-Then块中执行那个

,那么(一些大的长复数表达式)是在所有If-Then的第一行上评估的
阻止但仅在Select Case块中评估



是的,如果满足第一个测试并且它不会产生影响如果最后一次测试

满意,则
可能不会对用户产生任何差异。所有这些都是最佳实践理论。其差异是以微秒为单位测量
。虽然它可以产生差异(例如循环操作),但是仍有好处,所以

知道最好使用的程序是什么。


作为一个程序中有多个退出点的旁边被认为是糟糕的形式

,所以我不会在每个If-Then块中有一个Exit Sub,就像在你的例如。


-

我没有查看电子邮件帐户附上

到此消息。发送给...

在Hunter dot com的RBrandt



I was not talking about the comparisons. If the last test is true then both are
performing the same number of tests. I was talking about evaluating the value
that is being tested against. If you look at my previous post again this is the
(some big long complex expression) that I am talking about. If I was doing that
in a series of If-Then blocks the (some big long complex expression) is
evaluated on the first line of ALL of the If-Then blocks but is only evaluated
once in a Select Case block.

Yes, this would not make a difference if the first test is satisfied and it
likely would not make any difference perceptable to the user if the last test
were satisfied. All of this is "best practice theory" where the differences are
going to be measured in microseconds. There are curcumstances though where it
could make a difference (a looping operation for example) so it is still good to
know what the best procedures to use are.

As an aside having multiple exit points in a procedure is considered poor form
anyway so I would not have an Exit Sub in each If-Then block as in your example.

--
I don''t check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com


这篇关于A97 ......不重要,只是好奇(2个如果或选择案例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆