Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:52:07+00:00 2026-06-14T16:52:07+00:00

Here’s a little something I discovered in vb.net which I cannot figure out, I’ve

  • 0

Here’s a little something I discovered in vb.net which I cannot figure out, I’ve just got a form with a treeview on it and then the following:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    treeTest.Nodes.Add("a")
    treeTest.Nodes(0).Test()
End Sub

Test is an extension method:

Imports System.Runtime.CompilerServices
Public Module ExtModule
    <Extension()>
    Public Sub Test(ByRef node As TreeNode)
    End Sub
End Module

If I use ByRef then my treeview looks like:

https://i.stack.imgur.com/7zasN.png

And with ByVal I get:

https://i.stack.imgur.com/dBeVS.png

This seems totally backwards, if I’m simply sending a reference why is the node appearing twice, while if I make a copy it only appears once?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:52:09+00:00Added an answer on June 14, 2026 at 4:52 pm

    Okay, I’ve worked out some of what’s going on.

    It’s got relatively little to do with extension methods per se. It’s more about how VB handles ByRef in general, and some odd behaviour of TreeView.Nodes by the looks of it.

    In particular, you’ll get the exact same behaviour if you change this:

    treeTest.Nodes(0).Test()
    

    to:

    ExtModule.Test(treeTest.Nodes(0))
    

    … even if you remove the ExtensionAttribute.

    Here’s some C# code which demonstrates the same effect, without using ref parameters or extension methods at all:

    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);
        }
    }
    

    The important lines are these ones:

    TreeNode node = tree.Nodes[0];
    tree.Nodes[0] = node;
    

    When your empty extension method has a ByRef parameter, your code is equivalent to the above C# code – because VB fakes “real” ByRef behaviour by using a temporary variable and then assigning back to the original property.

    When your empty extension method has a ByVal parameter, your code is just equivalent to:

    TreeNode node = tree.Nodes[0];
    // Do nothing
    

    … and that doesn’t create a second node.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here is my HTML. Just a simple form: <form> Username: <input type=text id=username/> <br/>
Here is my code, which takes two version identifiers in the form 1, 5,
Here is what I am currently doing. PHP echo's out the recent post in
Here is an example: I write html code inside of textarea, then I swap
Here are the tables I have: Table A which has entries with item and
Here is my error(if you need any more info just ask)- Error SQL query:
Here's a problem I ran into recently. I have attributes strings of the form
here's a conundrum for you, Using Django 1.4, I cannot get messages set through
Here is a small demonstration of what my problem is: http://jsfiddle.net/k2Pqw/4/ After sizing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.