dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Iterating thru Dictionary

  1. #1
    Chris L. Guest

    Iterating thru Dictionary


    I keep getting an error when I use For Each Next with the dictionary. For
    ex:
    'lPhone being the object, mMainFile being the dictionary.

    For Each lPhone In mMainFile
    If Not (lPhone.LoanBal = "0.00") Then
    Call mMainFile.Remove(lPhone.ID)
    End If
    Next

    This gives the error: 'Object required.'

    Does the Dictionary object in the Micrsoft Scripting Runtime not support
    For Each Next? (I assumed it would mimic collections in almost every way.)
    If this is not supported, I assume I just use the method used with arrays
    - Plain old For Next. (Or Do Until/While loops if you prefer.)

    Thanks,
    Chris

  2. #2
    Tom Barnaby Guest

    Re: Iterating thru Dictionary


    Chris,

    I'm not sure if this is your problem, but it is a common one ...

    The iterator variable, lPhone in your example, has to be typed as a Variant.
    Furthermore, it is the *Keys* you are iterating over, not the items. I'll
    take a stab at rewriting this block of code ...

    Dim vKey as Variant
    For Each vKey in mMainFile
    If Not mMainFile(vKey).LoanBal = "0.00" then


    "Chris L." <clintvedt@ifcu.com> wrote:
    >
    >I keep getting an error when I use For Each Next with the dictionary. For
    >ex:
    >'lPhone being the object, mMainFile being the dictionary.
    >
    > For Each lPhone In mMainFile
    > If Not (lPhone.LoanBal = "0.00") Then
    > Call mMainFile.Remove(lPhone.ID)
    > End If
    > Next
    >
    >This gives the error: 'Object required.'
    >
    >Does the Dictionary object in the Micrsoft Scripting Runtime not support
    >For Each Next? (I assumed it would mimic collections in almost every way.)
    >If this is not supported, I assume I just use the method used with arrays
    >- Plain old For Next. (Or Do Until/While loops if you prefer.)
    >
    >Thanks,
    >Chris



  3. #3
    Tom Barnaby Guest

    Re: Iterating thru Dictionary


    Chris,

    I'm not sure if this is your problem, but it is a common one ...

    The iterator variable, lPhone in your example, has to be typed as a Variant.
    Furthermore, it is the *Keys* you are iterating over, not the items. That
    said, I'll take a stab at rewriting this block of code ...

    Dim vKey as Variant
    For Each vKey in mMainFile
    If Not (mMainFile(vKey).LoanBal = "0.00") then
    mMainFile.Remove vKey
    End If
    Next vKey

    Hope that helps

    Tom Barnaby
    www.intertech-inc.com


    "Chris L." <clintvedt@ifcu.com> wrote:
    >
    >I keep getting an error when I use For Each Next with the dictionary. For
    >ex:
    >'lPhone being the object, mMainFile being the dictionary.
    >
    > For Each lPhone In mMainFile
    > If Not (lPhone.LoanBal = "0.00") Then
    > Call mMainFile.Remove(lPhone.ID)
    > End If
    > Next
    >
    >This gives the error: 'Object required.'
    >
    >Does the Dictionary object in the Micrsoft Scripting Runtime not support
    >For Each Next? (I assumed it would mimic collections in almost every way.)
    >If this is not supported, I assume I just use the method used with arrays
    >- Plain old For Next. (Or Do Until/While loops if you prefer.)
    >
    >Thanks,
    >Chris



  4. #4
    Tom Barnaby Guest

    Re: Iterating thru Dictionary



    Whoops!! Ignore that, don't know what happened there ....


  5. #5
    Matthew Solnit Guest

    Re: Iterating thru Dictionary

    Chris L. <clintvedt@ifcu.com> wrote in message news:39f60916@news.devx.com...
    >
    > I keep getting an error when I use For Each Next with the dictionary. For
    > ex:
    > 'lPhone being the object, mMainFile being the dictionary.
    >
    > For Each lPhone In mMainFile
    > If Not (lPhone.LoanBal = "0.00") Then
    > Call mMainFile.Remove(lPhone.ID)
    > End If
    > Next
    >
    > This gives the error: 'Object required.'
    >
    > Does the Dictionary object in the Micrsoft Scripting Runtime not support
    > For Each Next? (I assumed it would mimic collections in almost every way.)
    > If this is not supported, I assume I just use the method used with arrays
    > - Plain old For Next. (Or Do Until/While loops if you prefer.)


    Tom's post is correct. When you use For Each with a Dictionary, you are
    iterating over the keys. But that's not the answer to your question is it? ;-)

    Try this:

    For Each lPhone In mMainFile.Items
    Next


    -- Matthew Solnit



  6. #6
    Chris L. Guest

    Re: Iterating thru Dictionary


    Thanks Tom,
    Your idea works quite well and is a lot cleaner than the solution I came
    up with.
    (Which was dumping the dictionary into an array using the DictionaryObj.Items
    method, and then iterating through that.)

    Chris


    "Tom Barnaby" <tbarnaby@intertech-inc.com> wrote:
    >
    >Chris,
    >
    >I'm not sure if this is your problem, but it is a common one ...
    >
    >The iterator variable, lPhone in your example, has to be typed as a Variant.
    > Furthermore, it is the *Keys* you are iterating over, not the items. That
    >said, I'll take a stab at rewriting this block of code ...
    >
    >Dim vKey as Variant
    >For Each vKey in mMainFile
    > If Not (mMainFile(vKey).LoanBal = "0.00") then
    > mMainFile.Remove vKey
    > End If
    >Next vKey
    >
    >Hope that helps
    >
    >Tom Barnaby
    >www.intertech-inc.com
    >
    >
    >"Chris L." <clintvedt@ifcu.com> wrote:
    >>
    >>I keep getting an error when I use For Each Next with the dictionary. For
    >>ex:
    >>'lPhone being the object, mMainFile being the dictionary.
    >>
    >> For Each lPhone In mMainFile
    >> If Not (lPhone.LoanBal = "0.00") Then
    >> Call mMainFile.Remove(lPhone.ID)
    >> End If
    >> Next
    >>
    >>This gives the error: 'Object required.'
    >>
    >>Does the Dictionary object in the Micrsoft Scripting Runtime not support
    >>For Each Next? (I assumed it would mimic collections in almost every way.)
    >>If this is not supported, I assume I just use the method used with arrays
    >>- Plain old For Next. (Or Do Until/While loops if you prefer.)
    >>
    >>Thanks,
    >>Chris

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center