For Each and collection object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: For Each and collection object

  1. #1
    John K. Guest

    For Each and collection object


    I use a lot of collection objects in my project and code is almost identical
    and I haven't had any problems with them until this one particular collection
    object. For some strange reason, it doesn't work.

    Dim LV As MyObject
    Dim LVS As MyObjects

    Set LVS = GetManyLVS()

    For Each LV In LVS.Count
    If LV.UserID = mobjUser.UserID Then
    AddUserVoteToListViewManual LV
    End If
    Next

    I'm getting "For Each may only iterate over a collection object or an array"
    error on the "For Each..." line because the "Count" property doesn't exist.

    FYI, I have these function my this collection class:

    1. Private mcolListRecoVote As New Collection
    2. Public Function NewEnum() As IUnknown
    (with procedure attribute set to -4.
    3. Friend Function Add(newListRecoVote As ListRecoVote)
    4. Public Function Item(vIndex As Variant) As Integer
    5. Public Function Count() As Integer


  2. #2
    Larry Rebich Guest

    Re: For Each and collection object

    dim LVS as Collection not MyObjects

    --
    Cheers,
    Larry Rebich

    More tips link to:
    http://www.buygold.net/tips.html

    Please:
    No personal e-mail questions :-)


    "John K." <johnchulkang@hotmail.com> wrote in message
    news:3a14743d$1@news.devx.com...
    >
    > I use a lot of collection objects in my project and code is almost

    identical
    > and I haven't had any problems with them until this one particular

    collection
    > object. For some strange reason, it doesn't work.
    >
    > Dim LV As MyObject
    > Dim LVS As MyObjects
    >
    > Set LVS = GetManyLVS()
    >
    > For Each LV In LVS.Count
    > If LV.UserID = mobjUser.UserID Then
    > AddUserVoteToListViewManual LV
    > End If
    > Next
    >
    > I'm getting "For Each may only iterate over a collection object or an

    array"
    > error on the "For Each..." line because the "Count" property doesn't

    exist.
    >
    > FYI, I have these function my this collection class:
    >
    > 1. Private mcolListRecoVote As New Collection
    > 2. Public Function NewEnum() As IUnknown
    > (with procedure attribute set to -4.
    > 3. Friend Function Add(newListRecoVote As ListRecoVote)
    > 4. Public Function Item(vIndex As Variant) As Integer
    > 5. Public Function Count() As Integer
    >




  3. #3
    John K. Guest

    Re: For Each and collection object


    No, I don't think that's it. I've used collection object names to Dim throughout
    my application and it works in all other instances except this one.

    Just to see, I did try "As Collection" and the result is the same.

    It's strange VB's Intellisense picks up the Count function of this collection
    object during coding and it doesn't exist when program runs.

    "Larry Rebich" <lrebich@earthlink.net> wrote:
    >dim LVS as Collection not MyObjects
    >
    >--
    >Cheers,
    >Larry Rebich
    >
    >More tips link to:
    >http://www.buygold.net/tips.html
    >
    >Please:
    >No personal e-mail questions :-)
    >
    >
    >"John K." <johnchulkang@hotmail.com> wrote in message
    >news:3a14743d$1@news.devx.com...
    >>
    >> I use a lot of collection objects in my project and code is almost

    >identical
    >> and I haven't had any problems with them until this one particular

    >collection
    >> object. For some strange reason, it doesn't work.
    >>
    >> Dim LV As MyObject
    >> Dim LVS As MyObjects
    >>
    >> Set LVS = GetManyLVS()
    >>
    >> For Each LV In LVS.Count
    >> If LV.UserID = mobjUser.UserID Then
    >> AddUserVoteToListViewManual LV
    >> End If
    >> Next
    >>
    >> I'm getting "For Each may only iterate over a collection object or an

    >array"
    >> error on the "For Each..." line because the "Count" property doesn't

    >exist.
    >>
    >> FYI, I have these function my this collection class:
    >>
    >> 1. Private mcolListRecoVote As New Collection
    >> 2. Public Function NewEnum() As IUnknown
    >> (with procedure attribute set to -4.
    >> 3. Friend Function Add(newListRecoVote As ListRecoVote)
    >> 4. Public Function Item(vIndex As Variant) As Integer
    >> 5. Public Function Count() As Integer
    >>

    >
    >



  4. #4
    Larry Rebich Guest

    Re: For Each and collection object

    How is MyObjects defined?

    --
    Cheers,
    Larry Rebich

    More tips link to:
    http://www.buygold.net/tips.html

    Please:
    No personal e-mail questions :-)


    "John K." <johnchulkang@hotmail.com> wrote in message
    news:3a147f0c$1@news.devx.com...
    >
    > No, I don't think that's it. I've used collection object names to Dim

    throughout
    > my application and it works in all other instances except this one.
    >
    > Just to see, I did try "As Collection" and the result is the same.
    >
    > It's strange VB's Intellisense picks up the Count function of this

    collection
    > object during coding and it doesn't exist when program runs.
    >
    > "Larry Rebich" <lrebich@earthlink.net> wrote:
    > >dim LVS as Collection not MyObjects
    > >
    > >--
    > >Cheers,
    > >Larry Rebich
    > >
    > >More tips link to:
    > >http://www.buygold.net/tips.html
    > >
    > >Please:
    > >No personal e-mail questions :-)
    > >
    > >
    > >"John K." <johnchulkang@hotmail.com> wrote in message
    > >news:3a14743d$1@news.devx.com...
    > >>
    > >> I use a lot of collection objects in my project and code is almost

    > >identical
    > >> and I haven't had any problems with them until this one particular

    > >collection
    > >> object. For some strange reason, it doesn't work.
    > >>
    > >> Dim LV As MyObject
    > >> Dim LVS As MyObjects
    > >>
    > >> Set LVS = GetManyLVS()
    > >>
    > >> For Each LV In LVS.Count
    > >> If LV.UserID = mobjUser.UserID Then
    > >> AddUserVoteToListViewManual LV
    > >> End If
    > >> Next
    > >>
    > >> I'm getting "For Each may only iterate over a collection object or an

    > >array"
    > >> error on the "For Each..." line because the "Count" property doesn't

    > >exist.
    > >>
    > >> FYI, I have these function my this collection class:
    > >>
    > >> 1. Private mcolListRecoVote As New Collection
    > >> 2. Public Function NewEnum() As IUnknown
    > >> (with procedure attribute set to -4.
    > >> 3. Friend Function Add(newListRecoVote As ListRecoVote)
    > >> 4. Public Function Item(vIndex As Variant) As Integer
    > >> 5. Public Function Count() As Integer
    > >>

    > >
    > >

    >




  5. #5
    John K. Guest

    Re: For Each and collection object


    From my original post:

    1. Private mcolListRecoVote As New Collection
    2. Public Function NewEnum() As IUnknown
    (with procedure attribute set to -4.
    3. Friend Function Add(newListRecoVote As ListRecoVote)
    4. Public Function Item(vIndex As Variant) As Integer
    5. Public Function Count() As Integer

    The name of this collection class would be "MyObjects" therefore,
    "Dim LVS As MyObjects" is valid. Again, I don't think this has anything
    to do with it not working but...

    "Larry Rebich" <lrebich@earthlink.net> wrote:
    >How is MyObjects defined?
    >
    >--
    >Cheers,
    >Larry Rebich
    >
    >More tips link to:
    >http://www.buygold.net/tips.html
    >
    >Please:
    >No personal e-mail questions :-)
    >
    >
    >"John K." <johnchulkang@hotmail.com> wrote in message
    >news:3a147f0c$1@news.devx.com...
    >>
    >> No, I don't think that's it. I've used collection object names to Dim

    >throughout
    >> my application and it works in all other instances except this one.
    >>
    >> Just to see, I did try "As Collection" and the result is the same.
    >>
    >> It's strange VB's Intellisense picks up the Count function of this

    >collection
    >> object during coding and it doesn't exist when program runs.
    >>
    >> "Larry Rebich" <lrebich@earthlink.net> wrote:
    >> >dim LVS as Collection not MyObjects
    >> >
    >> >--
    >> >Cheers,
    >> >Larry Rebich
    >> >
    >> >More tips link to:
    >> >http://www.buygold.net/tips.html
    >> >
    >> >Please:
    >> >No personal e-mail questions :-)
    >> >
    >> >
    >> >"John K." <johnchulkang@hotmail.com> wrote in message
    >> >news:3a14743d$1@news.devx.com...
    >> >>
    >> >> I use a lot of collection objects in my project and code is almost
    >> >identical
    >> >> and I haven't had any problems with them until this one particular
    >> >collection
    >> >> object. For some strange reason, it doesn't work.
    >> >>
    >> >> Dim LV As MyObject
    >> >> Dim LVS As MyObjects
    >> >>
    >> >> Set LVS = GetManyLVS()
    >> >>
    >> >> For Each LV In LVS.Count
    >> >> If LV.UserID = mobjUser.UserID Then
    >> >> AddUserVoteToListViewManual LV
    >> >> End If
    >> >> Next
    >> >>
    >> >> I'm getting "For Each may only iterate over a collection object or

    an
    >> >array"
    >> >> error on the "For Each..." line because the "Count" property doesn't
    >> >exist.
    >> >>
    >> >> FYI, I have these function my this collection class:
    >> >>
    >> >> 1. Private mcolListRecoVote As New Collection
    >> >> 2. Public Function NewEnum() As IUnknown
    >> >> (with procedure attribute set to -4.
    >> >> 3. Friend Function Add(newListRecoVote As ListRecoVote)
    >> >> 4. Public Function Item(vIndex As Variant) As Integer
    >> >> 5. Public Function Count() As Integer
    >> >>
    >> >
    >> >

    >>

    >
    >



  6. #6
    Arthur Wood Guest

    Re: For Each and collection object


    John,
    I see two possible problems:

    1)Your Add function a) should be a Sub, not a Function as it does not return
    a result, and b) appears to add ListRecoVote objects to the collection (you
    seem to attempting to iterate throgh a collection of MyObject objects in
    the calling code)

    2) Your Item function is returning an Integer, and should be returning a
    element of the collection - in this case a ListRecoVote object.


    Arthur Wood


    "John K." <johnchulkang@hotmail.com> wrote:
    >
    >I use a lot of collection objects in my project and code is almost identical
    >and I haven't had any problems with them until this one particular collection
    >object. For some strange reason, it doesn't work.
    >
    >Dim LV As MyObject
    >Dim LVS As MyObjects
    >
    >Set LVS = GetManyLVS()
    >
    >For Each LV In LVS.Count
    > If LV.UserID = mobjUser.UserID Then
    > AddUserVoteToListViewManual LV
    > End If
    >Next
    >
    >I'm getting "For Each may only iterate over a collection object or an array"
    >error on the "For Each..." line because the "Count" property doesn't exist.
    >
    >FYI, I have these function my this collection class:
    >
    >1. Private mcolListRecoVote As New Collection
    >2. Public Function NewEnum() As IUnknown
    >(with procedure attribute set to -4.
    >3. Friend Function Add(newListRecoVote As ListRecoVote)
    >4. Public Function Item(vIndex As Variant) As Integer
    >5. Public Function Count() As Integer
    >



  7. #7
    Anthony Jones Guest

    Re: For Each and collection object

    >For Each LV In LVS.Count

    Umm... shouldn't this be

    For Each LV in LVS

    ??

    --
    Anthony Jones
    Nuesoft Ltd




  8. #8
    mrfelis Guest

    Re: For Each and collection object

    I would thing so. Count returns an integer which isn't a collection.

    --
    ~~~
    C'Ya,
    mrfelis
    mrfelis@yahoo.NOSPAM.com
    just remove the spam
    Anthony Jones <yadayadayada@msn.com> wrote in message
    news:3a1504b4$1@news.devx.com...
    > >For Each LV In LVS.Count

    >
    > Umm... shouldn't this be
    >
    > For Each LV in LVS
    >
    > ??
    >
    > --
    > Anthony Jones
    > Nuesoft Ltd
    >
    >
    >




  9. #9
    John K. Guest

    Re: For Each and collection object


    Duh! Good call Anthony. I don't know how that got there and I don't know
    why I couldn't see it.

    I was switching between iterating through the collection object using "For
    Each LV In LVS" and "For i=1 To LVS.Count" and I guess I ended up with mix
    of the two. Thanks!

    "Anthony Jones" <yadayadayada@msn.com> wrote:
    >>For Each LV In LVS.Count

    >
    >Umm... shouldn't this be
    >
    >For Each LV in LVS
    >
    >??
    >
    >--
    >Anthony Jones
    >Nuesoft Ltd
    >
    >
    >



  10. #10
    Kenny Acock Guest

    Re: For Each and collection object


    John,

    >Duh! Good call Anthony. I don't know how that got there and I don't know
    >why I couldn't see it.


    It comes from staring at it too long. We don't all admit it, but we all
    do it every once in a while. Now you know how certain voters feel...

    --Kenny


    "John K." <johnchulkang@hotmail.com> wrote:
    >
    >Duh! Good call Anthony. I don't know how that got there and I don't know
    >why I couldn't see it.
    >
    >I was switching between iterating through the collection object using "For
    >Each LV In LVS" and "For i=1 To LVS.Count" and I guess I ended up with mix
    >of the two. Thanks!
    >
    >"Anthony Jones" <yadayadayada@msn.com> wrote:
    >>>For Each LV In LVS.Count

    >>
    >>Umm... shouldn't this be
    >>
    >>For Each LV in LVS
    >>
    >>??
    >>
    >>--
    >>Anthony Jones
    >>Nuesoft Ltd
    >>
    >>
    >>

    >



  11. #11
    Anthony Jones Guest

    Re: For Each and collection object

    >>
    Duh! Good call Anthony. I don't know how that got there and I don't know
    why I couldn't see it.
    <<

    You couldn't see it because you didn't read it.
    You didn't read it because you didn't need to.
    You didn't need to because you already knew what it said.

    As Kenny says it happens to all of us all of the time.

    For this reason we have found that peer line by line code reviews are our
    most effective bug busters.

    --
    Anthony Jones
    Nuesoft Ltd




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