05-10-12, 06:20 PM
تابع..................................
في واقع الأمر عندما يأتي الأمر استخدام IEnumerable و IEnumerator فإن أفضل البدائل لهما كلا من
IEnumerable(Of T) Interface
IEnumerator(Of T) Interface
وفي المثال التالي توضيح لكيفية عمل Implementation لهذان Interfaces
والكلاسات المرفقة بالمثال التالي يمكنكم استخدامها بأي شكل من الأشكال في أكوادكم فهي كلاسات عامة حيث يمكن استخدامها مع مجموعات من Strings و Integers و Colors
مثال :
ولإستخدام الكلاس الموجود أعلاه
افتح مشروع واضف الي الفورم 2 ليست بوكس واكتب الكود بالشكل التالي
طبعا لا أريد أن أطيل عليكم في الموضوع الخاص بكل من IEnumerable و IEnumerator وبشكل عام من لديه اي استفسار في هذا الأمر يمكنه أن يسأله بالتأكيد
وفي المشاركات التالية سنناقش كيفية الاستفادة من هذان Interfaces في موضوع المقال
تقبلوال تحياتي
أخوكم عمر
في واقع الأمر عندما يأتي الأمر استخدام IEnumerable و IEnumerator فإن أفضل البدائل لهما كلا من
IEnumerable(Of T) Interface
IEnumerator(Of T) Interface
وفي المثال التالي توضيح لكيفية عمل Implementation لهذان Interfaces
والكلاسات المرفقة بالمثال التالي يمكنكم استخدامها بأي شكل من الأشكال في أكوادكم فهي كلاسات عامة حيث يمكن استخدامها مع مجموعات من Strings و Integers و Colors
مثال :
كود :
''' <summary>
''' Copyright © Omar Amin Ibrahim 2011
''' silverlight1212@yahoo.com
''' </summary>
''' <remarks></remarks>
Public NotInheritable Class RiverNileEnumerator
Public Shared Function GetList(Of T)(ByVal list As IList(Of T)) As RiverNileListEnumerator(Of T)
If (list Is Nothing) Then
Throw New ArgumentNullException("list")
End If
Return New RiverNileListEnumerator(Of T)(list)
End Function
Public Shared Function GetCollection(Of T)(ByVal collection As ICollection(Of T)) As RiverNileCollectionEnumerator(Of T)
If (collection Is Nothing) Then
Throw New ArgumentNullException("collection")
End If
Return New RiverNileCollectionEnumerator(Of T)(collection)
End Function
Public Class RiverNileCollectionEnumerator(Of T)
Implements IEnumerable(Of T)
Implements IEnumerator(Of T)
#Region " Fields "
Private _collectiont As ICollection(Of T)
Private _index As Integer
Private _current As T
#End Region
#Region " Constructor "
Public Sub New(ByVal collection As ICollection(Of T))
MyBase.New()
_collectiont = collection
_index = -1
_current = CType(Nothing, T)
End Sub
#End Region
#Region " Properties "
Public ReadOnly Property Current As T
Get
If _current Is Nothing Then
Throw New InvalidOperationException()
End If
Return _current
End Get
End Property
Private ReadOnly Property System_Collections_Generic_Current As T Implements System.Collections.Generic.IEnumerator(Of T).Current
Get
Return Me.Current
End Get
End Property
Private ReadOnly Property System_Collections_Current As Object Implements System.Collections.IEnumerator.Current
Get
Return Me.Current
End Get
End Property
#End Region
#Region " Methods "
Public Function GetEnumerator() As IEnumerator(Of T)
Return New RiverNileCollectionEnumerator(Of T)(Me._collectiont)
End Function
Public Function MoveNext() As Boolean
_index = _index + 1
If _index = _collectiont.Count Then
Return False
Else
_current = _collectiont(_index)
End If
Return True
End Function
Public Sub Reset()
_index = -1
_current = CType(Nothing, T)
End Sub
Private Function System_Collections_Generic_GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return Me.GetEnumerator
End Function
Private Function System_Collections_GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return Me.GetEnumerator
End Function
Private Function System_Collections_MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
Return MoveNext()
End Function
Private Sub System_Collections_Reset() Implements System.Collections.IEnumerator.Reset
Me.Reset()
End Sub
Private Sub System_IDisposable_Dispose() Implements IDisposable.Dispose
' do nothing
End Sub
#End Region
End Class
Public Class RiverNileListEnumerator(Of T)
Implements IEnumerable(Of T)
Implements IEnumerator(Of T)
#Region " Fields "
Private _list As IList(Of T)
Private _index As Integer
Private _current As T
#End Region
#Region " Constructor "
Public Sub New(ByVal list As IList(Of T))
MyBase.New()
_list = list
_index = -1
_current = CType(Nothing, T)
End Sub
#End Region
#Region " Properties "
Public ReadOnly Property Current As T Implements System.Collections.Generic.IEnumerator(Of T).Current
Get
If _current Is Nothing Then
Throw New InvalidOperationException()
End If
Return _current
End Get
End Property
Private ReadOnly Property System_Collections_Current As Object Implements System.Collections.IEnumerator.Current
Get
Return Me.Current
End Get
End Property
#End Region
#Region " Methods "
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return New RiverNileListEnumerator(Of T)(Me._list)
End Function
Private Function System_Collections_GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return Me.GetEnumerator
End Function
Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
_index = _index + 1
If _index = _list.Count Then
Return False
Else
_current = _list(_index)
End If
Return True
End Function
Public Sub Reset() Implements System.Collections.IEnumerator.Reset
_index = -1
_current = CType(Nothing, T)
End Sub
Private Sub System_IDisposable_Dispose() Implements IDisposable.Dispose
' do nothing
End Sub
#End Region
End Class
End Class ' RiverNileEnumeratorافتح مشروع واضف الي الفورم 2 ليست بوكس واكتب الكود بالشكل التالي
كود :
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ColorList As ICollection(Of KnownColor) = New List(Of KnownColor) From {KnownColor.Aqua, KnownColor.Red, KnownColor.Black}
For Each clr As KnownColor In RiverNileEnumerator.GetCollection(ColorList)
ListBox1.Items.Add(clr)
Next
Dim StringList As IList(Of String) = New List(Of String) From {"Omar", "Amin", "Ibrahim"}
For Each Str As String In RiverNileEnumerator.GetList(StringList)
ListBox2.Items.Add(Str)
Next
End Sub
End Classطبعا لا أريد أن أطيل عليكم في الموضوع الخاص بكل من IEnumerable و IEnumerator وبشكل عام من لديه اي استفسار في هذا الأمر يمكنه أن يسأله بالتأكيد
وفي المشاركات التالية سنناقش كيفية الاستفادة من هذان Interfaces في موضوع المقال
تقبلوال تحياتي
أخوكم عمر
