Q: 如何以簡潔的語法, 作多重區間值的比較?
A: 用If ...ElseIf...End If, 寫多重比較, 程式會像樓梯愈走愈往右, 很難一目瞭然, 用Select ... Case就漂亮多了。
Sub Main()
Dim IntPurchaseOrderID = 11
Dim StrPurchaseOrderNumber As String
Select Case IntPurchaseOrderID
Case Is < 10
StrPurchaseOrderNumber = "00000" & IntPurchaseOrderID
Case Is < 100
StrPurchaseOrderNumber = "0000" & IntPurchaseOrderID
Case Is < 1000
StrPurchaseOrderNumber = "000" & IntPurchaseOrderID
Case Is < 10000
StrPurchaseOrderNumber = "00" & IntPurchaseOrderID
Case Is < 100000
StrPurchaseOrderNumber = "0" & IntPurchaseOrderID
Case Else
StrPurchaseOrderNumber = CStr(IntPurchaseOrderID)
End Select
Console.WriteLine(StrPurchaseOrderNumber)
Console.ReadLine()
End Sub
2008年3月24日 星期一
使用SELECT比較區間值
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言