Solved

arraylist comparison

Posted on 2011-09-09
3
265 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
  • 2
3 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now