Link to home
Start Free TrialLog in
Avatar of ispcorp
ispcorp

asked on

Argument 'Index' is not a valid value

Anybody know why i'm getting this error...
"Argument 'Index' is not a valid value"

There is a string value that its passing to it, I check that...


Private Function getSessionProduct(ByVal paramSessionCol As Collection, ByVal paramProductCol As Collection) As Collection
        Dim colSessionProduct As Collection
        Dim intCtr As Int32
        Dim itSize As Int32 = paramSessionCol.Count
        Dim objSessionElement As element
        Dim objElement As element

        For intCtr = 1 To itSize
            colSessionProduct = paramSessionCol.Item(intCtr)
            objSessionElement = colSessionProduct.Item(1)
            objElement = paramProductCol.Item(objSessionElement.ProductCode) <----THIS IS WHERE THE ERROR IS HAPPENING
            If Not objElement Is Nothing Then
                getSessionProduct = colSessionProduct
                Exit For
            End If
        Next
        getSessionProduct = Nothing
    End Function
    Private Sub
Avatar of brdrok
brdrok

objSessionElement.ProductCode  returns an integer?
Change your For loop like following:

For intCtr = 0 To itSize - 1

-tushar
Avatar of ispcorp

ASKER

objSessionElemeent.ProductCode returns a string

Tusharashah...
An index of a collection can never have an index of 0, it blows up if I pass intCtr=0 into it.
hmmm...could you please post the code for the Item() method.  This might provide more clues as to what is genrating this error.  My first gut reaction is that it's expecting a type integer but it's being a fred a type of string.

 paramProductCol.Item(objSessionElement.ProductCode)
use Items, that the default Property Name
=======
objElement = paramProductCol.Items(objSessionElement.ProductCode),
you could also do this:

objElement = paramProductCol(objSessionElement.ProductCode)
Avatar of ispcorp

ASKER

Item is a function within the Collection object.  There is not method for it.

b1xml2...I don't see the difference, but I'll try it.
>>objElement = paramProductCol.Item(objSessionElement.ProductCode)

u will get this error when objSessionElement.ProductCode returns an invalid key i.e. no item found in the collection for the key
ok .. i tried this out to validate what i had mentioned above

in page load
        Dim x As New Collection
        x.Add("s", "1")
        x.Add("s1", "2")

        Response.Write(x.Item("2"))

Prints "s1"

Now changed the code to search for a collection item with an invalid key
        Dim x As New Collection
        x.Add("s", "1")
        x.Add("s1", "2")

        Response.Write(x.Item("3"))

got the error
Argument 'Index' is not a valid value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Argument 'Index' is not a valid value.
Avatar of ispcorp

ASKER

I know, isn't there anything that would cause this error not to happen if an item is not found.  I know in a HashMap you can check to see if it exists.  I finally got a work around where I do a Try Catch, in the case where it can't find it, it just continues with the variable being NOTHING.  There has got to be a better way, though...
ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

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 ispcorp

ASKER

Ok, Rejojohny....Your right...I thought there might be another way, with out kicking off an error...Thanks