title: VB.Net passing Hashtable and string to recursive procedure
body: <p>Here I have a recursive procedure go. It seems that whenever the <code>Hashtable</code> is passed to the recursive procedure (<code>ByVal</code> or <code>ByRef</code>) it stays in memory even when exiting the procedure. The string stays in memory on each invocation of the procedure, but it goes away when exiting the procedure because it is passed <code>ByVal</code>. Are the two objects treated differently, or is my understanding off?</p>
  
  <p>At the end of <code>form1_load</code>, why is ht.count always equal to 4 regardless of whether it was passed byref or byval? </p>
  
  <p>I was thinking that ht.count and the length of str should always be the same since both are passed byval. Why is this not true?</p>
  
  <p>Thank you for your help and trying to understand! Let me know if I need to be more clear.</p>
  
  <pre><code>Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim ht As New Hashtable
      Dim i As Integer = 1
      Dim str As String = ""
      go(ht, str, i)
      MsgBox("strLen = " &amp; Len(str) &amp; vbCrLf &amp; "htCount = " &amp; ht.Count)
  End Sub
  Private Sub go(ByVal ht As Hashtable, ByVal str As String, ByVal i As Integer)
  
      str = str &amp; "0"
      ht.Add(i, 0)
  
      i = i + 1
      If i &lt; 5 Then
          go(ht, str, i)
      End If
  
  End Sub
  </code></pre>
  
  <p>Edit: I couldn't answer my own question because I'm new, so here is what i realized too:</p>
  
  <p>For anybody that is wondering, I realized I was trying to understand the way a mutable object is passed to a procedure byref or byval. I added ht2, and the go procedure now just assignes ht to ht2.</p>
  
  <pre><code>Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim ht As New Hashtable
      Dim ht2 As New Hashtable
      Dim i As Integer = 1
      Dim str As String = ""
      go(ht, str, i, ht2)
      MsgBox("strLen = " &amp; Len(str) &amp; vbCrLf &amp; "htCount = " &amp; ht.Count &amp; vbCrLf &amp; "ht2Count = " &amp; ht2.Count)
  End Sub
  Private Sub go(ByVal ht As Hashtable, ByVal str As String, ByVal i As Integer, ByVal ht2 As Hashtable)
  
      str = str &amp; "0"
      ht.Add(i, 0)
      ht2 = ht
  
      i = i + 1
      If i &lt; 5 Then
          go(ht, str, i, ht2)
      End If
  
  End Sub
  </code></pre>
  
  <p>Now ht2 is dependent on the way it is passed to the procedure, and ht2.count should always be the same as the length of str. Also, this helped me understand: <a href="http://en.wikipedia.org/wiki/Reference_type" rel="nofollow">http://en.wikipedia.org/wiki/Reference_type</a></p>