Delegates as parameters in VB.NET

2019-04-07 10:55发布

Backstory: I'm using log4net to handle all logging for a project I'm working on. One particular method can be called under several different circumstances -- some that warrant the log messages to be errors and others that warrant the log messages to be warnings.

So, as an example, how could I turn

Public Sub CheckDifference(ByVal A As Integer, ByVal B As Integer)
  If (B - A) > 5 Then
    log.ErrorFormat("Difference ({0}) is outside of acceptable range.", (B - A))
  End If
End Sub

Into something more along the lines of:

Public Sub CheckDifference(ByVal A As Integer, ByVal B As Integer, "Some delegate info here")
  If (B - A) > 5 Then
    **delegateinfo**.Invoke("Difference ({0}) is outside of acceptable range.", (B - A))
  End If
End Sub

So that I could call it and pass either log.ErrorFormat or log.WarnFormat as the delegate?

I'm using VB.NET with VS 2008 and .NET 3.5 SP1. Also, I'm fairly new to delegates in general, so if this question should be worded differently to remove any ambiguities, let me know.

EDIT: Also, how could I initialize the delegate to either the ErrorFormat or the WarnFormat in the class constructor? Would it be as easy as myDelegate = log.ErrorFormat? I would imagine there is more to it than that (pardon my ignorance on the subject -- delegates are really something I want to learn more about, but so far they have eluded my understanding).

4条回答
Fickle 薄情
2楼-- · 2019-04-07 11:30

Declare your delegate signature:

Public Delegate Sub Format(ByVal value As String)

Define your Test function:

Public Sub CheckDifference(ByVal A As Integer, _
                           ByVal B As Integer, _
                           ByVal format As Format)
    If (B - A) > 5 Then
        format.Invoke(String.Format( _
        "Difference ({0}) is outside of acceptable range.", (B - A)))
    End If
End Sub

Somewhere in your code call your Test function:

CheckDifference(Foo, Bar, AddressOf log.WriteWarn)

Or

CheckDifference(Foo, Bar, AddressOf log.WriteError)
查看更多
劳资没心,怎么记你
3楼-- · 2019-04-07 11:30

You can pass delegate as parameter in VB.NET (and C#). Look here for an example.

查看更多
萌系小妹纸
4楼-- · 2019-04-07 11:34
Public Delegate errorCall(ByVal error As String, Params objs As Objects())
CheckDifference(10, 0, AddressOf log.ErrorFormat)

Please forgive the formatting :P

Basically though, create the delegate that you want, with the correct signature, and pass the address of it to the method.

查看更多
神经病院院长
5楼-- · 2019-04-07 11:56

You'll first want to declare a delegate at the Class/Module level (all this code is from memory/not tested):

Private Delegate Sub LogErrorDelegate(txt as string, byval paramarray fields() as string)

Then .. you'll want to declare it as a property to your class e.g.

Private _LogError
Public Property LogError as LogErrorDelegate
  Get 
    Return _LogError
  End Get
  Set(value as LogErrorDelegate)
    _LogError = value
  End Set
End Property

The way to instantiate the delegate is:

Dim led as New LogErrorDelegate(AddressOf log.ErrorFormat)
查看更多
登录 后发表回答