[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2009-07-13
2
Medium Priority
?
404 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 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