Collections, swapping items in a collection 
Author Message
 Collections, swapping items in a collection

Hi,

I can't seem to work out how to simply swap two items over in a
collection. I need to sort a collection of classes depending on the
value of a class property.

I've tried the following:-
1. First attempt
      For rowCount = 1 To m_rows.Count - 1
        Set currentRow = m_rows(rowCount)
        Set nextRow = m_rows(rowCount + 1)
        if currentRow.value < nextRow.value then
          set tempRow = currentRow
          set currentRow = nextRow
          set nextRow = tempRow
   This fails....

2. Clone method in the row class
   This method is as follows
      Public Sub clone(rowToClone as RowClass)

   When i call this wuth "currentRow.clone(nextRow)"
     This fails with "object does not suport this propety or method"

Any ideas? Or even better a easier way of doing this?

Thanks
Andy

Sent via Deja.com
http://www.*-*-*.com/



Sat, 12 Jul 2003 21:00:25 GMT  
 Collections, swapping items in a collection
Hi,

You can't simply reassign values to items in a collection.  If you want
to change an item, you have to remove it and add a new one in its place:

For RowCount = 1 To m_rows.Count - 1
        Set CurrentRow = m_rows(RowCount)
        Set NextRow = m_rows(RowCount + 1)
        If CurrentRow.value < NextRow.value Then
            Call m_rows.Remove(RowCount)
            Call m_rows.Add(NextRow, before:=RowCount)
            Call m_rows.Remove(RowCount + 1)
            If RowCount < m_rows.Count - 1 Then
                Call m_rows.Add(CurrentRow, before:=RowCount + 1)
            Else
                Call m_rows.Add(CurrentRow)
            End If
        End If
        'Debug.Print m_rows(RowCount).value
 Next

Regards,

John.............

Quote:

> Hi,

> I can't seem to work out how to simply swap two items over in a
> collection. I need to sort a collection of classes depending on the
> value of a class property.

> I've tried the following:-
> 1. First attempt
>       For rowCount = 1 To m_rows.Count - 1
>         Set currentRow = m_rows(rowCount)
>         Set nextRow = m_rows(rowCount + 1)
>         if currentRow.value < nextRow.value then
>           set tempRow = currentRow
>           set currentRow = nextRow
>           set nextRow = tempRow
>    This fails....

> 2. Clone method in the row class
>    This method is as follows
>       Public Sub clone(rowToClone as RowClass)

>    When i call this wuth "currentRow.clone(nextRow)"
>      This fails with "object does not suport this propety or method"

> Any ideas? Or even better a easier way of doing this?

> Thanks
> Andy

> Sent via Deja.com
> http://www.deja.com/



Sun, 13 Jul 2003 02:36:30 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Swapping Collection Items

2. Collection.Add and Collection.Item question?

3. VB Collections - Accessing the collection, not the item.

4. Collections, Collections and More Collections

5. Swap objects in collection

6. ItemRemove event don't fire when removing last item in items collection object

7. All items in my collection = the last item added

8. Document collections and AllForms collections

9. Creating a CDO collection from an Outlook collection

10. using collection of collections

11. Storing a collection in a collection

12. collection in collection

 

 
Powered by phpBB® Forum Software