25-07-15, 07:55 AM
الكود التالي يوضح شكل الكلاس كاملا و في المشاركات التالية سنوضح كيفية اضافة صفات و طرق اخري للكلاس بل وسنضيف له دوال تجعل من الممكن استخدامه في برامج الداتا بيز التقليدية
كود :
Public Class VB4ArabStringCollection
Implements ICloneable, IEnumerable
#Region " Field "
' define internal list which will work as store for the collection Data
' benifits to use arraylist because it supports sort , binarysearch, ... and so on
' for more information about ArrayList, please visit the below link
' https://msdn.microsoft.com/en-us/library/System.Collections.ArrayList_methods%28v=vs.110%29.aspx
Private list As ArrayList
#End Region
#Region " Constructor "
Public Sub New()
' initialize the internal list
Me.list = New ArrayList()
End Sub
Public Sub New(ByVal capacity As Integer)
' initialize the internal list with a defined capacity
Me.list = New ArrayList(capacity)
End Sub
#End Region
#Region " Property "
Public ReadOnly Property InnerList As ArrayList
Get
If (Me.list Is Nothing) Then
Me.list = New ArrayList
End If
Return Me.list
End Get
End Property
#End Region
#Region " Methods "
' used to add items to the collection
Public Sub Add(item As String)
Me.InnerList.Add(item)
End Sub
' used to remove items from the collection
Public Sub Remove(item As String)
Me.InnerList.Remove(item)
End Sub
' Clone/copy the collection
Public Function Clone() As Object Implements ICloneable.Clone
' define new empty VB4ArabStringCollection
Dim stringCol As VB4ArabStringCollection = New VB4ArabStringCollection
' add all items to the collection
For Each item As Object In Me
' Option 1 add items as object
stringCol.InnerList.Add(item.Clone())
'' Option 2 add item as string
'Dim str As String = CType(item, String)
'stringCol.InnerList.Add(str)
Next
Return stringCol
End Function
' Returns an instance of class VB4ArabStringCollectionEnumerator
Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Return New VB4ArabStringCollectionEnumerator(Me)
End Function
#End Region
#Region " Internal Enumerator "
Private Class VB4ArabStringCollectionEnumerator
Implements IEnumerator
Private location As Integer = -1
Private vb4ArabCollection As VB4ArabStringCollection
Public Sub New(stringCol As VB4ArabStringCollection)
Me.vb4ArabCollection = stringCol
End Sub
' Current retrieves current item from the collection.
Public ReadOnly Property Current As Object Implements IEnumerator.Current
Get
Return Me.vb4ArabCollection.InnerList(location)
End Get
End Property
' MoveNext tells us what to do when user wants to move forward in the collection.
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
If Me.location < Me.vb4ArabCollection.InnerList.Count - 1 Then
Me.location += 1
Return True
Else
Return False
End If
End Function
' Reset sets the initial location/position of the collection
Public Sub Reset() Implements IEnumerator.Reset
location = -1
End Sub
End Class
#End Region
End Class ' VB4ArabStringCollection
