3

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.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
  • I would doubt this is possible, since optional parameters are compile-time syntactic sugar, whereas delegates are attached on the fly at runtime. – Nick Craver Aug 06 '10 at 00:15
  • 1
    @hamlin11: Depending on the scenario, sometimes you can used closed variables in an anonymous delegate or lambda to essentially accomplish the same thing. – Merlyn Morgan-Graham Aug 06 '10 at 00:22
  • @Merlyn: Sounds beyond me but I'll do some looking into those concepts to see if I can accomplish this with reasonable performance. Thanks – Brian Webster Aug 06 '10 at 00:33
  • @hamlin11: I mean, if you have a variable i available to you, you can pass a delegate that takes zero parameters into the function, if you use a lambda/closure that references i. It doesn't have to take it as a parameter to reference it. – Merlyn Morgan-Graham Aug 06 '10 at 01:22
  • @hamlin11: int i = 0; some_function(() => do_something_with_i(i)); – Merlyn Morgan-Graham Aug 06 '10 at 01:22
  • @hamlin11: in that example, () => do_something_with_i(i) turns into a function that takes zero parameters, and returns zero results. It gets a reference to i "by magic". – Merlyn Morgan-Graham Aug 06 '10 at 01:23
  • @Merlyn - That helps A LOT, thanks for the head start on the concept. Much appreciated – Brian Webster Aug 06 '10 at 14:03
  • @Merlyn, do you mind converting some of your comments into an answer and state whether or not it's viable in VB.NET & C# or just one or the other? That way I can clear this question. Thanks – Brian Webster Aug 06 '10 at 14:17

3 Answers3

1

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
  • 58,163
  • 16
  • 128
  • 183
0

In VB.net you can create an anonymous delegate using.

Sub(t1, t2) methodName(t2)

This allows you to optionally not pass in the first parameter.

jreed350z
  • 485
  • 1
  • 5
  • 15
-2

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
  • 104
  • 2
  • 12
  • 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 – Brian Webster Aug 06 '10 at 00:37
  • 1
    bad form??? What about the number of overloaded methods that can be eliminated because of optional params instead of redundant method signatures? – Tahbaza Aug 06 '10 at 00:48
  • 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 Aug 06 '10 at 03:19