tags:

views:

60

answers:

2

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.

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

What I'd like to do:

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.

A: 

Why not use nullable types, aka boxing, instead of using optional parameters?

Optional parameters are generally considered bad form because they aren't available in other .NET languages.

gazarsgo
Sorry just not sure how that achieves the same behavior. When a parameter is not provided, I want it to inherit a default value. This code was ported from C++ so optional parameters are used heavily. We don't have time to write hundreds of overloads to remove them
hamlin11
bad form??? What about the number of overloaded methods that can be eliminated because of optional params instead of redundant method signatures?
Tahbaza
I don't know how to explain it more simply than this: externalize your dependencies or put an abstraction around your functionality.Optional params don't eliminate redundant method signatures, optional parameters ARE redundant.
gazarsgo
+1  A: 

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();
}

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

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)

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

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

There are more pre-designed delegates:

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

etc.

Merlyn Morgan-Graham