1

I have declared an object like this:

dim oTest
oTest = CreateObject("Scripting.Dictionary")
oTest.Add ("a", "Red")
oTest.Add ("b", "Blue")

and I want to pass this information to another page like this:

response.redirect ("nextpage.aspx?username=fred&paramstring=" & oTest)

but I'm getting this error:

Operator '&' is not defined for string "nextpage.aspx?username=fred" and type 'Dictionary'.

Can anyone help?

5
  • The reason for the error is objects oTest cannot be pass directly to String variables, VBScript / Classic ASP doesn't support serialising objects you have to do that yourself using the various functions / methods available to you in VBScript. Commented Feb 11, 2016 at 10:19
  • the error you have is strange for VBScript, and calling methods with paranthesis also. are you sure that this is an ASP / VBScript question? it seems to me a Visual Basic .NET (ASP.Net) question. Commented Feb 11, 2016 at 18:13
  • No, I'm using Classic ASP. We have .NET libraries installed but I don't know if the code makes use of them. It seems I can't use the Dictionary object for passing parameters between pages: looks like I'm stuck with concatenated strings. I was hoping to use something more sophisticated. But I appreciate your help and your feedback! Commented Feb 11, 2016 at 19:52
  • OK, here's what I can do. On the original page I assign the object to a session variable: session("testobject") = oTest and then after I get to the next page, I do this: dim oTest oTest = session("testobject") and then I can use the values like this: oTest.Item("a") This does just about everything I originally needed! Thank you all Commented Feb 11, 2016 at 21:21
  • 1
    Now I'm pretty sure that you're on VB.Net. 1- you're redirecting to a page called nextpage.aspx and aspx is an asp.net extension. 2- in VBScript your code cannot not compile due to error Microsoft VBScript compilation error: Cannot use parentheses when calling a Sub by addressing oTest.Add ( lines. 3- oTest = CreateObject(".. is not a valid object assignment in VBScript, it should be Set oTest = CreateObject("... More evidence? And yes, you can store the object in Session, it works for both ASP Classic and ASP.Net. Commented Feb 11, 2016 at 21:41

2 Answers 2

1

Functions to (de)serialize a Dictionary:

Option Explicit

Const csSD1 = "&"
Const csSD2 = "="

' make dictionary from string containing serialized dictionary (key-csSD2-value-csSD1-...)
Function s2d(s)
  Dim d : Set d = CreateObject("Scripting.Dictionary")
  Dim e, p
  For Each e In Split(s, csSD1)
      p = Split(e, csSD2)
      d(p(0)) = p(1)
  Next
  Set s2d = d
End Function

' serialize dictionary to string (key-csSD2-value-csSD1-...)
Function d2s(d)
  Dim a : a = d.Keys
  Dim i
  For i = 0 To UBound(a)
       a(i) = Join(Array(a(i), d(a(i))), csSD2)
  Next
  d2s = Join(a, csSD1)
End Function

Dim s : s = "a=red&b=blue"
Dim d : Set d = s2d(s)
If s = d2s(d) Then WScript.Echo "ok"
Sign up to request clarification or add additional context in comments.

1 Comment

special characters must be escaped, otherwise s2d may return incorrect key-value pairs.
0

The Scripting.Dictionary has two methods that might be helpful.

  • .Keys() - Returns an Array of the keys
  • .Items() - Returns anArray` of the items

Using the two Arrays you could iterate through them and build up a String. Something like this should work.

<%
Dim DirTest
Dim DirKeys, DirItems
Dim Index, NumOfKeys, Params

Set DirTest = Server.CreateObject("Scripting.Dictionary")
With DirTest
  Call .Add("a", "Red")
  Call .Add("b", "Blue")
  DirKeys = .Keys()
  DirItems = .Items()
End With
If IsArray(DirKeys) Then
  Params = ""
  NumOfKeys = UBound(DirKeys)
  For Index = 0 To NumOfKeys
    Params = Params & DirKeys(Index) & "=" & DirItems(Index) & "&"
  Next
  'Trim the last Ampersand
  Params = Left(Params, Len(Params) - 1)
  Response.Write Params
End If
%>

Output:

a=Red&b=Blue

1 Comment

Thank you both for this. It looks like the Dictionary object won't help at all, since I will have to dismantle it before moving to another page. I will have to think again! I appreciate all you help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.