在C ++中,可以将临时对象参数传递给函数:
struct Foo
{
Foo(int arg);
// ...
}
void PrintFoo(const Foo& f);
PrintFoo(Foo(10))
我正在尝试在Visual Basic 6中实现类似的东西:
'# Scroll bar params
Public Type ScrollParams
sbPos As Long
sbMin As Long
sbMax As Long
End Type
Public Function MakeScrollParams(pos As Long, min As Long, max As Long)
Dim params As ScrollParams
With params
.sbPos = pos
.sbMin = min
.sbMax = max
End With
Set MakeScrollParams = params
End Function
'# Set Scroll bar parameters
Public Sub SetScrollParams(sbType As Long, sbParams As ScrollParams)
Dim hWnd As Long
' ...
End Sub
但是,Call SetScrollParams(sbHorizontal, MakeScrollParams(3, 0, 10))
会引发错误:ByRef参数类型不匹配。为什么呢?
答案 0 :(得分:3)
需要从现有代码中更改一些内容:
您需要强烈输入MakeScrollParams
函数的声明。
它返回ScrollParams
类型的实例,因此您应该在声明中明确指定。像这样:
Public Function MakeScrollParams(pos As Long, min As Long, max As Long) As ScrollParams
您需要从该函数的最后一行中删除Set
关键字,以避免出现“Object Required”编译错误。您只能将Set
与对象一起使用,例如类的实例。对于常规值类型,您可以完全省略它:
MakeScrollParams = params
所以完整的函数声明如下所示:
Public Function MakeScrollParams(pos As Long, min As Long, max As Long) As ScrollParams
Dim params As ScrollParams
With params
.sbPos = pos
.sbMin = min
.sbMax = max
End With
MakeScrollParams = params
End Function
并且这样称呼它:
Call SetScrollParams(sbHorizontal, MakeScrollParams(3, 0, 10))
现在效果很好。
答案 1 :(得分:2)
也许?
Public Function MakeScrollParams(pos As Long,min As Long,max As Long) As ScrollParams