老有关问题,怎么将函数作为参数进行传递
老问题,如何将函数作为参数进行传递?
如何将函数作为参数进行传递?
如下例:
Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
call Mouse_OverLease(command1,process1,process2)
End Sub
Public Sub Mouse_OverLease(obj As Object,pro1 As [用什么类型?],pro2 as [用什么类型?])
On Error GoTo Err1
If obj.CurrentX >= 0 And obj.CurrentY >= 0 And obj.CurrentX <= obj.Width And obj.CurrentY <= obj.Height Then
SetCapture obj.hwnd
pro1
Else
ReleaseCapture
pro2
End If
Exit Sub
Err1:
MsgBox "错误"
End Sub
Private Sub process1()
MsgBox "process1"
End Sub
Private Sub process2()
MsgBox "process2"
End Sub
我知道上面例用类可以实现,做成类,但我不想做成类
Mouse_OverLease(obj,pro1,pro2)这个过程中,obj是对象,可能有多个模块中使用这过程,obj都不一样,同样相对的要执行的2个过程也不尽相同,请教怎么样实现。。。
------解决方案--------------------
try:
Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call Mouse_OverLease(Command1, "process1", "process2")
End Sub
Public Sub Mouse_OverLease(obj As Object, pro1 As String, pro2 As String)
On Error GoTo Err1
If obj.CurrentX >= 0 And obj.CurrentY >= 0 And obj.CurrentX <= obj.Width And obj.CurrentY <= obj.Height Then
SetCapture obj.hWnd
CallByName Me, pro1, VbMethod
Else
ReleaseCapture
CallByName Me, pro2, VbMethod
End If
Exit Sub
Err1:
MsgBox "错误"
End Sub
Public Sub process1()
MsgBox "process1"
End Sub
Public Sub process2()
MsgBox "process2"
End Sub
------解决方案--------------------
嗯,代码重用,有脑筋。OBJ是个窗体对象。Pro1,Pro2是两个过程。一般说来函数作为参数调用,像C、C++一样是要有返回值才行的,在表达式中实现;如果函数没有返回值,就用不着考虑那么复杂,用Call来调用过程速度要快得多。如下:
Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
call Mouse_OverLease(command1)
End Sub
Public Sub Mouse_OverLease(obj As Object,a as long,b as long)
On Error GoTo Err1
If (obj.CurrentX >= 0 And obj.CurrentX <= obj.Width) And (obj.CurrentY >= 0 And obj.CurrentY <= obj.Height) Then
SetCapture obj.hwnd
Call process1
Else
ReleaseCapture
Call process2
End If
Exit Sub
Err1:
MsgBox "错误"
End Sub
Public Sub process1()
MsgBox "process1"
End Sub
Public Sub process2()
MsgBox "process2"
End Sub
有返回值的情况:
Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
call Mouse_OverLease(command1,Para1 as long,Para2 as long)
End Sub
Public Sub Mouse_OverLease(obj As Object,a as long,b as long)
dim Para1 as long,Para2 as long
On Error GoTo Err1
If (obj.CurrentX >= 0 And obj.CurrentX <= obj.Width) And (obj.CurrentY >= 0 And obj.CurrentY <= obj.Height) Then
SetCapture obj.hwnd
Para1=process1(a)
Else
ReleaseCapture
Para2=process2(b)
End If
Exit Sub
Err1:
MsgBox "错误"
End Sub
Public Function process1(a as long) as long
MsgBox "process1"
process1=a
End function
如何将函数作为参数进行传递?
如下例:
Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
call Mouse_OverLease(command1,process1,process2)
End Sub
Public Sub Mouse_OverLease(obj As Object,pro1 As [用什么类型?],pro2 as [用什么类型?])
On Error GoTo Err1
If obj.CurrentX >= 0 And obj.CurrentY >= 0 And obj.CurrentX <= obj.Width And obj.CurrentY <= obj.Height Then
SetCapture obj.hwnd
pro1
Else
ReleaseCapture
pro2
End If
Exit Sub
Err1:
MsgBox "错误"
End Sub
Private Sub process1()
MsgBox "process1"
End Sub
Private Sub process2()
MsgBox "process2"
End Sub
我知道上面例用类可以实现,做成类,但我不想做成类
Mouse_OverLease(obj,pro1,pro2)这个过程中,obj是对象,可能有多个模块中使用这过程,obj都不一样,同样相对的要执行的2个过程也不尽相同,请教怎么样实现。。。
------解决方案--------------------
try:
Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call Mouse_OverLease(Command1, "process1", "process2")
End Sub
Public Sub Mouse_OverLease(obj As Object, pro1 As String, pro2 As String)
On Error GoTo Err1
If obj.CurrentX >= 0 And obj.CurrentY >= 0 And obj.CurrentX <= obj.Width And obj.CurrentY <= obj.Height Then
SetCapture obj.hWnd
CallByName Me, pro1, VbMethod
Else
ReleaseCapture
CallByName Me, pro2, VbMethod
End If
Exit Sub
Err1:
MsgBox "错误"
End Sub
Public Sub process1()
MsgBox "process1"
End Sub
Public Sub process2()
MsgBox "process2"
End Sub
------解决方案--------------------
嗯,代码重用,有脑筋。OBJ是个窗体对象。Pro1,Pro2是两个过程。一般说来函数作为参数调用,像C、C++一样是要有返回值才行的,在表达式中实现;如果函数没有返回值,就用不着考虑那么复杂,用Call来调用过程速度要快得多。如下:
Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
call Mouse_OverLease(command1)
End Sub
Public Sub Mouse_OverLease(obj As Object,a as long,b as long)
On Error GoTo Err1
If (obj.CurrentX >= 0 And obj.CurrentX <= obj.Width) And (obj.CurrentY >= 0 And obj.CurrentY <= obj.Height) Then
SetCapture obj.hwnd
Call process1
Else
ReleaseCapture
Call process2
End If
Exit Sub
Err1:
MsgBox "错误"
End Sub
Public Sub process1()
MsgBox "process1"
End Sub
Public Sub process2()
MsgBox "process2"
End Sub
有返回值的情况:
Private Sub command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
call Mouse_OverLease(command1,Para1 as long,Para2 as long)
End Sub
Public Sub Mouse_OverLease(obj As Object,a as long,b as long)
dim Para1 as long,Para2 as long
On Error GoTo Err1
If (obj.CurrentX >= 0 And obj.CurrentX <= obj.Width) And (obj.CurrentY >= 0 And obj.CurrentY <= obj.Height) Then
SetCapture obj.hwnd
Para1=process1(a)
Else
ReleaseCapture
Para2=process2(b)
End If
Exit Sub
Err1:
MsgBox "错误"
End Sub
Public Function process1(a as long) as long
MsgBox "process1"
process1=a
End function