Solved

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

Posted on 2009-07-13
2
380 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
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
Comment Utility
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
Comment Utility
Easy enough - thanks.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now