Latest Event Updates

Problems in VB.net with delegates.

Posted on Updated on

I’m C# developer working in a VB.net project and as everybody said, if you know C# you won’t have any problems with VB. Well this hasn’t been 100% in my case (probably a 98%).

There are some annoyances in VS that drive me crazy like the fact that reorganises the code automatically when I press enter or when I add something in a multiple line sentence it removes all my indentation (bloody _ )

Recently I came across another issue with VB, the events.

The environment: ASP.net, AJAX… and VB, of course.

I created a user control called “RequestSearchControl” and I added an event called “RequestSelected“.


Partial Public Class RequestSearchControl

Inherits UserControl

Public Event RequestSelected(ByVal requestID As Integer)


Private Sub SelectRow(ByVal rowIndex As Integer)

RaiseEvent RequestSelected(rowIndex)

End Sub


End Class

Good, everything compiles!

Then I used my brand new user control in a Page


<
td>

<uc1:RequestSearchControl ID=”ParentRequestSearch”

runat=”server”

OnRequestSelected=”ParentRequestSearch_RequestSelected” />

</td>

and I added the handler in the code (I added the handler in the markup, in the code and in the OnLoad using AddHandler just in case…)

Partial Public Class RequestEditControl

Inherits UserControl


Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

AddHandler ParentRequestSearch.RequestSelected, AddressOf ParentRequestSearch_RequestSelected

End Sub



Protected Sub ParentRequestSearch_RequestSelected(ByVal requestID As Long) Handles ParentRequestSearch.RequestSelected

‘ Do Something


End Sub

End Class

Good again, everything compiles! Again! I’m on the good track!

But then… nothing happened!


Note:

When I say nothing I mean, NOTHING! No compile errors, no exceptions and it executes the code that should raise the event, and nothing.

After a number of nothing-happening-trials I thought, “Why don’t I add a delegate as I would have done it in c#?

I added:

Public Delegate Sub RequestSelectedDataHandler(ByVal requestID As Integer)

and I changed the event definition:

Public Event RequestSelected As RequestSelectedDataHandler

And… Voila! all the events worked.

Why didn’t the compiler or the runtime complain? I don’t know but it really didn’t make me think any better about VB...

Advertisements

My first post."The good thing about VB.NET"

Posted on

Finally, and after a long time seeing how a number of people share their thoughts in blogs, I’ve decided to do the same and write about whatever I want, isn’t that freedom?

There’s another one another reason why I have started this blog. In the project I’m now I have 1 min 30 secs at least from Compile to the login page and another 15 or 20 until the home page has been loaded. Now I can’t say anymore that c# is superior in all aspects I can finally say something good about VB.Net “VB.Net is the language that gives you time to write your blog”. Very friendly this guy.

Well this has been my first post and as it’s commonly said “Better late than never…”