Friday, September 29, 2006

Your Own Serviced Layer ?

Have you ever though of writting a serviced layer? well I just did.. :) I wrote a small example also .. It quit intersting... It easy if you have constat attributes in your project, if not you will be in a sup ..

I have posted the code here..


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim myCars As New Cars
myCars.Add(New Car("SDSDSD"))

For Each b As Car In myCars
MsgBox(b.Name)
Next






End Sub
End Class

Public Class Car

Private mName As String

Public Sub New(ByVal CarName As String)
mName = CarName
End Sub

Public Property Name() As String
Get
Return mName
End Get

Set(ByVal Value As String)
mName = Value
End Set
End Property

End Class

Public Class Cars
Implements IEnumerable

Private mList As New ArrayList

Public Sub Add(ByVal bk As Car)
mList.Add(bk)
End Sub

Default Public ReadOnly Property List(ByVal index As Integer) As Car
Get
Return DirectCast(mList(index), Car)
End Get
End Property

Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return New CarsEnumerator(mList)
End Function

Class CarsEnumerator
Implements IEnumerator

Private index As Integer = -1

Private mList As ArrayList

Friend Sub New(ByVal list As ArrayList)
mList = list
End Sub

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

Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
If index < mList.Count - 1 Then
index += 1
Return True
Else
Return False
End If
End Function

Public Sub Reset() Implements System.Collections.IEnumerator.Reset
index = 0
End Sub
End Class
End Class

No comments: