?
Solved

Can't Return Dictionary Object from Function

Posted on 2005-04-15
13
Medium Priority
?
982 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:jrram
  • 6
  • 4
  • 3
13 Comments
 
LVL 10

Accepted Solution

by:
thefritterfatboy earned 600 total points
ID: 13791473
Use byRef instead.

Function addToCart (byref virtualCart, itemsList, quantitiesList)
' if anything goes wrong false will be returned if you've trapped using an "exit function" statement
addToCart = false
' do function code
'if it's all worked OK:
addToCart = true
end function

then when calling your function just use:

if addToCart (myCart, itemsToOrderList, qtyToOrderList) then
' do your work with "myCart" dictionary
else
' error: addtoCart failed to work
end if

"myCart" is being passed as a reference and
0
 
LVL 10

Expert Comment

by:thefritterfatboy
ID: 13791478
... should be updated within the function.

(Sorry - I was a bit hasty with the "submit" button there!)
0
 
LVL 13

Author Comment

by:jrram
ID: 13791674
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Author Comment

by:jrram
ID: 13791709
I was able to work that last error out using

Set Session("globalCart") = myCart
0
 
LVL 13

Author Comment

by:jrram
ID: 13792121
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.
0
 
LVL 10

Expert Comment

by:thefritterfatboy
ID: 13792181
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/>"
0
 
LVL 10

Assisted Solution

by:ThaSmartUno
ThaSmartUno earned 400 total points
ID: 13792684
just for future reference ... just like you set the dictionary element to a session variable ...

to return the dictionary element you would do
Set addToCart = virtualCart '<-- Here is the problem line
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13792726
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?
0
 
LVL 13

Author Comment

by:jrram
ID: 13793077
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.
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13793095
well it seems in this case quantityList has the same as the cart
0
 
LVL 13

Author Comment

by:jrram
ID: 13793596
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.
0
 
LVL 13

Author Comment

by:jrram
ID: 13793702
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)

%>
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13793717
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

621 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