Link to home
Start Free TrialLog in
Avatar of jrram
jrramFlag for United States of America

asked on

Can't Return Dictionary Object from Function

Is it not possible to return a dictionary object from a function?  Here's my code:

<%

      Function addToCart (virtualCart, itemsList, quantitiesList)
      
            Dim itemDetails(2)
      
            For i = 0 to UBound(itemsList)
            
                  'itemDetails(0) 'Item ID
                  'itemDetails(1) 'Quantity
                  'itemDetails(2) 'Unit Price
                  'itemDetails(3) 'Item Name
                  'itemDetails(4) 'Total
                  
                  Response.Write itemsList(i)
                  
                  itemDetails(0) = itemsList(i)
                  itemDetails(1) = quantitiesList(i)
                  
                  
                   If virtualCart.Exists(itemsList(i)) Then
                   
                         currItemDetails = virtualCart.Item(itemsList(i))
                         currItemsDetails(1) = currItemsDetails(1) + itemDetails(1)
                         virtualCart.Item(itemsList(i)) = currItemDetails
                   
                  Else
                        virtualCart.Add itemsList(i), itemDetails
                  End If            
            
            Next
            
            addToCart = virtualCart '<-- Here is the problem line

      End Function


'itemsToOrderList = Split(Trim(Request.Form("idList")), ",")
'qtyToOrderList = Split(Trim(Request.Form("qtyList")), ",")

itemsToOrderList = Array(12, 177, 89)
qtyToOrderList = Array(1, 250, 75)

If Session("globalCart") = "" Then

      Response.Write "Cart is currently empty..."
      
      Set myCart = Server.CreateObject("Scripting.Dictionary")
      
Else

      Response.Write "Cart currently has items..."
      
      myCart = Session("globalCart")

End If

myCart = addToCart (myCart, itemsToOrderList, qtyToOrderList)

Session("globalCart") = myCart

%>

The error I am getting is:
Error Type:
Microsoft VBScript runtime (0x800A01C2)
Wrong number of arguments or invalid property assignment
/comDevelop Files/portalControl/addToCart2.asp, line 33

line 33 is: addToCart = virtualCart
ASKER CERTIFIED SOLUTION
Avatar of thefritterfatboy
thefritterfatboy

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 thefritterfatboy
thefritterfatboy

... should be updated within the function.

(Sorry - I was a bit hasty with the "submit" button there!)
Avatar of jrram

ASKER

Now I'm getting an error when trying to assign it back to a Session variable.

<%

      Function addToCart (byRef virtualCart, itemsList, quantitiesList)
      
            addToCart = false
            
            Dim itemDetails(2)
      
            For i = 0 to UBound(itemsList)
            
                  'itemDetails(0) 'Item ID
                  'itemDetails(1) 'Quantity
                  'itemDetails(2) 'Unit Price
                  'itemDetails(3) 'Item Name
                  'itemDetails(4) 'Total
                  
                  itemDetails(0) = itemsList(i)
                  itemDetails(1) = quantitiesList(i)
                  
                   If virtualCart.Exists(itemsList(i)) Then
                   
                         currItemDetails = virtualCart.Item(itemsList(i))
                         currItemsDetails(1) = currItemsDetails(1) + itemDetails(1)
                         virtualCart.Item(itemsList(i)) = currItemDetails
                   
                  Else
                        virtualCart.Add itemsList(i), itemDetails
                  End If            
            
            Next
            
            'addToCart = virtualCart
            addToCart = true

      End Function


'itemsToOrderList = Split(Trim(Request.Form("idList")), ",")
'qtyToOrderList = Split(Trim(Request.Form("qtyList")), ",")

itemsToOrderList = Array(12, 177, 89)
qtyToOrderList = Array(1, 250, 75)

If Session("globalCart") = "" Then

      Response.Write "Cart is currently empty, initializing order..."
      
      Set myCart = Server.CreateObject("Scripting.Dictionary")
      
Else

      Response.Write "Cart currently has items, adding to order..."
      
      myCart = Session("globalCart")

End If

If addToCart (myCart, itemsToOrderList, qtyToOrderList) Then

      Session("globalCart") = myCart '  <-- Problem line
      Response.Write "It is all good..."
      
Else
      Response.Write "something went wrong..."
End If

%>

Error is:
Error Type:
Session object, ASP 0185 (0x8002000E)
A default property was not found for the object.
/comDevelop Files/portalControl/addToCart2.asp, line 60
Avatar of jrram

ASKER

I was able to work that last error out using

Set Session("globalCart") = myCart
Avatar of jrram

ASKER

I increased the points since this is another arm of the question, but now I seems to have a logic error.  Here's the code.

<%

      Function addToCart (byRef virtualCart, itemsList, quantitiesList)
      
            addToCart = false
            
            Dim itemDetails(2)
      
            For i = 0 to UBound(itemsList)
            
                  'itemDetails(0) 'Item ID
                  'itemDetails(1) 'Quantity
                  'itemDetails(2) 'Unit Price
                  'itemDetails(3) 'Item Name
                  'itemDetails(4) 'Total
                  
                  itemDetails(0) = itemsList(i)
                  itemDetails(1) = quantitiesList(i)
                  
                   If virtualCart.Exists(itemsList(i)) Then
                   
                         Response.Write "Before update, Item " & itemsList(i) & " has " & virtualCart.Item(itemsList(i))(1) & " units ordered. <br />"
                   
                         virtualCart.Item(itemsList(i))(1) = virtualCart.Item(itemsList(i))(1) + itemDetails(1)
                         
                  Else
                        virtualCart.Add itemsList(i), itemDetails
                  End If
                  
                  Response.Write "Item " & itemsList(i) & " now has " & virtualCart.Item(itemsList(i))(1) & " units ordered. <br /><br />"            
            
            Next
            
            addToCart = true

      End Function


'itemsToOrderList = Split(Trim(Request.Form("idList")), ",")
'qtyToOrderList = Split(Trim(Request.Form("qtyList")), ",")

itemsToOrderList = Array(12, 177, 89)
qtyToOrderList = Array(1, 250, 75)

If IsNull(Session("globalCart")) Or Not IsObject(Session("globalCart"))Then

      Response.Write "Cart is currently empty, initializing order...<br />"
      
      Set myCart = Server.CreateObject("Scripting.Dictionary")
      
Else

      Response.Write "Cart currently has items, adding to order...<br />"
      
      Set myCart = Session("globalCart")

End If

If addToCart (myCart, itemsToOrderList, qtyToOrderList) Then

      Set Session("globalCart") = myCart
      Response.Write "All additions/updates successful..."
      
Else
      Response.Write "Something went wrong..."
End If

%>

Everytime the page is refreshed, the quantity for each item should double.  The output should be:

On the first pass:
Cart is currently empty, initializing order...
Item 12 now has 1 units ordered.

Item 177 now has 250 units ordered.

Item 89 now has 75 units ordered.

All additions/updates successful...

On the 2nd pass:
Cart currently has items, adding to order...
Before update, Item 12 has 1 units ordered.
Item 12 now has 2 units ordered.

Before update, Item 177 has 250 units ordered.
Item 177 now has 500 units ordered.

Before update, Item 89 has 75 units ordered.
Item 89 now has 150 units ordered.

All additions/updates successful...

The first pass is correct, but the 2nd pass is returning:

Cart currently has items, adding to order...
Before update, Item 12 has 1 units ordered.
Item 12 now has 1 units ordered.

Before update, Item 177 has 250 units ordered.
Item 177 now has 250 units ordered.

Before update, Item 89 has 75 units ordered.
Item 89 now has 75 units ordered.

All additions/updates successful...

I'm guessing there is some logic wrong with this line:
virtualCart.Item(itemsList(i))(1) = virtualCart.Item(itemsList(i))(1) + itemDetails(1)

or

saving back to the session object.

Help me, help me.
Can you also output the contents of: itemDetails(1) so we can see that?

Response.Write "Before update, Item " & itemsList(i) & " has " & virtualCart.Item(itemsList(i))(1) & " units ordered.
<br />"
Response.write "Adding: " & itemDetails(1) & "<br/>"
SOLUTION
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
well this line here ...

virtualCart.Item(itemsList(i))(1) already contains the qty
and itemDetails(1) you set to the qty ...
I think what you are looking for is this

virtualCart.Item(itemsList(i))(1) = itemDetails(1)

because quantitylist will have the updated quantites first not the cart right?
Avatar of jrram

ASKER

quantityList will have the number of additional quantities of the item the user wants to order.  So, that why I'm trying to add it to the values that are existing in the cart.
well it seems in this case quantityList has the same as the cart
Avatar of jrram

ASKER

There just seems to be an issues with storing arrays in the dictionary object.  But it's strange that I can initially store an array in the dictionary object, but I can't update the dictionary array item or store the dictionary object array back to a variable.
Avatar of jrram

ASKER

It seems that you can't update the array items directly, you have to save the entire array to a variable, update the array, then re-save the array to the dictionary object. ...this code works...

<%

     Function addToCart (virtualCart, itemsList, quantitiesList)
     
          addToCart = false
         
          Dim itemDetails(2)
     
          For i = 0 to UBound(itemsList)
         
               'itemDetails(0) 'Item ID
               'itemDetails(1) 'Quantity
               'itemDetails(2) 'Unit Price
               'itemDetails(3) 'Item Name
               'itemDetails(4) 'Total
               
               itemDetails(0) = itemsList(i)
               itemDetails(1) = quantitiesList(i)
               
                If virtualCart.Exists(itemsList(i)) Then
               
                     'Response.Write "Before update, Product# " & itemsList(i) & " has " & virtualCart.Item(itemsList(i))(1) & " units ordered.<br />"
                     'Response.Write "Adding..." & itemDetails(1) & "additional units of this item to order.<br />"
                     
                     If IsArray (virtualCart.Item(itemsList(i))) Then
                     
                           arrayToUpdate = (virtualCart.Item(itemsList(i)))
                           
                           arrayToUpdate(1) = arrayToUpdate(1) + CInt(itemDetails(1))
                           virtualCart.Item(itemsList(i)) = arrayToUpdate
                     
                     End If
                   
               Else
                    virtualCart.Add itemsList(i), itemDetails
               End If
               
               'Response.Write "Product# " & itemsList(i) & " now has " & virtualCart.Item(itemsList(i))(1) & " units ordered. <br /><br />"
         
          Next
         
          Set addToCart = virtualCart

     End Function


'itemsToOrderList = Split(Trim(Request.Form("idList")), ",")
'qtyToOrderList = Split(Trim(Request.Form("qtyList")), ",")

itemsToOrderList = Array(12, 177, 89)
qtyToOrderList = Array(1, 250, 75)

If IsNull(Session("globalCart")) Or Not IsObject(Session("globalCart"))Then

     Response.Write "Cart is currently empty, initializing order...<br />"
     
     Set myCart = Server.CreateObject("Scripting.Dictionary")
     
Else

     Response.Write "Cart currently has items, adding to order...<br />"
     
     Set myCart = Session("globalCart")

End If

Set myCart = addToCart (myCart, itemsToOrderList, qtyToOrderList)

%>
you may want to look into changing your cart to work with cookies not session ... since session is using valuable server memory, where-as cookies are using "who cares" client memory