VB6:如何在Visual Basic 6中向函数传递临时值 在C++中,可以将临时对象参数传递给函数: struct Foo { Foo(int arg); // ... } void PrintFoo(const Foo& f); PrintFoo(Foo(10))
我试图在Visual Basic 6中实现类似的功能:VB6:如何在Visual Basic 6中向函数传递临时值 在C++中,可以将临时对象参数传递给函数: struct Foo { Foo(int arg); // ... } void PrintFoo(const Foo& f); PrintFoo(Foo(10)),vb6,Vb6,我试图在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
'# 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
但是,调用SetScrollParams(sbHorizontal,MakeScrollParams(3,0,10))
会引发一个错误:ByRef参数类型不匹配。为什么?也许吧
公共函数将ScrollParams(位置长,最小长,最大长)作为ScrollParams可能吗
公共函数MakeScrollParams(pos As Long,min As Long,max As Long)作为ScrollParams需要对现有代码进行一些更改:
MakeScrollParams
函数的声明
它返回一个ScrollParams
类型的实例,因此您应该在声明中明确指定该实例。像这样:
Public Function MakeScrollParams(pos As Long, min As Long, max As Long) As ScrollParams
Set
关键字,以避免出现“需要对象”的编译错误。只能对对象(例如类的实例)使用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))
并这样称呼它:
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))
现在可以很好地工作。需要对现有代码进行一些更改:
MakeScrollParams
函数的声明
它返回一个ScrollParams
类型的实例,因此您应该在声明中明确指定该实例。像这样:
Public Function MakeScrollParams(pos As Long, min As Long, max As Long) As ScrollParams
Set
关键字,以避免出现“需要对象”的编译错误。只能对对象(例如类的实例)使用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))
并这样称呼它:
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))
现在可以很好地工作。公共函数MakeScrollParams(pos-As-Long,min-As-Long,max-As-Long)不应该是:公共函数MakeScrollParams(pos-As-Long,min-As-Long,max-As-Long)作为ScrollParams吗?完全正确!我忘了在函数声明后键入ScrollParams。非常感谢。公共函数MakeScrollParams(pos As Long,min As Long,max As Long)不应该是:公共函数MakeScrollParams(pos As Long,min As Long,max As Long)作为ScrollParams吗?完全正确!我忘了在函数声明后键入ScrollParams。非常感谢。