VB Implementation of the format function in C # -- replace rformat () with the parameter ()

Source: Internet
Author: User

I attended the C # training a few days ago and found that the format parameter replacement function is really practical, especially when writing SQL statements, such as an SQL statement.

Insert into (F1, F2, F3) values (V1, V2, V3)

If you want to use VB to write, it is hard to write and ugly to add a string of "" and. Basically, as long as there are more than one parameter, an error is required in almost 100%. If C # is used for writing, this is the case.

Str SQL = "insert into (F1, F2, F3) values ('{0}', '{1}', '{2 }')"

SQL = SQL. String. Format (SQL, V1, V2, V3)

The syntax may not be correct. It takes only a few days to train and you are not familiar with the C # syntax. This is easy to write, and the location of each parameter is clear at a glance. After thinking about it today, I finally decided to write a VB Function by myself. It took more than an hour to finally solve the problem. I added various error checks and it should be okay.

 

VB can only follow up to 25 rows, so it should be enough to write so much. If it is not enough, just add a few line breaks and parameters.

  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. ''Check parameters
  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", "The provided parameterized string order does not match !! "
  37. End if
  38. If islast then
  39. Err. Raise 2, "rformat", "provided parameterized strings may be missing !! "
  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", "too many parameters are provided! "
  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", "Too few parameters are provided! "
  88. End if
  89. Rformat = RET
  90. End Function
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.