Link to home
Start Free TrialLog in
Avatar of Jayrw
Jayrw

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jayrw
Jayrw

ASKER

Easy enough - thanks.