ASP three-tier architecture Convert implementation code

Source: Internet
Author: User

This class mainly solves the problem of type conversion. If you directly use the type conversion function, the program will report an error because the variable is null or the format is incorrect. In most cases, this error is allowed. for example, to convert a string variable to a number, if the variable is null, it generally needs to return 0 automatically.
Another important function is to encapsulate variable formatting to ensure that the output format of the entire website is consistent, such as time format and currency format. when formatting the date and currency, it is easy to encounter errors due to null values. Generally, you have to write a logic to pre-determine the null value and then format the variable. after using this class to convert the type and format the output, you don't have to worry about these trivial details, which can greatly improve the programming mood.
Some other formatting functions are also added, such as Convert. toPer () is used to Convert a number into a hundred points, Convert. firstUppercase () is used to uppercase the first letter ...... you can extend this class at any time as needed. Don't forget to share it with you.
Some basic functions can be used together if you write them at will, but in special cases, You have to rewrite them. for example, I wrote Convert. toInt () method, which converts a variable to an Integer. the most basic operation is to judge whether it is null. If it is not null, you can simply use Cint. however, when a variable exceeds the range, you have to determine whether it is within the Integer Range. Therefore, you have written a private method called IsOverflowInteger () to determine whether the variable value is a number within a certain range. after such processing, I believe that we can basically handle all the situations.
So I think there are still many improvements to the existing methods in the Convert class. If you have better and better functions, please share them with me, let it form the most standard variable processing class in ASP, no need to rely on the limited functions in ASP.
The following lists some of the main methods. For details, see the code.
Type determination:
Convert. IsInteger (ByVal Value) is used to determine whether the Value is an integer. Only 0 ~ 9 and-
Convert. IsInt (ByVal Value) determines whether the int type is used. It is similar to the following. You do not need to explain it.
Convert. IsLng (ByVal Value)
Convert. IsDecimal (ByVal Value)
Convert. IsSng (ByVal Value)
Convert. IsDbl (ByVal Value)
Convert. IsCur (ByVal Value)
Convert. IsBln (ByVal Value)
Convert. IsDat (ByVal Value)
Convert. IsArr (ByVal Value)
Type conversion:
Convert. ToStr (ByVal Value)
Convert. ToInt (ByVal Value)
Convert. ToLng (ByVal Value)
Convert. ToSng (ByVal Value)
Convert. ToDbl (ByVal Value)
Convert. ToBln (ByVal Value)
Convert. ToCur (ByVal Value)
Convert. ToDat (ByVal Value)
Format:
Convert. FormatDat (ByVal Value, ByVal vStyle) date formatting
Convert. FormatCur (ByVal Value, ByVal vDecimal) Currency formatting
Convert. FormatNum (ByVal Value, ByVal vDecimal) numeric formatting
Other formatting:
Convert. ToPer (Byval value, Byval value0) percentage, with %
Convert. FirstUppercase (ByVal value) in upper case
Replace Convert. SafeSql (ByVal value) with 'to ''in SQL''
The Code is as follows: (I won't insert code, but I don't know how CSDN operates. Clicking insert code is a <textarea>, instead of a code style that can be folded. Ask a friend who knows about it .) Copy codeThe Code is as follows: Class Con_Convert
'****** Global message
Private I, j, value0, value1, value2
Private Sub Class_Initialize
End Sub
Private Sub Class_Terminate
End Sub
'================================================ ========================================================
'Check Type, Return true/false
'================================================ ========================================================
Public Function IsStr (ByVal Value)
IsStr = true
End Function
* Check string if is Integer
Public Function IsInteger (ByVal Value)
If Trim (Value) = "" or IsNull (Value) or IsEmpty (Value) then
IsInteger = false
Else
IsInteger = True
Value0 = Trim (Value)
For I = 1 To len (value0)
If Asc (Mid (value0, I, 1)> = Asc ("0") and Asc (Mid (value0, I, 1) <= Asc ("9 ") then
Else
If Asc (Mid (value0, I, 1) = Asc ("-") and I = 1 then
Else
IsInteger = false
Exit
End if
End If
Next
End if
End Function
* Check if Value is in range of integer
'Only use in this class
'Value:
'Vbound: max
Private Function IsOverflowInteger (ByVal Value, ByVal vBound)
If IsInteger (Value) and IsInteger (vBound) then
IsOverflowInteger = false
Value0 = trim (value)
Value1 = trim (vBound)
If IsOverflowInteger = false then
'Delete 0 from left
Do while (left (value0, 1) = "0" or left (value0, 1) = "-")
Value0 = right (value0, len (value0)-1)
Loop
Do while (left (value1, 1) = "0" or left (value1, 1) = "-")
Value1 = right (value1, len (value1)-1)
Loop
If len (value0) = len (value1) then
For I = 1 to len (value0)
If Asc (mid (value0, I, 1)> Asc (mid (value1, I, 1) or Asc (mid (value0, I, 1)> asc ("9") or Asc (mid (value0, I, 1) <Asc ("0") then
IsOverflowInteger = true
Exit
End if
Next
Else
If len (value0)> len (value1) then
IsOverflowInteger = true
End if
End if
End if
Else
IsOverflowInteger = true
End if
End Function
Public Function IsInt (ByVal Value)
IsInt = true
If left (trim (value), 1) = "-" then
If IsOverflowInteger (trim (value), "-32768") then
IsInt = false
End if
Else
If IsOverflowInteger (trim (value), "32767") then
IsInt = false
End if
End if
End function
Public Function IsLng (ByVal Value)
IsLng = true
If left (trim (value), 1) = "-" then
If IsOverflowInteger (trim (value), "-2147483648") then
IsLng = false
End if
Else
If IsOverflowInteger (trim (value), "2147483647") then
IsLng = false
End if
End if
End Function
'**************************************
'Decimal'
'**************************************
'***** Check string if is Decimal
Private Function IsDecimal (ByVal Value)
Dim intDecimalCount
IntDecimalCount = 0
If Trim (Value) = "" or IsNull (Value) or IsEmpty (Value) then
IsDecimal = false
Else
IsDecimal = True
Value0 = Trim (Value)
For I = 1 To len (value0)
If Asc (Mid (value0, I, 1)> = Asc ("0") and Asc (Mid (value0, I, 1) <= Asc ("9 ") then
Else
Select case Asc (Mid (value0, I, 1 ))
Case Asc ("-")
If I = 1 then
Else
IsDecimal = false
Exit
End if
Case Asc (".")
If intDecimalCount <2 then
IntDecimalCount = intDecimalCount + 1
Else
IsDecimal = false
Exit
End if
Case else
IsDecimal = false
Exit
End select
End If
Next
End if
End Function
'***** Check if Value is in range of Decimal
'Only use in this class
'Value:
'Vbound:
Private Function IsOverflowDecimal (ByVal Value, ByVal vBound)
If Trim (Value) = "" or IsNull (Value) or IsEmpty (Value) or Trim (vBound) = "" or IsNull (vBound) or IsEmpty (vBound) then
IsOverflowDecimal = true
Else
End if
End Function
Public Function compute g (ByVal Value)
Struct G = IsDecimal (value)
'-340282300000000000000000000000000000000 ~ -0.000000000000000000000000000000000000000000001401298
'2017 ~ 340282300000000000000000000000000000000
'-3.402823 E38 ~ -1.401298 E-45
'100 E-45 ~ 3.402823 E38
End Function
Public Function IsDbl (ByVal Value)
IsDbl = IsDecimal (value)
'-1.79769313486232 E308 ~ -4.94065645841247 E-324
'100 E-324 ~ 1.7976931348623 E308
End Function
Public Function IsCur (ByVal Value)
IsCur = IsDecimal (value)
'-922337203685477.5808 ~ 922337203685477.5807
End Function
Public Function IsBln (ByVal Value)
If Value = true or Value = false or trim (Value) = "1" or trim (Value) = "0" then
IsBln = true
Else
IsBln = false
End if
End Function
Public Function IsDat (ByVal Value)
If Trim (Value) = "" or IsNull (Value) or IsEmpty (Value) then
IsDat = false
Else
IsDat = IsDate (Value)
End if
End Function
Public Function IsArr (ByVal Value)
If Trim (Value) = "" or IsNull (Value) or IsEmpty (Value) then
IsArr = false
Else
IsArr = IsArray (Value)
End if
End Function
'================================================ ========================================================
'Convert Type, Return value/initial value
'================================================ ========================================================
Public Function ToStr (ByVal Value)
ToStr = trim (Value)
End Function
Public Function ToInt (ByVal Value)
If IsInt (Value) then
ToInt = Cint (Value)
Else
ToInt = 0
End if
End Function
Public Function ToLng (ByVal Value)
If IsLng (Value) then
ToLng = clng (Value)
Else
ToLng = 0
End if
End Function
Public Function ToSng (ByVal Value)
If then G (Value) then
ToSng = cSng (Value)
Else
ToSng = 0
End if
End Function
Public Function ToDbl (ByVal Value)
If IsDbl (Value) then
ToDbl = cDbl (Value)
Else
ToDbl = 0
End if
End Function
Public Function ToBln (ByVal Value)
If IsBln (Value) then
ToBln = cbool (Value)
Else
ToBln = false
End if
End Function
* VDecimal: number of decimal places
Public Function ToCur (ByVal Value)
If IsCur (Value) then
ToCur = ccur (Value)
Else
ToCur = 0
End if
End Function
* VType: format of date
Public Function ToDat (ByVal Value)
If IsDat (Value) then
ToDat = cdate (value)
Else
ToDat = ""
End if
End Function
'================================================ ========================================================
'Format
'================================================ ========================================================
'*************************************** ****************
'Formatdat
'Vdate
'Vstyle-1-30/1/30/30/2008/1/2008/2008-JAN-
'10: 2008-1/1/2008
'22: JAN-2008
'30: 11:20:20
'40: 2008-01-09
Public Function FormatDat (ByVal Value, ByVal vStyle)
Dim dateThis, intStyle
DateThis = ToDat (Value)
IntStyle = ToInt (vStyle)
If dateThis = "" or isnull (dateThis) then
FormatDat = ""
Else
Dim arrMonthArray (12)
ArrMonthArray (1) = "JAN"
ArrMonthArray (2) = "FEB"
ArrMonthArray (3) = "MAR"
ArrMonthArray (4) = "APR"
ArrMonthArray (5) = "MAY"
ArrMonthArray (6) = "JUN"
ArrMonthArray (7) = "JUL"
ArrMonthArray (8) = "AUG"
ArrMonthArray (9) = "SEP"
ArrMonthArray (10) = "OCT"
ArrMonthArray (11) = "NOV"
ArrMonthArray (12) = "DEC"
Select case intStyle
Case 1
FormatDat = cstr (year (dateThis) & "/" & cstr (month (dateThis) & "/" & cstr (day (dateThis ))
Case 2
FormatDat = cstr (month (dateThis) & "/" & cstr (day (dateThis) & "/" & cstr (year (dateThis ))
Case 3
FormatDat = cstr (day (dateThis) & "/" & cstr (month (dateThis) & "/" & cstr (year (dateThis ))
Case 4
FormatDat = cstr (day (dateThis) & "-" & arrMonthArray (month (dateThis) & "-" & cstr (year (dateThis ))
Case 10
FormatDat = cstr (year (dateThis) & "-" & cstr (month (dateThis ))
Case 11
FormatDat = cstr (year (dateThis) & "/" & cstr (month (dateThis ))
Case 12
FormatDat = cstr (month (dateThis) & "/" & cstr (year (dateThis ))
Case 22
FormatDat = arrMonthArray (month (dateThis) & "-" & cstr (year (dateThis ))
Case 30
FormatDat = cstr (year (dateThis) & "-" & cstr (month (dateThis) & "-" & cstr (day (dateThis )) & "" & hour (dateThis) & ":" & minute (dateThis) & ":" & second (dateThis)
Case 40
FormatDat = cstr (year (dateThis) & "-" & ZeroPad (cstr (month (dateThis), 2) & "-" & ZeroPad (cstr (day (dateThis), 2)
Case else
FormatDat = cstr (year (dateThis) & "-" & cstr (month (dateThis) & "-" & cstr (day (dateThis ))
End select
End if
End Function
'**************
'Formatcur
'**************
Public Function FormatCur (ByVal Value, ByVal vDecimal)
FormatCur = Formatcurrency (ToCur (Value), ToInt (vDecimal ))
End Function
Public Function FormatNum (ByVal Value, ByVal vDecimal)
FormatNum = FormatNumber (ToDbl (Value), ToInt (vDecimal ))
End Function
'================================================ ========================================================
'Other format
'================================================ ========================================================
Public Function ToPer (Byval value, Byval value0)
If Convert. ToDbl (value0) <> 0 then
ToPer = me. FormatNum (Convert. ToDbl (value)/Convert. ToDbl (value0) * 100,2) & "%"
Else
ToPer = "0.00%"
End if
End Function
'***** Value-> Value first code change to uppercase
Public Function FirstUppercase (ByVal value)
Value0 = trim (value)
If len (value0) = 0 then
FirstUppercase = ""
Else
FirstUppercase = UCase (left (value0, 1) & right (value0, len (value0)-1)
End if
End Function
Public Function SafeSql (ByVal value)
SafeSql = replace (value ,"'","''")
End Function
End Class
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.