CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2007
    Posts
    274

    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.

    Tell me what you think about using the handles keyword at design time as opposed to wiring up the events with addhandler.

    I am asking this question to c# developers, because those in the know, know why I ask c# developers.

    Please do tell, I'm interested to hear your thoughts.

    (yes I understand that the handles get converted to addhandler during compile)

  2. #2
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: vb.net handles vs addhandler

    Quote Originally Posted by Traps View Post
    Tell me what you think about using the handles keyword at design time as opposed to wiring up the events with addhandler.
    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.

  3. #3
    Join Date
    Aug 2005
    Posts
    198

    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)
    	{
    	}
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured