The second important class is for type conversion. The class name is Con_Convert. It is instantiated at the beginning of the page code. The object name is Convert, which borrows the object name of .net type conversion. This class mainly solves the problem of type conversion. Directly using the type conversion function will cause the program to report an error because the variable is empty or has an incorrect format, and this error is allowed in most cases. For example, if you want to convert a string variable into a number, if the variable is empty, you generally need Automatically returns 0.
Another important function is to encapsulate variable formatting operations, which can maintain the uniform output format of the entire website, such as time format, currency format, etc. When formatting dates and currencies, it is very easy to encounter errors due to null values. Generally, I have to write a logic to pre-judge the null value and then format the variable. After using this class to be responsible for type conversion and formatted output, I don’t have to worry about these trivial details, which can greatly improve my programming mood.
There are also other formatting functions that have been added, such as Convert.ToPer() is used to convert numbers into percentages, Convert.FirstUppercase() is used to capitalize the first letter... You can according to your own needs, Feel free to extend this class and don’t forget to share it with everyone.
Some basic functions can be used casually if they are written casually, but when encountering special circumstances, they need to be rewritten. For example, the Convert.ToInt() method I wrote converts variables to Integer. The most basic operation is Check whether it is empty. If not, just use Cint(). But when a variable exceeds the range, you have to determine whether it is within the range of Integer, so I wrote a private method IsOverflowInteger() to determine whether the variable value is a number within a certain range. After such processing, I believe it can basically Handled all situations.
So I think that there are still many existing methods in the Convert class that need to be improved. If you have better and more complete functions, please share them so that they can become the most standard variable processing class in ASP and no longer have to rely on them. Those limited functions in ASP.
Some of the more important methods are listed below. Please see the code for specific details.
Type judgment:
Convert.IsInteger(ByVal Value) determines whether it is an integer, only 0~9 and - signs are allowed
Convert.IsInt(ByVal Value) determines whether it is int type. It is similar to the following and no need to explain.
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) Number formatting
Other formatting:
Convert.ToPer(Byval value,Byval value0) Percentage, with %
Convert.FirstUppercase(ByVal value) Capitalize the first letter
Convert.SafeSql(ByVal value) replaces 'for' in sql
The code is as follows: (I don’t know how to insert code, and I don’t know how CSDN operates. When you click to insert code, it is a <textarea>, not a code-folding style. Please ask friends who know it for advice.)
Copy the code code 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 For
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 for
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 For
end if
case Asc(.)
if intDecimalCount<2 then
intDecimalCount=intDecimalCount + 1
else
IsDecimal = false
Exit For
end if
case else
IsDecimal = false
Exit For
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 IsSng(ByVal Value)
IsSng=IsDecimal(value)
' -340282300000000000000000000000000000000 ~ -0.0000000000000000000000000000000000000000000001401298
' 0.00000000000000000000000000000000000000000001401298 ~ 3402823000000000000000000000000000000
' -3.402823 E38 ~ -1.401298 E-45
' 1.401298 E-45 ~ 3.402823 E38
End Function
Public Function IsDbl(ByVal Value)
IsDbl=IsDecimal(value)
' -1.79769313486232 E308 ~ -4.94065645841247 E-324
' 4.94065645841247 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 IsSng(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 0:2008-1-30 1:2008/1/30 2:1/30/2008 3:30/1/2008 4:30-JAN-2008
' 10:2008-1 11:2008/1 12:1/2008
'22:JAN-2008
'30:2008-1-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