?
Solved

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

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 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