代表作为VB.NET中的参数 [英] Delegates as parameters in VB.NET

查看:265
本文介绍了代表作为VB.NET中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Backstory:我正在使用 log4net 处理项目的所有日志记录, m工作。一种特殊的方法可以在几种不同的情况下被调用 - 一些保证日志消息是错误的,另一些则是保证日志消息是警告。



一个例子,我如何转到

  Public Sub CheckDifference(ByVal A As Integer,ByVal B As Integer)
如果(B-A)> 5然后
log.ErrorFormat(Difference({0})不在可接受的范围内,(B - A))
End If
End Sub

更多的是:

  Public Sub CheckDifference(ByVal A As Integer,ByVal B As Integer,Some delegate info here)
If(B - A)> 5然后
** delegateinfo **。Invoke(Difference({0})不在可接受的范围内,(B - A))
End If
End Sub

所以我可以调用它并传递log.ErrorFormat或log.WarnFormat作为委托?



我在VS 2008和.NET 3.5 SP1中使用VB.NET。另外,对于一般的代表来说,我是相当新的,所以如果这个问题应该用不同的措辞来消除任何歧义,让我知道。



编辑:另外,怎么可能我将代理初始化为类构造函数中的ErrorFormat或WarnFormat?它会像 myDelegate = log.ErrorFormat 一样简单吗?我会想象,有更多的事情(原谅我对这个问题的无知 - 代表们真的是我想要更多地了解的东西,但到目前为止,他们已经忽略了我的理解)。

解决方案

声明您的委托签名:

 公共委托子格式(ByVal值As String)

定义您的测试功能:



公共子检查差异(ByVal A As Integer,_
ByVal B As Integer,_
ByVal格式为格式)
如果(B - A )> 5然后
format.Invoke(String.Format(_
Difference({0})在可接受的范围之外,(B - A)))
如果
End Sub

您的代码中的某个地方调用您的测试功能:

  CheckDifference(Foo,Bar,AddressOf log.WriteWarn)

  CheckDifference(Foo,Bar,AddressOf log.WriteError)


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).

解决方案

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)

这篇关于代表作为VB.NET中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆