'Visual Basic 2010 Collecties - Inleiding Object Oriented Programming Class Person Private m_Name As String Public Property Name() As String Get Name = m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property End Class Module Client1 Dim persons As Person() Dim personsCount As Integer Sub Main() Dim person1 As Person = New Person person1.Name = "John" ' Dim person2 As Person = New Person person2.Name = "Jane" ' Console.WriteLine(personsCount) ' AddPerson(person1) AddPerson(person2) ' Console.WriteLine(personsCount) ' Console.WriteLine(persons(1).Name) ' Console.ReadLine() End Sub Sub AddPerson(ByVal item As Person) ReDim Preserve persons(personsCount) persons(personsCount) = item personsCount += 1 End Sub End Module Class Persons Private m_Items As Person() Default Public ReadOnly Property Item(ByVal index As Integer) As Person Get ' (1) Item = m_Items(index) End Get End Property Private m_Count As Integer Public ReadOnly Property Count() As Integer Get Count = m_Count End Get End Property Public Sub Add(ByVal item As Person) ReDim Preserve m_Items(Count) m_Items(Count) = item m_Count += 1 End Sub End Class Module Client2 Sub Main() Dim person1 As Person = New Person person1.Name = "John" ' Dim person2 As Person = New Person person2.Name = "Jane" ' Dim persons1 As Persons = New Persons ' Console.WriteLine(persons1.Count) ' persons1.Add(person1) persons1.Add(person2) ' Console.WriteLine(persons1.Count) ' Console.WriteLine(persons1(1).Name) ' (2) Console.WriteLine(persons1.Item(1).Name) ' (3) ' Console.ReadLine() End Sub End Module Module Client3 Sub Main() Dim person1 As Person = New Person person1.Name = "John" ' Dim persons1 As Persons = New Persons persons1.Add(person1) End Sub End Module Class Rectangle Private m_Height As Integer Public Property Height() As Integer Get Height = m_Height End Get Set(ByVal value As Integer) m_Height = value End Set End Property Private m_Width As Integer Public Property Width() As Integer Get Width = m_Width End Get Set(ByVal value As Integer) m_Width = value End Set End Property Public Function GetArea() As Integer GetArea = Height * Width End Function End Class Class Rectangles Private m_Items As Rectangle() Default Public ReadOnly Property Item(ByVal index As Integer) As Rectangle Get Item = m_Items(index) End Get End Property Private m_Count As Integer Public ReadOnly Property Count() As Integer Get Count = m_Count End Get End Property Public Sub Add(ByVal rectangle As Rectangle) ReDim Preserve m_Items(Count) m_Items(Count) = rectangle m_Count += 1 End Sub Public Sub RemoveAt(ByVal index As Integer) For itemIndex As Integer = index To Count - 2 m_Items(itemIndex) = m_Items(itemIndex + 1) Next ReDim Preserve m_Items(Count - 2) m_Count -= 1 End Sub Public Function GetTotalArea() As Integer For Each item As Rectangle In m_Items GetTotalArea += item.GetArea() Next End Function End Class Module Exercise1Solution Sub Main() Dim rectangle1 As Rectangle = New Rectangle rectangle1.Height = 1 rectangle1.Width = 2 Console.WriteLine(rectangle1.GetArea()) ' Dim rectangle2 As Rectangle = New Rectangle rectangle2.Height = 3 rectangle2.Width = 4 Console.WriteLine(rectangle2.GetArea()) ' Dim rectangles1 As Rectangles = New Rectangles rectangles1.Add(rectangle1) rectangles1.Add(rectangle2) ' For index As Integer = 0 To rectangles1.Count - 1 Console.Write(rectangles1(index).GetArea() & " ") Next Console.WriteLine() Console.WriteLine(rectangles1.GetTotalArea()) ' rectangles1.RemoveAt(1) ' For index As Integer = 0 To rectangles1.Count - 1 Console.Write(rectangles1(index).GetArea() & " ") Next Console.WriteLine() Console.WriteLine(rectangles1.GetTotalArea()) ' Console.ReadLine() End Sub End Module Module SentenceTestFixture Sub Main() Dim sentence1 As Sentence = New Sentence Console.WriteLine(sentence1.Count = 0) Console.WriteLine(sentence1.IndexOf("Hello") = -1) Console.WriteLine(sentence1.Contains("Hello") = False) ' sentence1.Add("Hello") Console.WriteLine(sentence1.Count = 1) Console.WriteLine(sentence1.Item(0) = "Hello") Console.WriteLine(sentence1.IndexOf("Hello") = 0) Console.WriteLine(sentence1.Contains("Hello") = True) Console.WriteLine(sentence1.IndexOf("World") = -1) Console.WriteLine(sentence1.Contains("World") = False) ' sentence1.Add("World") Console.WriteLine(sentence1.Count = 2) Console.WriteLine(sentence1.Item(0) = "Hello") Console.WriteLine(sentence1.Item(1) = "World") Console.WriteLine(sentence1.IndexOf("Hello") = 0) Console.WriteLine(sentence1.Contains("Hello") = True) Console.WriteLine(sentence1.IndexOf("World") = 1) Console.WriteLine(sentence1.Contains("World") = True) ' Console.ReadLine() End Sub End Module Class Sentence Private m_Words As String() = New String() {} Public ReadOnly Property Count() As Integer Get Count = m_Words.Length End Get End Property Default Public ReadOnly Property Item(ByVal index As Integer) As String Get If index >= 0 AndAlso index < Count Then Item = m_Words(index) End Get End Property Public Sub Add(ByVal word As String) ReDim Preserve m_Words(Count) m_Words(Count - 1) = word End Sub Public Function IndexOf(ByVal word As String) As Integer IndexOf = -1 If Count > 0 Then Dim found As Boolean Do IndexOf += 1 found = (Item(IndexOf) = word) Loop Until found OrElse IndexOf = Count - 1 If Not found Then IndexOf = -1 End If End Function Public Function Contains(ByVal word As String) As Boolean Contains = (IndexOf(word) <> -1) End Function End Class Namespace Alternative1 Class Sentence Private m_Count As Integer Private m_Capacity As Integer = 16 Private m_Words(m_Capacity - 1) As String Public ReadOnly Property Count() As Integer Get Count = m_Count End Get End Property Default Public ReadOnly Property Item(ByVal index As Integer) As String Get Item = m_Words(index) End Get End Property Public Sub Add(ByVal word As String) m_Count += 1 If Count > m_Capacity Then m_Capacity *= 2 ReDim Preserve m_Words(m_Capacity - 1) End If m_Words(Count - 1) = word End Sub Public Function IndexOf(ByVal word As String) As Integer IndexOf = -1 If Count > 0 Then Dim found As Boolean Do IndexOf += 1 found = (Item(IndexOf) = word) Loop Until found OrElse IndexOf = Count - 1 If Not found Then IndexOf = -1 End If End Function Public Function Contains(ByVal word As String) As Boolean Contains = (IndexOf(word) <> -1) End Function End Class End Namespace Namespace Alternative2 Class Sentence ' LinkedList Type Private m_StartWord As Word = New Word With {.Value = "sentinel"} Public ReadOnly Property Count() As Integer Get Dim element As Word = m_StartWord Do Until element.NextWord Is Nothing element = element.NextWord Count += 1 Loop End Get End Property Default Public ReadOnly Property Item(ByVal index As Integer) As String Get If index >= 0 AndAlso index < Count Then Dim elementIndex As Integer = -1 Dim element As Word = m_StartWord Do Until elementIndex = index OrElse element.NextWord Is Nothing elementIndex += 1 element = element.NextWord Loop If elementIndex = index Then Item = element.Value End If End Get End Property Public Sub Add(ByVal word As String) Dim lastWord As Word = m_StartWord Do Until lastWord.NextWord Is Nothing lastWord = lastWord.NextWord Loop lastWord.NextWord = New Word With {.Value = word} End Sub Public Function IndexOf(ByVal word As String) As Integer Dim elementIndex As Integer = -1 Dim element As Word = m_StartWord.NextWord Dim found As Boolean Do Until element Is Nothing OrElse found elementIndex += 1 found = (element.Value = word) element = element.NextWord Loop If found Then IndexOf = elementIndex Else IndexOf = -1 End If End Function Public Function Contains(ByVal word As String) As Boolean Contains = (IndexOf(word) <> -1) End Function End Class Class Word ' LinkedListNode Type Private m_Value As String Public Property Value() As String Get Value = m_Value End Get Set(ByVal value As String) m_Value = value End Set End Property Private m_NextWord As Word Public Property NextWord() As Word Get NextWord = m_NextWord End Get Set(ByVal value As Word) m_NextWord = value End Set End Property End Class End Namespace 'Bezoek www.vbvoorbeelden.be voor meer Visual Basic voorbeelden. 'Copyright - De Wolf / vbvoorbeelden - 2003-2011 - Alle rechten voorbehouden.