vb.net handles vs addhandler
I am aware that I'm posting vb specific keywords in a c# forum, and I am confident that the experts here do not have an issue with this. I am a preferred c# developer, that is to say, I prefer c# (I wont get into my reasons, thats another topic). With that said. :thumb:
Tell me what you think about using the handles keyword at design time as opposed to wiring up the events with addhandler. :confused:
I am asking this question to c# developers, because those in the know, know why I ask c# developers. :wave:
Please do tell, I'm interested to hear your thoughts. :)
(yes I understand that the handles get converted to addhandler during compile) :D
Re: vb.net handles vs addhandler
Quote:
Originally Posted by
Traps
Tell me what you think about using the handles keyword at design time as opposed to wiring up the events with addhandler. :confused:
If Visual Designer takes care of it, I do not care.
If I need to add it, I prefer AddHandler since it makes my intention more clear (Handles is usually hidden far right). I find it also easier to work with if I need to translate code between C# and VB.NET.
Re: vb.net handles vs addhandler
'Handles' has more complex behind-the-scenes behavior.
With 'Handles' (and the corresponding object declared 'WithEvents'), you can assign to the WithEvents variable and the events wired via 'Handles' are not lost.
e.g., the following VB code
Code:
Private WithEvents x As DelegateServer
Private Sub z1(ByVal member As MemberNode) Handles x.DoubleClick
End Sub
strictly translates the following C#:
Code:
[System.Runtime.CompilerServices.AccessedThroughProperty("x")]
private DelegateServer _x;
private DelegateServer x
{
[System.Diagnostics.DebuggerNonUserCode]
get
{
return this._x;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized), System.Diagnostics.DebuggerNonUserCode]
set
{
if (this._x != null)
{
this._x.DoubleClick -= z1;
}
this._x = value;
if (this._x != null)
{
this._x.DoubleClick += z1;
}
}
}
private void z1(MemberNode member)
{
}
but of course the more usual conversion is the not-quite-equivalent:
Code:
private DelegateServer x;
//INSTANT C# TODO TASK: Insert the following converted event handler wireups at the end of the 'InitializeComponent' method for forms, 'Page_Init' for web pages, or into a constructor for other classes:
x.DoubleClick += new System.EventHandler(z1);
private void z1(MemberNode member)
{
}