Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, I've worked out <em>some</em> of what's going on.</p> <p>It's got relatively little to do with extension methods per se. It's more about how VB handles <code>ByRef</code> in general, and some odd behaviour of <code>TreeView.Nodes</code> by the looks of it.</p> <p>In particular, you'll get the <em>exact</em> same behaviour if you change this:</p> <pre><code>treeTest.Nodes(0).Test() </code></pre> <p>to:</p> <pre><code>ExtModule.Test(treeTest.Nodes(0)) </code></pre> <p>... even if you remove the <code>ExtensionAttribute</code>.</p> <p>Here's some C# code which demonstrates the same effect, without using <code>ref</code> parameters or extension methods at all:</p> <pre><code>using System.Drawing; using System.Windows.Forms; class Test { static void Main() { TreeView tree = new TreeView { Nodes = { "a" } }; Form form = new Form { Controls = { tree } }; form.Load += delegate { TreeNode node = tree.Nodes[0]; tree.Nodes[0] = node; }; Application.Run(form); } } </code></pre> <p>The important lines are these ones:</p> <pre><code>TreeNode node = tree.Nodes[0]; tree.Nodes[0] = node; </code></pre> <p>When your empty extension method has a <code>ByRef</code> parameter, your code is equivalent to the above C# code - because VB fakes "real" <code>ByRef</code> behaviour by using a temporary variable and then assigning back to the original property.</p> <p>When your empty extension method has a <code>ByVal</code> parameter, your code is just equivalent to:</p> <pre><code>TreeNode node = tree.Nodes[0]; // Do nothing </code></pre> <p>... and that <em>doesn't</em> create a second node.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload