Repost - Hidden variable nonsense in VB

  • Thread starter Thread starter TheNedMan
  • Start date Start date
T

TheNedMan

Sorry for the repost, but I want to get to the bottom of this...

From the Microsoft sample "How-To Create an Offline Application":

Public Delegate Sub CustomersRowChangeEventHandler(ByVal sender As
Object, ByVal e As CustomersRowChangeEvent)
Public Event CustomersRowChanged As CustomersRowChangeEventHandler

Protected Overrides Sub OnRowChanged(ByVal e As
DataRowChangeEventArgs)
MyBase.OnRowChanged(e)
If (Not (Me.CustomersRowChangedEvent) Is Nothing) Then
RaiseEvent CustomersRowChanged(Me, New
CustomersRowChangeEvent(CType(e.Row,CustomersRow), e.Action))
End If
End Sub

*However* CustomersRowChangedEvent is not defined anywhere - the IDE
can't navigate to its definition - when hovering over
it, the IDE just says it's:
Private Dim CustomersRowChangedEvent As
HowTo.CustomersDataSet.CustomersRowChangeEventHandler

This is driving me insane - in an answer to my previous post Herfried
K. Wagner says that a hidden delegate is created, but that is not
happening here since the CustomersRowChangeEventHandler delegate is
explicitly created. So why the need for the hidden variable
CustomersRowChangedEvent?

To clear this up, I looked up the C# version of "How-To Create an
Offline Application" - it just omitted the "Event" part and used the
actual variable, which I would have thought was the obvious way of
doing this.

Why the nonsense code in VB, when it apparently can be done without
referring to a mysterious hidden variable??
 
TheNedMan said:
Sorry for the repost, but I want to get to the bottom of this...

From the Microsoft sample "How-To Create an Offline Application":

Public Delegate Sub CustomersRowChangeEventHandler(ByVal sender As
Object, ByVal e As CustomersRowChangeEvent)
Public Event CustomersRowChanged As CustomersRowChangeEventHandler

Protected Overrides Sub OnRowChanged(ByVal e As
DataRowChangeEventArgs)
MyBase.OnRowChanged(e)
If (Not (Me.CustomersRowChangedEvent) Is Nothing) Then
RaiseEvent CustomersRowChanged(Me, New
CustomersRowChangeEvent(CType(e.Row,CustomersRow), e.Action))
End If
End Sub

*However* CustomersRowChangedEvent is not defined anywhere - the IDE
can't navigate to its definition - when hovering over
it, the IDE just says it's:
Private Dim CustomersRowChangedEvent As
HowTo.CustomersDataSet.CustomersRowChangeEventHandler

This is driving me insane - in an answer to my previous post Herfried
K. Wagner says that a hidden delegate is created, but that is not
happening here since the CustomersRowChangeEventHandler delegate is
explicitly created. So why the need for the hidden variable
CustomersRowChangedEvent?

To clear this up, I looked up the C# version of "How-To Create an
Offline Application" - it just omitted the "Event" part and used the
actual variable, which I would have thought was the obvious way of
doing this.

Why the nonsense code in VB, when it apparently can be done without
referring to a mysterious hidden variable??

In VB in the vast majority of cases you don't need to access the implicit
delegate variable that VB creates directly at all. This is because VB has
the RaiseEvent keyword. RaiseEvent performs the necessary checks to make
sure the delegate has a value prior to calling it.

The only time I've seen it necessary to directly access the variable was in
an implementation of the BackgroundWorker component (which is coming in
Whidbey) for VB2003. It needed to marshal the events to a different thread,
therefore it accessed the delegate directly to access its invocation list.

So to summarise: -

1. RaiseEvent does the right thing 99.9% of the time
2. The other 0.1% can be handled by accessing the implicit variable.

Regards,

Nick Hall
 
I'm still confused - the delegate is explicit in this case:
Public Delegate Sub CustomersRowChangeEventHandler(ByVal sender As
Object, ByVal e As CustomersRowChangeEvent)

It's this event that's hidden (only viewed when hovering over it):
Private Dim CustomersRowChangedEvent As
HowTo.CustomersDataSet.CustomersRowChangeEventHandler

Why does VB create another, hidden, event to pair with the
CustomersRowChanged event?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top