收录日期:2019/05/24 23:35:16 时间:2009-07-29 18:16:49 标签:.net,vb.net,string

I have the following condition

If InStr("a,b,c", stringA) > 0 OrElse (InStr("y,z", stringB) > 0 AndAlso value = 0) Then endif

COndition 1 is false so i check for condition 2 that is "(InStr("y,z", stringB) > 0 AndAlso value = 0) "

What puzzles me is that when stringB is nothing it still falls into the if condition and executes the code.

On first look it would seem that when stringB is nothing condition2 would fail and as a result would not fall into the if condition.

Any explanation why this would happen?

thanks

The InStr() function has an optional overload that starts with the position to start checking (the "start position".) If that is excluded (like as in your example), then it starts searching that the beginning of the string, as you would expect.

When Nothing is passed into the function for stringB, the "start position" is returned. So, one would think that "0" is returned, but in reality you are probably getting "1" back. That is because the InStr function treats the string as a 1-based array of characters instead of a 0-based array of characters. So the "start position" that is returned is 1, not 0; and as we all know 1 is > 0 :) (Source on MSDN)