Dictionary Issue

C

CJM

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
 
T

Tim Slattery

CJM said:
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?
 
C

CJM

Tim Slattery said:
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.
 
S

Steven Cheng[MSFT]

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.)
 
C

CJM

Steven Cheng said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top