尝试尝试或两次添加委托...不成功。
Public Function CallbackAddress() As Integer
'UPGRADE_WARNING: Add a delegate for AddressOf CalledBack Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="E9E157F7-EF0C-4016-87B7-7D7FBBC6EE08"'
CallbackAddress = GetAddress(AddressOf CalledBack)
End Function
Private Function GetAddress(ByVal address As Integer) As Integer
'GetAddress = address
Return address
End Function
这里是添加委托的失败(不完整或完全错误)的尝试:
Public Delegate Sub CalledBackDelegate(ByVal param As Integer)
Public Function CallbackAddress() As Integer
Dim myCalledBackDelegate As CalledBackDelegate = New CalledBackDelegate(AddressOf CalledBack)
CallbackAddress = GetAddress(myCalledBackDelegate) // << This doesn't work!
End Function
仍然以错误结束。 myCalledbackDegate无法转换为整数。我想念什么?
建议的所有三个函数都是返回整数的函数。虽然当然可以有一个函数返回一个委托,但是有一个返回fixed委托的函数将毫无意义。
委托是对函数的引用,用于在调用站点不知道将调用哪种方法的情况下调用函数。现在,这通常是通过lambda函数完成的。下面是一个完整的应用程序,显示了当今如何普遍使用委托和lambda(一种特殊类型的委托)。您的示例缺少用法,因此我们无法真正为您提供帮助。希望这有助于阐明概念。 (请使用iPad调整布局)。
imports System
Public Module Module1
Public Sub Main()
console.Write("Enter a number:")
Dim x as integer = if(integer.tryparse(console.ReadLine().trim, x), x, x)
console.Write("Enter another number:")
dim y as integer = if(integer.tryparse(console.ReadLine().trim, y), y, y)
console.Write("Enter a plus or minus sign:")
dim operation = console.ReadLine().Trim()
dim op as func(Of integer, integer, integer)
select case operation
case "+"
op = addressof add
case "-"
op = addressof minus
end Select
console.WriteLine($"Result of {x} {operation} {y} is {op(x, y)}")
dim lambda as func(of integer)
select case operation
case "+"
lambda = function() x + y
case "-"
lambda = function() x - y
end select
console.writeline($"Result of {x} {operation} {y} is {lambda()}")
End Sub
public Function Add(x as integer, y as integer) as integer
return x + y
end Function
public function Minus(x as integer, y as integer) as integer
return x - y
end function
End Module