Solved

Passing a StringBuilder ByVal still updates the variable outside the function.

Posted on 2009-07-13
2
392 Views
Last Modified: 2012-05-07
Fairly new to VB.NET, and having a little trouble with passing StringBuilder parameters ByVal.  In the example below, what I do with the StringBuilder inside the CaseTypeListSelectHTML function (do a Replace) actually modifies the ddHTML variable outside the function (which is what I would expect from a ByRef, not a ByVal).  So the first time through the loop, CaseType1 is selected, then the second time, both CaseType1 and CaseType2, and then the third time, CaseType1, CaseType2, and CaseType3 are all selected.  Is this expected behavior?  Do I need to manually make a copy of my StringBuilder to re-initialize it before each call?
'Calling function code - ddHTML has been initialized as a StringBuilder which contains all dropdown options with nothing selected.  Then...
 
            For Each ct In CaseTypes
                i += 1
                sbHTML.AppendLine("<tr class='item' idxitem='" & i.ToString & "'><td><select class='casetypename' name='casetypename' style='width:150px;' disabled='disabled'>" & CaseTypeListSelectHTML(ddHTML, ct.CaseTypeName) & "</select></td></tr>")
            Next
 
'Function definition
    Private Function CaseTypeListSelectHTML(ByVal sbHTML As StringBuilder, Optional ByVal selected As String = "") As String
            'Select the one selected
            If selected.Length > 0 Then
                sbHTML.Replace("<option value='" & selected & "'>", "<option value='" & selected & "' selected='selected'>")
            End If
 
            Return sbHTML.ToString
    End Function
 
'

Open in new window

0
Comment
Question by:Jayrw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24841838
A StringBuilder is an OBJECT (a "reference" type as opposed to a "value" type) and therefore is always passed "by reference".  So even though you've specified it as ByVal, changes made to it will be reflected in the original instance.

When an Object is passed as ByRef then the actual instance the variable "points" to can be replaced by another one.  That is to say that you could create a new instance and assign it to the variable passed ByRef and the original variable would now point to the new instance.

See: http://msdn.microsoft.com/en-us/library/eek064h4(VS.80).aspx
0
 

Author Closing Comment

by:Jayrw
ID: 31602908
Easy enough - thanks.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question