VimpMedlem sedan juli 20022 537 inlägg Jag sitter och experimenterar lite med en collection.
För tillfället verkar koden inte göra ett dugg, men det kanske den inte ska? ;)
Först ser klassen ut så här:
Public Class Collection(Of T)
Implements System.Collections.Generic.IList(Of T)
Public Sub Add(ByVal item As T) Implements System.Collections.Generic.ICollection(Of T).Add
End Sub
Public Sub Clear() Implements System.Collections.Generic.ICollection(Of T).Clear
End Sub
Public Function Contains(ByVal item As T) As Boolean Implements System.Collections.Generic.ICollection(Of T).Contains
End Function
Public Sub CopyTo(ByVal array() As T, ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of T).CopyTo
End Sub
Public ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of T).Count
Get
End Get
End Property
Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.Generic.ICollection(Of T).IsReadOnly
Get
End Get
End Property
Public Function Remove(ByVal item As T) As Boolean Implements System.Collections.Generic.ICollection(Of T).Remove
End Function
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
End Function
Public Function IndexOf(ByVal item As T) As Integer Implements System.Collections.Generic.IList(Of T).IndexOf
End Function
Public Sub Insert(ByVal index As Integer, ByVal item As T) Implements System.Collections.Generic.IList(Of T).Insert
End Sub
Default Public Property Item(ByVal index As Integer) As T Implements System.Collections.Generic.IList(Of T).Item
Get
End Get
Set(ByVal value As T)
End Set
End Property
Public Sub RemoveAt(ByVal index As Integer) Implements System.Collections.Generic.IList(Of T).RemoveAt
End Sub
End Class
Sen ser koden för consolen ut så här:
Module Module1
Sub Main()
Dim MyCollection As New Vomber.Collections.Collection(Of String)
Console.BackgroundColor = ConsoleColor.White
Console.ForegroundColor = ConsoleColor.Black
Console.WriteLine("Test av collection")
Console.ResetColor()
MyCollection.Add("Testar")
MyCollection.Add("lalala")
For i As Integer = 0 To 1
Console.WriteLine("{0}: {1}", i, MyCollection.Item(i))
Next
Console.Read()
End Sub
End Module
När konsolen körs kommer detta fram:
Test av collection
0:
1:
Någon som har tips på hur jag ska kunna lägga till data i min collection samt hämta den?
PDahlenMedlem sedan apr. 2004778 inlägg Eftersom du inte har någon kod i dina subbar så gör klassen ingenting. Du måste lägga till kod.
Sen rekommenderar jag att du gör en Inherit CollectionBase istället. Mycket enklare.
I min blogg har jag lite inlägg om collections. Där kan du hitta kod att lägga i dina subbar och funktioner.
VimpMedlem sedan juli 20022 537 inlägg Fungerar bra, tack för hjälpen.
"Färdig" kod:
Public Class Collection(Of T)
Implements Generic.ICollection(Of T), Generic.IEnumerator(Of T)
Private _MyArrayList As ArrayList
Private _index As Integer = -1
Private disposed As Boolean = False
Public Sub New()
_MyArrayList = New ArrayList
End Sub
Public Sub New(ByVal theArrayList As ArrayList)
_MyArrayList = theArrayList
End Sub
Public Sub Add(ByVal item As T) Implements System.Collections.Generic.ICollection(Of T).Add
_MyArrayList.Add(item)
End Sub
Public Sub Clear() Implements System.Collections.Generic.ICollection(Of T).Clear
_MyArrayList.Clear()
End Sub
Public Function Contains(ByVal item As T) As Boolean Implements System.Collections.Generic.ICollection(Of T).Contains
Return _MyArrayList.Contains(item)
End Function
Public Sub CopyTo(ByVal array() As T, ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of T).CopyTo
_MyArrayList.CopyTo(array, arrayIndex)
End Sub
Public ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of T).Count
Get
Return _MyArrayList.Count
End Get
End Property
Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.Generic.ICollection(Of T).IsReadOnly
Get
Return _MyArrayList.IsReadOnly()
End Get
End Property
Public Function Remove(ByVal item As T) As Boolean Implements System.Collections.Generic.ICollection(Of T).Remove
_MyArrayList.Remove(item)
End Function
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return _MyArrayList(0)
End Function
Public ReadOnly Property Current() As T Implements System.Collections.Generic.IEnumerator(Of T).Current
Get
Return _MyArrayList(0)
End Get
End Property
Public Function MoveNext() As Boolean Implements System.Collections.Generic.IEnumerator(Of T).MoveNext
_index += 1
Return _index < _MyArrayList.Count
End Function
Private Overloads Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposed Then
If disposing Then
Dispose()
End If
End If
disposed = True
End Sub
#Region " IDisposable Support "
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
#End Region
End Class
PDahlenMedlem sedan apr. 2004778 inlägg Där satt den. ;)
Men som jag sa, du kan ärva CollectionBase istället.
VimpMedlem sedan juli 20022 537 inlägg CollectionBase fanns inte med i System.Collections.Generic så det blev så här istället.
Tänkte mest göra den för att lära mig mer om generics :)
PaceMedlem sedan juni 20019 024 inlägg Förmodar att de är Whidbey du skriver i, så jag undrar vad (Of T) betyder?
VimpMedlem sedan juli 20022 537 inlägg
Pace skrev:
Förmodar att de är Whidbey du skriver i, så jag undrar vad (Of T) betyder?
I Visual Basic skriver man (Of T) och i C# <T>.
När man skapar en instans av class som använder sig av generics skriver man t.ex.:
Dim MyClass As GenericClass(Of Integer)
Då blir (Of T) automatiskt en integer vilket betyder att maan slipper typecasta.
Jag skrev lite om det förut:
http://vomber.europe.webmatrixhosting.net/articles/187.aspx
PaceMedlem sedan juni 20019 024 inlägg Oki, tack för förklaringen! :)