views:

29

answers:

1

I am running into a bit of a problem with ByVal in VBScript. Here's a quick sample script that I wrote to illustrate the issue:

<%
Option Explicit

dim PublicDict

Set PublicDict = createobject("Scripting.Dictionary")
PublicDict.Add "MyKey", "What's up doc?"

response.write OutputStringFromDictionary( PublicDict ) & "<br />"
response.write PublicDict("MyKey")

Set PublicDict = nothing


Function OutputStringFromDictionary( ByVal DictionaryParameter )

    DictionaryParameter("MyKey") = replace(DictionaryParameter("MyKey"), "'", "''")

    OutputStringFromDictionary = DictionaryParameter("MyKey")

end Function
%>

This script outputs these lines to the browser:

What''s up doc?

What''s up doc?

I was hoping to get:

What''s up doc?

What's up doc?

How do I make it so that OutputStringFromDictionary doesn't modify the original dictionary?

+2  A: 

You'd have to clone the actual data somehow. Passing the dictionary by value prevents the dictionary itself from being modified, but you still have pointers to the same items that the dictionary references. You can't modify the dictionary, but you can modify the items it contains.

davidsidlinger
I hadn't thought about the dictionary being a collection of pointers. I'll try the clone and see if that works.
quakkels
Instead of `LocalDict = PublicDict` I had to loop through `PublicDict` and add each key/value pair to LocalDict... that did the trick.
quakkels