Compare two lists for equality

 أحيانا قد نحتاج أن نتاكد أن العناصر الموجودة في مصفوفة ما مطابقة تماما للعناصر الموجودة في مصفوفة أخري

و الكود التالي يقوم بهذه العملية
 
  Public Function Compare(Of T)(first As IEnumerable(Of T), second As IEnumerable(Of T)) As Boolean
        If second Is Nothing AndAlso first Is Nothing Then
            Return True
        End 
If
 
       If second Is Nothing OrElse first Is Nothing Then
            Return False
        End 
If

 
       Dim identical As Boolean = (first.Count second.Count)
 
       If identical Then
            For Each current 
As T In second
                If Not first
.Contains(currentThen
                    identical 
False
                    Exit 
For
 
               End If
 
           Next
        End 
If

 
       Return identical
    End 
Function 

الكود التالي يوضح بعض الاساليب لكيفية استخدام الدالة أعلاه

 Usage :

Dim l1 As New ArrayList
        Dim l2 
As New ArrayList

        l1
.Add("omar")
 
       l2.Add("omar")

 
       l1.Add("ahmed")
 
       l2.Add("Mohammed")


 
       l1.Add(10)
 
       l2.Add(10)

 
       l1.Add(Color.Black)
 
       l2.Add(Color.Black)


 
       If Compare(Of String)(l1.OfType(Of String), l2.OfType(Of String)) Then
            
' do something
            MessageBox.Show("both lists are identical")
        Else
            '
do something
            MessageBox
.Show("both lists are not identical")
 
       End If


 
       If Compare(Of Integer)(l1.OfType(Of Integer), l2.OfType(Of Integer)) Then
            MessageBox
.Show("both lists are identical")
 
       Else
            MessageBox
.Show("both lists are not identical")
 
       End If

 
       If Compare(Of Color)(l1.OfType(Of Color), l2.OfType(Of Color)) Then

            MessageBox
.Show("both lists are identical")
 
       Else
            MessageBox
.Show("both lists are not identical")
 
       End If

Comments

Popular posts from this blog

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET