تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
مقال - مقدمة لصناعة Nested Control
#2
تابع..................................

في واقع الأمر عندما يأتي الأمر استخدام 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 في موضوع المقال


تقبلوال تحياتي
أخوكم عمر
}}}
تم الشكر بواسطة:


الردود في هذا الموضوع
مقال - مقدمة لصناعة Nested Control - بواسطة Raggi Tech - 05-10-12, 06:20 PM

المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  مقدمة الي تشفير الحروف الأبجدية العربية silverlight 3 10,076 17-05-22, 02:23 AM
آخر رد: flawer69
  التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI - مقدمة RaggiTech 3 6,341 30-07-21, 05:14 PM
آخر رد: kebboud
  مقال: الكومبو بوكس ComboBox كيف تضيف أيقونات Blue Sky 1 3,454 30-06-19, 10:41 AM
آخر رد: invocker
  [كود] Line Control silverlight 1 3,177 29-05-19, 10:30 PM
آخر رد: egbest2
  [سؤال] How to Setting the FileUpload value using VB.net inside of a WebBrowser Control dametucorazon 1 2,360 18-03-19, 08:58 PM
آخر رد: 5000
  مقدمة إلي ضغط الصور ..... Zip Bitmap silverlight 0 2,599 10-05-18, 04:35 AM
آخر رد: silverlight
  تصميم ثيمات للكونترول Control Theme silverlight 4 4,158 23-06-17, 02:34 AM
آخر رد: ابراهيم كركوكي
Rainbow Toolbox in VB.NET - Common Controls - Lesson One - Button Control Properties Genius Live 19 16,138 17-06-17, 01:11 PM
آخر رد: نوره
  مقدمة إلي إخفاء المعلومات - الجزء الأول silverlight 5 4,505 07-01-17, 10:44 PM
آخر رد: Basil Abdallah
  مقدمة إلي إخفاء المعلومات - الجزء الثاني silverlight 1 3,210 06-01-17, 11:52 AM
آخر رد: silverlight

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


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