سؤال

أواجه مشكلة غريبة.IIF هو العبث عندما أعمل مع صفيف.يبدو أنه يتحقق عبري آخر على الرغم من أنه لم يتم تنشيطه.هنا هو بعض التعليمات البرمجية التي توضح المشكلة: giveacodicetagpre.

هل كانت مفيدة؟

المحلول

The Iif operator doesn't implement short circuiting and will evaluate both the true and false case. If you want a short-circuit version then use If.

Dim itemLimit As String = If(values.Length < 6, "200", values(5))

نصائح أخرى

Have a look at this article: http://www.fmsinc.com/free/newtips/net/nettip33.asp

From the article:

Visual Basic, VBA, and Visual Basic .NET support the IIF function as an alternative to the If...Then...Else statement. Although this may seem like a shortcut, IIF functions differently than If...Then...Else.

IIF must evaluate the entire statement when preparing the argument, which can lead to undesirable side effects.

In other words, your If...Then...Else works because the Else clause isn't being evaluated if the condition fails. The IIf, on the other hand, evaluates all the statements, causing the IndexOutOfBounds exception.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top