一个有可选参数的函数, 在该函数代码里如何识别调用者是否提供了可选参数?

realdreamer 2002-12-27 02:36:56
rt
...全文
88 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
realdreamer 2002-12-27
  • 打赏
  • 举报
回复
谢了, 楼上各位. 感谢大家积极参与:)

给分!
用户 昵称 2002-12-27
  • 打赏
  • 举报
回复
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
CloneCenter 2002-12-27
  • 打赏
  • 举报
回复
是依靠可选参数的默认值来完成的。
Function Incrment(byval AInt as integer, optional byval AInc as integer = 1) as Integer
Incrment = AInt + AInc
end function

在这里不需要判断是否提供了 Optional 参数,如果提供了就用它提供的值,如果不是就用默认值。
ivandova 2002-12-27
  • 打赏
  • 举报
回复
如果参数为对象或者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
用户 昵称 2002-12-27
  • 打赏
  • 举报
回复
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
holydiablo 2002-12-27
  • 打赏
  • 举报
回复

不过optional MaxLena as integer=1(可以加默认值),应该可以解决问题
CloneCenter 2002-12-27
  • 打赏
  • 举报
回复
是依靠可选参数的默认值来完成的。
Function Incrment(byval AInt as integer, optional byval AInc as integer = 1) as Integer
Incrment = AInt + AInc
end function

在这里不需要判断是否提供了 Optional 参数,如果提供了就用它提供的值,如果不是就用默认值。
litsnake1 2002-12-27
  • 打赏
  • 举报
回复
end sub
litsnake1 2002-12-27
  • 打赏
  • 举报
回复
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

用户 昵称 2002-12-27
  • 打赏
  • 举报
回复
up first, wait me search.
Kain 2002-12-27
  • 打赏
  • 举报
回复
private sub check(a as long,optional b as long=0)

end sub

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧