private sub (str1 as string,optional str2 as string ,optional MaxLena as integer) if str2="" then debug.print "调用者没有使用这个参数,但是并不是绝对的,也有可能调用着确实传了一个空字符串过来,这样就是没办法了的事了" end if if MaxLen=0 then debug.print "调用者没有使用这个参数,但是并不是绝对的,也有可能调用着确实传了一个0过来,这样也没办法了的事了" end if
是依靠可选参数的默认值来完成的。 Function Incrment(byval AInt as integer, optional byval AInc as integer = 1) as Integer Incrment = AInt + AInc end function在这里不需要判断是否提供了 Optional 参数,如果提供了就用它提供的值,如果不是就用默认值。
对 不过optional MaxLena as integer=1(可以加默认值),应该可以解决问题
right, agree with upstairs and see this' works like the printf-function in C. ' takes a string with format characters and an array ' to expand. ' ' the format characters are always "%x", independ of the ' type. ' ' usage example: ' dim str ' str = fmt( "hello, Mr. %x, today's date is %x.", Array("Miller",Date) ) ' response.Write str function fmt( str, args ) dim res ' the result string. res = "" dim pos ' the current position in the args array. pos = 0 dim i for i = 1 to Len(str) ' found a fmt char. if Mid(str,i,1)="%" then if i<Len(str) then ' normal percent. if Mid(str,i+1,1)="%" then res = res & "%" i = i + 1 ' expand from array. elseif Mid(str,i+1,1)="x" then res = res & CStr(args(pos)) pos = pos+1 i = i + 1 end if end if ' found a normal char. else res = res & Mid(str,i,1) end if next fmt = res end function
如果参数为对象或者Variant类型的话,用isMissing()函数,如果为integer,single等简单类型的话,好象是没法判断,因为简单数据类型没有一个“Missing”的标志位只能你自己给它设一个缺省值,而且要保证你的缺省值不会导致你的程序出错。例如:Private Sub test(Optional varA As Variant, Optional intB As Integer = 32767) If IsMissing(varA) Then '// varA hasn't been passed into this procedure End If End Sub
是依靠可选参数的默认值来完成的。 Function Incrment(byval AInt as integer, optional byval AInc as integer = 1) as Integer Incrment = AInt + AInc end function在这里不需要判断是否提供了 Optional 参数,如果提供了就用它提供的值,如果不是就用默认值。
right, agree with upstairs and see this' works like the printf-function in C. ' takes a string with format characters and an array ' to expand. ' ' the format characters are always "%x", independ of the ' type. ' ' usage example: ' dim str ' str = fmt( "hello, Mr. %x, today's date is %x.", Array("Miller",Date) ) ' response.Write str function fmt( str, args ) dim res ' the result string. res = "" dim pos ' the current position in the args array. pos = 0 dim i for i = 1 to Len(str) ' found a fmt char. if Mid(str,i,1)="%" then if i<Len(str) then ' normal percent. if Mid(str,i+1,1)="%" then res = res & "%" i = i + 1 ' expand from array. elseif Mid(str,i+1,1)="x" then res = res & CStr(args(pos)) pos = pos+1 i = i + 1 end if end if ' found a normal char. else res = res & Mid(str,i,1) end if next fmt = res end function
if str2="" then
debug.print "调用者没有使用这个参数,但是并不是绝对的,也有可能调用着确实传了一个空字符串过来,这样就是没办法了的事了"
end if
if MaxLen=0 then
debug.print "调用者没有使用这个参数,但是并不是绝对的,也有可能调用着确实传了一个0过来,这样也没办法了的事了"
end if
Function Incrment(byval AInt as integer, optional byval AInc as integer = 1) as Integer
Incrment = AInt + AInc
end function在这里不需要判断是否提供了 Optional 参数,如果提供了就用它提供的值,如果不是就用默认值。
不过optional MaxLena as integer=1(可以加默认值),应该可以解决问题
' takes a string with format characters and an array
' to expand.
'
' the format characters are always "%x", independ of the
' type.
'
' usage example:
' dim str
' str = fmt( "hello, Mr. %x, today's date is %x.", Array("Miller",Date) )
' response.Write str
function fmt( str, args )
dim res ' the result string.
res = "" dim pos ' the current position in the args array.
pos = 0 dim i
for i = 1 to Len(str)
' found a fmt char.
if Mid(str,i,1)="%" then
if i<Len(str) then
' normal percent.
if Mid(str,i+1,1)="%" then
res = res & "%"
i = i + 1 ' expand from array.
elseif Mid(str,i+1,1)="x" then
res = res & CStr(args(pos))
pos = pos+1
i = i + 1
end if
end if ' found a normal char.
else
res = res & Mid(str,i,1)
end if
next fmt = res
end function
Optional intB As Integer = 32767)
If IsMissing(varA) Then
'// varA hasn't been passed into this procedure
End If
End Sub
Function Incrment(byval AInt as integer, optional byval AInc as integer = 1) as Integer
Incrment = AInt + AInc
end function在这里不需要判断是否提供了 Optional 参数,如果提供了就用它提供的值,如果不是就用默认值。
' takes a string with format characters and an array
' to expand.
'
' the format characters are always "%x", independ of the
' type.
'
' usage example:
' dim str
' str = fmt( "hello, Mr. %x, today's date is %x.", Array("Miller",Date) )
' response.Write str
function fmt( str, args )
dim res ' the result string.
res = "" dim pos ' the current position in the args array.
pos = 0 dim i
for i = 1 to Len(str)
' found a fmt char.
if Mid(str,i,1)="%" then
if i<Len(str) then
' normal percent.
if Mid(str,i+1,1)="%" then
res = res & "%"
i = i + 1 ' expand from array.
elseif Mid(str,i+1,1)="x" then
res = res & CStr(args(pos))
pos = pos+1
i = i + 1
end if
end if ' found a normal char.
else
res = res & Mid(str,i,1)
end if
next fmt = res
end function