VB.NET - 有没有办法利用的代表可选参数? (或计划允许这样做?)这样做、没有办法、可选、参数

2023-09-03 20:51:16 作者:别拿牙签当大炮

这是我的理解,这是不可能在一个委托在VB附带VS2008的版本的可选参数。

It is my understanding that it is not possible to have an optional parameter in a delegate in the version of VB that ships with VS2008.

不过,我想知道是否有任何变通办法或计划将这一功能到VB.NET的未来?

However, I am wondering if there are any workarounds or plans for incorporating this feature into VB.NET in the future?

我想要做的:

Public Delegate Function Deserializer(Of T)(ByRef Buffer() As Byte, optional ByRef BufferPosition As Integer = 0) As T 

'Implementation of a func that matches the delegate'
Class A
  Public Function Deserialize(Byref Buffer() as Byte, optional Byref BufferPosition as integer = 0)
  ....

在没有指定可选的实际代表自身内部的,它会至少是不错的能够做到这一点只在功能实现:

In the absence of specifying "optional" inside the actual delegate itself, it'd at least be nice to be able to do it in the function implementation only:

Public Delegate Function Deserializer(Of T)(ByRef Buffer() As Byte, ByRef BufferPosition As Integer) As T 

'Implementation of a func that matches the delegate'
Class A
  Public Function Deserialize(Byref Buffer() as Byte, optional Byref BufferPosition as integer = 0)
  ....

目前至少该第二方式,对于委托的功能将总是有一个值映射到每个参数,尽管一些可能来自功能一侧且不主叫侧

At least this second way, the functions for the delegate will always have a value mapped to each parameter, although some may come from the function side and not the calling side.

推荐答案

而不是试图写一个委托签名处理所有我感兴趣的情况下,可能限制未来的灵活性(虽然它真的取决于案件.. ),我有时用一个动作(代表谁的签名不带任何参数),而不是写我自己的委托:

Instead of trying to write a delegate signature that handles all the cases I am interested in, and possibly limiting future flexibility (though it really depends on the case..), I sometimes use an Action (delegate who's signature takes no parameters) instead of writing my own delegate:

void SomeFunction(Action action)
{
    // ...
    action();
}

有一个在C#中的功能(我敢pretty的确定在VB中),使您可以包括值从局部范围内,而不是将它们作为参数。这就是所谓的匿名委托。一般来说CS而言,以当地的价值观和你在一起,而不是将它们作为一个参数,就是所谓的闭包的一种形式。

There is a feature in C# (and I'm pretty sure in VB) that allows you to include values from the local scope, rather than passing them as parameters. It is called an anonymous delegate. In general CS terms, taking local values with you, rather than passing them as a parameter, is one form of what is called a closure.

int someValueFromLocalScope = 5;
SomeFunction(() => DoSomethingElse(someValueFromLocalScope));

// you can also assign a closure to a local variable:

int someValueFromLocalScope = 5;
Action doSomethingElse =
    delegate()
    {
        DoSomethingElse(someValueFromLocalScope);
    }; // Or you could use () => { } syntax...
SomeFunction(doSomethingElse);

然后,SomeFunction并不需要知道什么参数适用于特定的操作。

Then, SomeFunction doesn't need to know what parameters apply to the specific action.

有关操作的详细信息:(答案是闭包,但是这可能也有帮助)

More info about Action: (the answer is about closures, but this may be helpful too)

行动仅仅是一个委托,它接受零个参数,并返回零值: http://msdn.microsoft.com/en-us/library/system .action.aspx

Action is simply a delegate that takes zero parameters, and returns zero values: http://msdn.microsoft.com/en-us/library/system.action.aspx

就像任何其他的委托,也可以使用Action作为一个成员变量。

Just like any other delegate, you can also use Action as a member variable.

有更多的pre设计的代表:

There are more pre-designed delegates:

Action<TFirstParameter>
Func<TReturnValue>
Func<TFirstParameter, TReturnValue>

等。