前幾天參加C#培訓,發現其中的FORMAT參數替換功能實在是實用,尤其是在寫SQL語句的時候,比如一個SQL 陳述式
insert into (f1,f2,f3) values (v1,v2,v3)
如果要用VB來寫,要加一串的 "" 和 & 連字號,實在是難寫又難看。基本上只要參數一多,幾乎100%要寫錯。如果用C#來寫,就是這樣
str sql="insert into (f1,f2,f3) values ('{0}','{1}','{2}')"
sql=sql.string.format(sql,v1,v2,v3)
文法寫的可能不對,才培訓幾天,還沒太熟悉C#的文法。這樣寫些來實在是方便,各個參數的位置一目瞭然。今天想了想 ,終於還是決定自己寫一個VB的函數,花了一個多小時,終於搞定,加入了各種錯誤檢查,應該是沒什麼問題了。
VB最多隻能繼行25行,所以就寫這麼多了,應該是夠用了,實在不夠用就去幾個換行加參數就是了。
- Public Function rFormat(str As String, _
- ByVal v0 As Variant, _
- Optional ByVal v1 As Variant = "{E}", _
- Optional ByVal v2 As Variant = "{E}", _
- Optional ByVal v3 As Variant = "{E}", _
- Optional ByVal v4 As Variant = "{E}", _
- Optional ByVal v5 As Variant = "{E}", _
- Optional ByVal v6 As Variant = "{E}", _
- Optional ByVal v7 As Variant = "{E}", _
- Optional ByVal v8 As Variant = "{E}", _
- Optional ByVal v9 As Variant = "{E}", _
- Optional ByVal v10 As Variant = "{E}", _
- Optional ByVal v11 As Variant = "{E}", _
- Optional ByVal v12 As Variant = "{E}", _
- Optional ByVal v13 As Variant = "{E}", _
- Optional ByVal v14 As Variant = "{E}", _
- Optional ByVal v15 As Variant = "{E}", _
- Optional ByVal v16 As Variant = "{E}", _
- Optional ByVal v17 As Variant = "{E}", _
- Optional ByVal v18 As Variant = "{E}", _
- Optional ByVal v19 As Variant = "{E}", _
- Optional ByVal v20 As Variant = "{E}", _
- Optional ByVal v21 As Variant = "{E}", _
- Optional ByVal v22 As Variant = "{E}" _
- )
- Dim Ret, i As Integer, n As Integer, ocErr As Boolean
- Dim isLast As Boolean, ci As Integer, p1, plen
- ''檢查參數
- isLast = False
- For i = 0 To 22
- p1 = InStr(str, "{" & i & "}")
- plen = Len("{" & i & "}")
- If p1 > 0 Then
- ci = Val(Mid(str, p1 + 1, plen - 2))
- If ci <> i Then
- Err.Raise 1, "rFormat", "提供的參數化字串順序不匹配!!"
- End If
- If isLast Then
- Err.Raise 2, "rFormat", "提供的參數化字串可能有缺失!!"
- End If
- n = i + 1
- Else
- isLast = True
- End If
- Next
- i = 0
- Ret = str
- ocErr = False
- If i >= n And v0 <> "{E}" Then ocErr = True
- i = i + 1
- If i >= n And v1 <> "{E}" Then ocErr = True
- i = i + 1
- If i >= n And v2 <> "{E}" Then ocErr = True
- i = i + 1
- If i >= n And v3 <> "{E}" Then ocErr = True
- i = i + 1
- If i >= n And v4 <> "{E}" Then ocErr = True
- i = i + 1
- If ocErr Then
- Err.Raise 1, "rFormat", "提供參數的數量太多!"
- End If
- i = 0
- Ret = Replace(Ret, "{" & i & "}", v0 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v1 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v2 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v3 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v4 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v5 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v6 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v7 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v8 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v9 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v10 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v11 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v12 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v13 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v14 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v15 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v16 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v17 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v18 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v19 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v20 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v21 & ""): i = i + 1
- Ret = Replace(Ret, "{" & i & "}", v22 & ""): i = i + 1
- If InStr(Ret, "{E}") > 0 Then
- Err.Raise 1, "rFormat", "提供參數的數量太少!"
- End If
- rFormat = Ret
- End Function