Dictionary Issue

Discussion in 'ASP General' started by CJM, Jan 31, 2006.

  1. CJM

    CJM Guest

    I'm using a dictionary to record some totals, however, my first attempt
    failed and I wasnt sure why...

    snippet from original code:

    oTotalFilters.Item(rsNetStock.fields("PartNo")) =
    oTotalFilters.Item(rsNetStock.fields("PartNo") ) + CInt(iNumFilters)


    After going back to the start, I eventually realised that the dictionary
    Item property didn't like 'rsNetStock.fields("PartNo")' as a parameter, so I
    had to use something like the following:

    sTest = rsNetStock.fields("PartNo")
    oTotalFilters.Item(sTest) = oTotalFilters.Item(sTest) + CInt(iNumFilters)

    Am I missing something here? Surely, my original code should have worked?

    Is this a known bug/feature?

    Thanks

    CJM

    --

    [remove the obvious bits]
    CJM, Jan 31, 2006
    #1
    1. Advertising

  2. CJM

    Tim Slattery Guest

    "CJM" <> wrote:

    >I'm using a dictionary to record some totals, however, my first attempt
    >failed and I wasnt sure why...
    >
    >snippet from original code:
    >
    >oTotalFilters.Item(rsNetStock.fields("PartNo")) =
    >oTotalFilters.Item(rsNetStock.fields("PartNo") ) + CInt(iNumFilters)
    >
    >
    >After going back to the start, I eventually realised that the dictionary
    >Item property didn't like 'rsNetStock.fields("PartNo")' as a parameter,


    No reason it shouldn't like that. The argument to "item" is expected
    to be a character string. The statement you give above assumes that
    there is already something stored under that string, otherwise there
    would be nothing to retrieve and add to.


    > so I
    >had to use something like the following:
    >
    > sTest = rsNetStock.fields("PartNo")
    >oTotalFilters.Item(sTest) = oTotalFilters.Item(sTest) + CInt(iNumFilters)
    >
    >Am I missing something here? Surely, my original code should have worked?


    What were you expecting to happen? What happened instead?

    --
    Tim Slattery
    MS MVP(DTS)
    Tim Slattery, Jan 31, 2006
    #2
    1. Advertising

  3. CJM

    CJM Guest

    "Tim Slattery" <> wrote in message
    news:...
    >>
    >> sTest = rsNetStock.fields("PartNo")
    >>oTotalFilters.Item(sTest) = oTotalFilters.Item(sTest) + CInt(iNumFilters)
    >>

    >
    > What were you expecting to happen? What happened instead?
    >


    I was expecting the dictionary item to be updated.

    When I retrieve the dictionary items later they were all 0. The reason why
    is that 'oTotalFilters.Item(rsNetStock.fields("PartNo")) ' resolved to null
    (whereas 'oTotalFilters.Item(sTest)' resolved to the appropriate value.
    CJM, Jan 31, 2006
    #3
  4. Hi CJM,

    For Vbscript, it is dynamic script which use late binding, and the type of
    the objects used in script are validated at runtime. For your scenario, is
    the rsNetStock an ADO.RecordSet? If so, the RecordSet.fields(key) return
    a ADO field object, not directly the value. While we simply assign it to a
    variable like

    dim id = rsRecords.Fields("id")

    that's ok. However, if we directly pass it to the dictionary object's Add
    method or key/value accessor, the runtime engine will fail to parse the
    object. If you do not want to use additional temp variable, you need to
    explicitly use the "Value" property to access the data value of each field
    in record row. e.g:

    =========================
    while not myRS.EOF

    Response.Write("<br/>CategoryName: " & myRS("CategoryName"))


    list(myRS.Fields("CategoryID").Value) = myRS.Fields("CategoryName").Value &
    " value"


    myRS.MoveNext()

    wEnd
    ========================

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], Feb 1, 2006
    #4
  5. CJM

    CJM Guest

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi CJM,
    >
    > For Vbscript, it is dynamic script which use late binding, and the type of
    > the objects used in script are validated at runtime. For your scenario, is
    > the rsNetStock an ADO.RecordSet? If so, the RecordSet.fields(key) return
    > a ADO field object, not directly the value. While we simply assign it to a
    > variable like
    >
    > dim id = rsRecords.Fields("id")
    >
    > that's ok. However, if we directly pass it to the dictionary object's
    > Add
    > method or key/value accessor, the runtime engine will fail to parse the
    > object. If you do not want to use additional temp variable, you need to
    > explicitly use the "Value" property to access the data value of each field
    > in record row. e.g:
    >
    > =========================
    > while not myRS.EOF
    >
    > Response.Write("<br/>CategoryName: " & myRS("CategoryName"))
    >
    >
    > list(myRS.Fields("CategoryID").Value) = myRS.Fields("CategoryName").Value
    > &
    > " value"
    >
    >
    > myRS.MoveNext()
    >
    > wEnd
    > ========================
    >



    Stephen,

    Thanks for your response. It explains a lot.

    I think I'll omit the extra variable and use '.value' instead.

    Thanks

    Chris
    CJM, Feb 1, 2006
    #5
  6. You're welcome CJM,

    Good luck!

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], Feb 2, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    431
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    311
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    2,977
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    508
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    172
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page