تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
Enumerating Objects Using Paginating
#1
كاتب الموضوع : Islam Ibrahim

السلام عليكم ورحمة الله وبركاته,

أحيانا نحتاج للقيام بعمل Pagination , مثلاً عندما نرغب في استعراض نظام الملفات ونرغب في الحصول على أول عشر ملفات ضمن مجلد محدد وبعدها الانتقال إلى العشرة ملفات التالية وهكذا ..... وعرضهم على سبيل المثال ضمن DataGrid, ولتحقيق ذلك هناك طرق عديدة و أهمها بناء Typed Enumerator خاص بنا يقوم بهذا العمل.

ال Enumerator التالي يقوم بالمهمة مهما يكن النوع T الذي نتعامل معه سواء كان String أو FileStream او غيره مع تحديد كمية الكائنات التي نرغب في استعراضها في كل مرة:


كود :
Public Class RangeIterator(Of T)
Implements IEnumerator(Of IEnumerable(Of T))


#Region "Fields"

Private _currentRangePosition As Int32 = 0
Private _countPerRange As Int32 = 0
Private _list As IEnumerable(Of T)
Private _rangesCount As Int32 = 1

#End Region

#Region "Implementation"

Public Sub New(ByVal List As IEnumerable(Of T), ByVal countPerRange As Integer)
If List Is Nothing OrElse List.Count = 0 Then
Throw New ArgumentNullException("List", "List cannot be null.")
ElseIf countPerRange <= 0 Then
Throw New ArgumentException("countPerRange", "Range count cannot be equal or less than Zero.")
Else
Me._list = List
Me._countPerRange = countPerRange
Me._currentRangePosition = -1
If _list.Count <= countPerRange Then
Me._rangesCount = 1
Else
If Me._list.Count Mod Me._countPerRange = 0 Then
Me._rangesCount = Me._list.Count / Me._countPerRange
Else
Me._rangesCount = Me._list.Count / Me._countPerRange + 1
End If
End If
End If
End Sub

Public ReadOnly Property Current() As System.Collections.Generic.IEnumerable(Of T) Implements System.Collections.Generic.IEnumerator(Of System.Collections.Generic.IEnumerable(Of T)).Current
Get
Dim CurrentRangeStartIndex = Me._currentRangePosition * Me._countPerRange
Dim CurrentRangeCount = Me._list.Count - CurrentRangeStartIndex
Dim CurrentRangeEndIndex = Math.Min(CurrentRangeCount, Me._countPerRange)
Dim result As IEnumerable(Of T) = Me._list.Skip( _
CurrentRangeStartIndex).Take(CurrentRangeEndIndex)
Return result
End Get

End Property

Public ReadOnly Property Current1() As Object Implements System.Collections.IEnumerator.Current
Get
Return Me.Current
End Get
End Property

Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
If Me._currentRangePosition + 1 < Me._rangesCount Then
Me._currentRangePosition += 1
Return True
Else
Return False
End If
End Function

Public Sub Reset() Implements System.Collections.IEnumerator.Reset
Me._currentRangePosition = -1
End Sub

#End Region

Private disposedValue As Boolean = False ' To detect redundant calls

' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free other state (managed objects).
End If

' TODO: free your own state (unmanaged objects).
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub

#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region

End Class
وهذا مثال بسيط لاستخدام ال Enumerator السابق:


كود :
Dim list As New RangeIterator(Of String)( _
My.Computer.FileSystem.GetFiles("D:\"), 3)

Do While list.MoveNext
Dim msg = <Files>
<%= From itm In list.Current _
Select <File><%= itm & _
Environment.NewLine %></File> %>
</Files>.Value
MsgBox(msg)
Loop
}}}
تم الشكر بواسطة:


التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم