快速判斷 If
在 bibby 的文章中,看到一篇有關 ?? 的用法,原始網址: http://bibby.be/2009/11/blog-post.html
C# bibby 談過了,那我來談談 VB.NET 的部分。前面介紹就不重複講了,請參考 bibby 的文章。
先寫個例子如下
C#
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
02
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
03
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
04
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
05
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
06
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
07
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
08
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
09
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
10
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
VB.NET
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
02
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
03
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
04
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
05
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
06
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
07
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
08
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
09
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
10
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
二個輸出的結果都是 this is strB
VB.NET 中,算是個簡易判斷的函式, IF( TestExpression, FalsePart) (註*)
其中 TestExpression 會判斷丟進去的變數如果是 null (nothing) 就使用 falsepart 部分),
二邊其實都可以再丟一個判斷式進去,如下範例:
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
出來的結果會是 this is strC 。
舉一反三,判斷式裡可以再丟判斷式,可以創造多層,不過不建議這麼做,到時候會不好維護。
IF 在這邊有另一個用法,IF(TestExpression, Truepart, Falsepart ) (註**),判斷 TestExpression 是否成立,True 就執行 Truepart ,反之就用 Falsepart。
![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
判斷 strB 長度是否大於 15 ,是的話印出 strB ,反之則印出 strC。這出來的結果會是 this is C
善用這個,可以讓程式看起來較為簡潔、乾淨,不過可別使用太多層,這樣反而會造成反效果,
變得很難閱讀。
註* 我不知道該叫他甚麼,說是運算子,也不太像,反而比較像函式的運用,就姑且叫他函式好了
註** VS2005 在此要改為 IIF,VS2008 用 IF or IIF 二者均可。
2009/11/10 補充
larrynung 網友推薦文章,寫的蠻詳細的,若要更深入了解,建議去看看
http://www.dotblogs.com.tw/larrynung/archive/2009/03/10/7424.aspx