麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > ASP > 正文

ASP 三層架構 Convert類實現代碼

2024-05-04 11:08:46
字體:
來源:轉載
供稿:網友
第二個重要的類,作用是類型轉換,類名Con_Convert.在頁面代碼的開頭實例化,對象名為Convert,借用了.net的類型轉換的對象名稱.
 
 
 
這個類主要解決在類型轉換時,如果直接使用類型轉換函數,會因為變量為空或者格式不對而導致程序報錯,而這種報錯在大多數情況下是允許的.例如要轉換一個字符串變量為數字,如果變量為空,則一般需要自動返回0. 
另外一個重要功能就是封裝變量格式化操作,可以保持整個網站的輸出格式統一,例如時間格式,貨幣格式等等. 日期和貨幣格式化的時候,極易遇到因空值報錯的情況,一般都不得不寫個預判斷空值的邏輯,再格式化變量. 使用這個類負責類型轉換和格式化輸出后,就不用操心這些瑣碎的細節了,可以讓編程的心情得到大大改善啊. 
還有些其他格式化功能,也加了進去,例如Convert.ToPer()是用來轉換數字成百分數,Convert.FirstUppercase()用來做首字母大寫...... 你可以根據自己的需要,隨時擴展這個類,不要忘記了和大家分享哦. 
有些基本的函數,如果隨便寫一寫,基本可以湊合著用,但是遇到特殊情況,就要重新改寫.比如我寫的Convert.ToInt()方法,將變量轉換為Integer. 最基本的操作,是判斷一下是否為空,不為空就直接用Cint()就可以了. 但是遇到變量超出了范圍,又得判斷是否在Integer范圍內,所以又寫了一個私有方法IsOverflowInteger(),用于判斷變量值是否為某一個范圍內的數字.經過這樣的處理,相信基本可以處理所有的情況了. 
所以我想,Convert類中的已有方法還是會有不少需要改善的,大家如果有更好更完善的函數請發上來分享,讓它形成ASP中最標準的變量處理的類,再不用依賴ASP中那些有限的功能了. 
如下列舉一些比較主要的方法,具體細節請看代碼. 
類型判斷: 
Convert.IsInteger(ByVal Value) 判斷是否整數,只允許0~9和-號 
Convert.IsInt(ByVal Value) 判斷是否int型,其下類似,不用解釋了. 
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) 
類型轉換: 
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) 
格式化: 
Convert.FormatDat(ByVal Value, ByVal vStyle) 日期格式化 
Convert.FormatCur(ByVal Value,ByVal vDecimal) 貨幣格式化 
Convert.FormatNum(ByVal Value,ByVal vDecimal) 數字格式化 
其他格式化: 
Convert.ToPer(Byval value,Byval value0) 百分數,帶% 
Convert.FirstUppercase(ByVal value) 首字母大寫 
Convert.SafeSql(ByVal value) 替換sql中的'為'' 
代碼如下: (我不會插入代碼,不知道CSDN是怎么操作的,點插入代碼就是一個<textarea>,而不是可以折疊代碼的風格,向了解的朋友請教.) 
復制代碼代碼如下:

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 ture/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.000000000000000000000000000000000000000000001401298 
' 0.000000000000000000000000000000000000000000001401298 ~ 340282300000000000000000000000000000000 
' -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 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91精品国产综合久久婷婷香 | 一区二区三区欧美精品 | 日本高清视频网站www | 日本aaaa片毛片免费观蜜桃 | 久久久成人999亚洲区美女 | 久久精国 | 欧美精品成人一区二区在线观看 | 亚洲免费在线视频 | 毛片视频播放 | 污黄视频在线播放 | 国产精品一区二区三区在线看 | 黄色毛片a级| 91一级毛片 | 国产乱淫av | 神秘电影91 | 在线免费黄色网 | 国产成人高潮免费观看精品 | 日本精品视频一区二区三区四区 | 日韩视频1| 亚洲欧美aⅴ | 免费色片| 久久精品伊人网 | 日韩电影av在线 | 一级观看免费完整版视频 | 日韩精品99久久久久久 | 色999国产 | 狠狠干91 | 亚洲天堂在线电影 | 成人在线观看免费高清 | 一区二区高清视频在线观看 | 午夜精品成人 | 国产精品成人亚洲一区二区 | 日本在线视频一区二区三区 | 国产精品久久久久久久成人午夜 | 免费黄色大片在线观看 | 狠狠干五月天 | 国产一级一国产一级毛片 | 91精品国产日韩91久久久久久360 | 国产女王女m视频vk 中文日韩 | 中文字幕在线观看精品 | 免费看欧美一级特黄a大片 久久免费视频一区二区三区 |