Make sure all items in a Collection equal the same thing

R

RyanH

Is there another way to test if all item value in a Collection or Array are
the same thing? For example, I want to make sure that all items in the
Collection or Array = "Complete". here is the code I use. I was wondering
if I am doing this a long about way.

For j = 1 To colSalesOrderStatus.Count - 1
If colSalesOrderStatus(j) <> colSalesOrderStatus(j + 1) Then
'do something
Exit For
End If
Next j
 
P

Per Jessen

Hi Ryan

First I would test against the desired value ie. <> Complere,

the I would use this loop:

For Each colItem In colSalesOrderStatus
If colItem <> "Complete" Then
'Do something
Exit For
End If
Next

Regards,
Per
 
P

Per Jessen

Hi Ryan

First I would compare against a constant value, earlier elements in my
collection as it requires that first item is equal to "Complete"

Second, my code would look like this:

For Each colItem In colSalesOrderStatus
If colItem <> "Complete" Then
'Do something
Exit For
End If
Next

Regards,
Per
 
R

RyanH

Thanks for the reply, but I was wondering if there was a shortcut way of
testing if all objects in the collection are the same. I already have a
loop. Plus the loop you wrote won't work for me because the collection may
contain all strings such as "Complete", "Ready", or "Archive".

I was thinking something like this: I know this doesn't work, for
demonstration purposes

Collection.AllValues = "Complete"
--
Cheers,
Ryan


Per Jessen said:
Hi Ryan

First I would compare against a constant value, earlier elements in my
collection as it requires that first item is equal to "Complete"

Second, my code would look like this:

For Each colItem In colSalesOrderStatus
If colItem <> "Complete" Then
'Do something
Exit For
End If
Next

Regards,
Per
 

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

Top