如何设置函数的默认参数为某个对象

futurepi 2010-12-10 10:12:54
1.一个函数,参数为listBox 类型,如何设置默认参数是list1?
我是这样写的
functin print ( a as integer,optional lstobj as listBox=list1)
但提示说:要求常数表达式。

2。如何判断 这个参数是否有传进来,用 ISMissing好像不可以
...全文
175 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
futurepi 2010-12-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 aisac 的回复:]

你用的对象不是常量,不支持。变通的方式就是你来判断传入的参数是否为nothing,然后再对其赋值。

VB code
if o_in is nothing then set o_in = o_exists
[/Quote]
根据传入的参数 判断是否是 nothing ,是的话就是没有传进参数 然后在对其赋值。 这是个好方法

谢谢大家
futurepi 2010-12-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 chinaboyzyq 的回复:]

引用楼主 vbbianchengde 的回复:
1.一个函数,参数为listBox 类型,如何设置默认参数是list1?
我是这样写的
functin print ( a as integer,optional lstobj as listBox=list1)
但提示说:要求常数表达式。

2。如何判断 这个参数是否有传进来,用 ISMissing好像不可以


1、不要用pr……
[/Quote]

谢谢提醒, 这里的print 我是随便取个名字
futurepi 2010-12-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jiashie 的回复:]

不可能默认参数为一个对象实例吧
都没有运行,根本不存在这个对象
[/Quote]

是这样哦
熊孩子开学喽 2010-12-11
  • 打赏
  • 举报
回复
形参申明至少得有BYVAL或BYREF吧.
futurepi 2010-12-11
  • 打赏
  • 举报
回复
typeName好像和VarType差不多
神马都能聊 2010-12-11
  • 打赏
  • 举报
回复
你用的对象不是常量,不支持。变通的方式就是你来判断传入的参数是否为nothing,然后再对其赋值。

if o_in is nothing then set o_in = o_exists
jiashie 2010-12-11
  • 打赏
  • 举报
回复
不可能默认参数为一个对象实例吧
都没有运行,根本不存在这个对象
chinaboyzyq 2010-12-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 vbbianchengde 的回复:]
1.一个函数,参数为listBox 类型,如何设置默认参数是list1?
我是这样写的
functin print ( a as integer,optional lstobj as listBox=list1)
但提示说:要求常数表达式。

2。如何判断 这个参数是否有传进来,用 ISMissing好像不可以
[/Quote]

1、不要用print做函数名,它是VB保留字。
2、ISMissing只能判断variant变量
3、可以用nothing判断:

Private Sub Command1_Click()
printXX 5
printXX 5, List2
End Sub

Function printXX(a As Integer, Optional lstobj As ListBox)
If lstobj Is Nothing Then Set lstobj = List1
Debug.Print lstobj.Name

End Function

vbman2003 2010-12-11
  • 打赏
  • 举报
回复
对象默认是nothing,一定想写的话:
optional lstobj as listbox=nothing
vbman2003 2010-12-11
  • 打赏
  • 举报
回复

function myPrint(a as integer,optional lstobj as listbox) as string
if lstobj is nothing then
myPrint="nothing"
else
'处理lstobj....
myPrint="ok"
end if
end function
futurepi 2010-12-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 link0007 的回复:]

VB code
function print ( a as integer,optional lstobj as listBox=me.list1)
[/Quote]

这样也不行, 提示说内部错误

加不加 byval byref 也是一样
嗷嗷叫的老马 2010-12-11
  • 打赏
  • 举报
回复
参数的"类型",注意是一个"类型",而不是一个"实例".

声明为Variant类型,然后使用VarType函数取得传入的参数的类型,并进入Select Case分支,以对不同类型的参数作处理.

很多内部函数支持多种类型参数输入,就和这样差不多.
LinkSe7en 2010-12-10
  • 打赏
  • 举报
回复
function print ( a as integer,optional lstobj as listBox=me.list1)   

7,763

社区成员

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

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