views:

231

answers:

3

I'm trying to extend the GridView class to always show the header and footer even when the datasource is empty by using the code I found online (link). However, the code is written in C# but I use VB.

What is the VB equivalent of the following?

public event MustAddARowHandler MustAddARow;

Is there a way around VB.NET not allowing events to return a type?

Also, I cannot convert the following function because of the error below.

Code:

Protected Function OnMustAddARow(ByVal data As IEnumerable) As IEnumerable
    If MustAddARow = Nothing Then 'Error on MustAddARow'
        Throw New NullReferenceException("The datasource has no rows.  You " _
                                        & "must handle the 'MustAddARow' Event.")
    End If
    Return MustAddARow(data) 'Error on MustAddARow'
End Function

Error: Public Event MustAddARow(data As System.Collections.IEnumerable)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

+2  A: 

Like this:

Public Event MustAddRow(data As IEnumerable)
SLaks
What about "MustAddARowHandler" in the original line?
Steven
The VB.Net compiler will automatically generate the delegate type.
SLaks
SLaks is right, but there is another way: Public Event MustAddRow as MustAddRowHandler
M.A. Hanin
Adding "as MustAddRowHandler" causes the following error: Events cannot have a return type.
Steven
True, because in VB events cannot return anything. They only raise, add handlers, and remove handlers. I'm surprised that in C# they can. I'd appreciate if someone with c# knowledge would explain exactly what is going on in that code you linked.
M.A. Hanin
@M.A. Hanin, see this SO post for an answer: http://stackoverflow.com/questions/1356488/ccreating-multicast-delegate-with-boolean-return-type/1356524#1356524
stakx
+2  A: 

Note: This answer is actually wrong. My bad. The syntax shown is correct (which is why I won't delete this answer right away), but it won't compile, since events -- which are usually multi-cast delegates -- aren't allowed to return a value in VB.NET.


Event declaration:

Public Event MustAddRow As MustAddRowHandler

Delegate type declaration: (which is the prerequisite for the above)

Public Delegate Function MustAddRowHandler(ByVal data As IEnumerable) As IEnumerable
stakx
Error: Events cannot be declared with a delegate type that has a return type.
Steven
What are multicast delegates and how do they differ from unicast delegates? in VB, all delegates have an invokation list, that is - they can all invoke multiple target methods.
M.A. Hanin
@M.A. Hanin: AFAIK, delegates are multicast by default. I'm not even sure if unicast delegates can be defined at all, since the .NET classes that implement delegates are protected in special ways. I can't give a definite answer on this, but I suspect VB.NET simply will accept only `Sub` delegates, i.e. those not returning any values. -- See also my comment at the other answer.
stakx
A: 

As described in the answer of my other post (link), I compiled the C# code into a DLL which I use in my VB Code.

Steven