Solved

arraylist comparison

Posted on 2011-09-09
3
300 Views
Last Modified: 2012-05-12
I had an arraylist with values

DOCX
DOC
DOCX
DOC
DOC
DOC

I want to check whether all the values in the arraylist are equal or not

here the arraylsit values are different (4 DOC 2 DOCX) if different i want to set a flag to false
else set flag to true.

0
Comment
Question by:shragi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36513756
It should be something along the lines of:
Dim baseLine As String = yourArrayList(0).ToString()
Dim allEqual As Boolean = True

For Each item In yourArrayList
    If item.ToString() <> baseLine Then
        allEqual = False
        Exit For
    End If
Next

If allEqual Then
    MessageBox.Show("All items are equal")
Else
    MessageBox.Show("All items are NOT equal")
End If

Open in new window

0
 
LVL 19

Accepted Solution

by:
Shahan Ayyub earned 500 total points
ID: 36513961
Hi!

Have a look at this way:

            bool IsEqual=false;
            ArrayList elements = new ArrayList(new object[] { "DOC", "DOC", "DOC", "DOC", "DOC", "DOC" });
            elements.Sort();
            if (elements[elements.Count - 1] == elements[0])  
                IsEqual = true;

Open in new window

0
 
LVL 19

Assisted Solution

by:Shahan Ayyub
Shahan Ayyub earned 500 total points
ID: 36514038
If you can use LINQ you should look at Distinct() and Count() methods. That would be even more shorter than my previous solution.

This link may help you on that: (where most of the LINQ examples are given)
http://msdn.microsoft.com/en-us/vbasic/bb688085

VB.Net version of my previous solution:
Dim IsEqual As Boolean = False
Dim elements As New ArrayList(New Object() {"DOC", "DOC", "DOC", "DOC", "DOC", "DOC"})
elements.Sort()
If elements(elements.Count - 1) = elements(0) Then
	IsEqual = True
End If

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question