c#中format函数功能之一的VB实现——参数替换rFormat()

来源:互联网 发布:返利和淘宝联盟哪个高 编辑:程序博客网 时间:2024/06/05 04:09

前几天参加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行,所以就写这么多了,应该是够用了,实在不够用就去几个换行加参数就是了。

  1. Public Function rFormat(str As String, _
  2.     ByVal v0 As Variant, _
  3.     Optional ByVal v1 As Variant = "{E}", _
  4.     Optional ByVal v2 As Variant = "{E}", _
  5.     Optional ByVal v3 As Variant = "{E}", _
  6.     Optional ByVal v4 As Variant = "{E}", _
  7.     Optional ByVal v5 As Variant = "{E}", _
  8.     Optional ByVal v6 As Variant = "{E}", _
  9.     Optional ByVal v7 As Variant = "{E}", _
  10.     Optional ByVal v8 As Variant = "{E}", _
  11.     Optional ByVal v9 As Variant = "{E}", _
  12.     Optional ByVal v10 As Variant = "{E}", _
  13.     Optional ByVal v11 As Variant = "{E}", _
  14.     Optional ByVal v12 As Variant = "{E}", _
  15.     Optional ByVal v13 As Variant = "{E}", _
  16.     Optional ByVal v14 As Variant = "{E}", _
  17.     Optional ByVal v15 As Variant = "{E}", _
  18.     Optional ByVal v16 As Variant = "{E}", _
  19.     Optional ByVal v17 As Variant = "{E}", _
  20.     Optional ByVal v18 As Variant = "{E}", _
  21.     Optional ByVal v19 As Variant = "{E}", _
  22.     Optional ByVal v20 As Variant = "{E}", _
  23.     Optional ByVal v21 As Variant = "{E}", _
  24.     Optional ByVal v22 As Variant = "{E}" _
  25.     )
  26. Dim Ret, i As Integer, n As Integer, ocErr As Boolean
  27. Dim isLast As Boolean, ci As Integer, p1, plen
  28. ''检查参数
  29. isLast = False
  30. For i = 0 To 22
  31.     p1 = InStr(str, "{" & i & "}")
  32.     plen = Len("{" & i & "}")
  33.     If p1 > 0 Then
  34.         ci = Val(Mid(str, p1 + 1, plen - 2))
  35.         If ci <> i Then
  36.             Err.Raise 1, "rFormat""提供的参数化字符串顺序不匹配!!"
  37.         End If
  38.         If isLast Then
  39.             Err.Raise 2, "rFormat""提供的参数化字符串可能有缺失!!"
  40.         End If
  41.         n = i + 1
  42.     Else
  43.         isLast = True
  44.     End If
  45. Next
  46. i = 0
  47. Ret = str
  48. ocErr = False
  49. If i >= n And v0 <> "{E}" Then ocErr = True
  50. i = i + 1
  51. If i >= n And v1 <> "{E}" Then ocErr = True
  52. i = i + 1
  53. If i >= n And v2 <> "{E}" Then ocErr = True
  54. i = i + 1
  55. If i >= n And v3 <> "{E}" Then ocErr = True
  56. i = i + 1
  57. If i >= n And v4 <> "{E}" Then ocErr = True
  58. i = i + 1
  59. If ocErr Then
  60.     Err.Raise 1, "rFormat""提供参数的数量太多!"
  61. End If
  62. i = 0
  63. Ret = Replace(Ret, "{" & i & "}", v0 & ""): i = i + 1
  64. Ret = Replace(Ret, "{" & i & "}", v1 & ""): i = i + 1
  65. Ret = Replace(Ret, "{" & i & "}", v2 & ""): i = i + 1
  66. Ret = Replace(Ret, "{" & i & "}", v3 & ""): i = i + 1
  67. Ret = Replace(Ret, "{" & i & "}", v4 & ""): i = i + 1
  68. Ret = Replace(Ret, "{" & i & "}", v5 & ""): i = i + 1
  69. Ret = Replace(Ret, "{" & i & "}", v6 & ""): i = i + 1
  70. Ret = Replace(Ret, "{" & i & "}", v7 & ""): i = i + 1
  71. Ret = Replace(Ret, "{" & i & "}", v8 & ""): i = i + 1
  72. Ret = Replace(Ret, "{" & i & "}", v9 & ""): i = i + 1
  73. Ret = Replace(Ret, "{" & i & "}", v10 & ""): i = i + 1
  74. Ret = Replace(Ret, "{" & i & "}", v11 & ""): i = i + 1
  75. Ret = Replace(Ret, "{" & i & "}", v12 & ""): i = i + 1
  76. Ret = Replace(Ret, "{" & i & "}", v13 & ""): i = i + 1
  77. Ret = Replace(Ret, "{" & i & "}", v14 & ""): i = i + 1
  78. Ret = Replace(Ret, "{" & i & "}", v15 & ""): i = i + 1
  79. Ret = Replace(Ret, "{" & i & "}", v16 & ""): i = i + 1
  80. Ret = Replace(Ret, "{" & i & "}", v17 & ""): i = i + 1
  81. Ret = Replace(Ret, "{" & i & "}", v18 & ""): i = i + 1
  82. Ret = Replace(Ret, "{" & i & "}", v19 & ""): i = i + 1
  83. Ret = Replace(Ret, "{" & i & "}", v20 & ""): i = i + 1
  84. Ret = Replace(Ret, "{" & i & "}", v21 & ""): i = i + 1
  85. Ret = Replace(Ret, "{" & i & "}", v22 & ""): i = i + 1
  86. If InStr(Ret, "{E}") > 0 Then
  87.     Err.Raise 1, "rFormat""提供参数的数量太少!"
  88. End If
  89. rFormat = Ret
  90. End Function