VB.Net Tutorial/Collections/Array Object — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:40, 26 мая 2010
Object array
Option Strict On
Imports System
MustInherit Public Class Control
Public Sub New(top As Integer, left As Integer)
Me.top = top
Me.left = left
End Sub
Public MustOverride Sub DrawControl( )
Protected top As Integer
Protected left As Integer
End Class
Public Class Label
Inherits Control
Public Sub New(top As Integer, left As Integer, contents As String)
MyBase.New(top, left)
listBoxContents = contents
End Sub
Public Overrides Sub DrawControl( )
Console.WriteLine("Writing string to the listbox: {0}", listBoxContents)
End Sub
Private listBoxContents As String
End Class
Public Class Button
Inherits Control
Public Sub New(top As Integer, left As Integer)
MyBase.New(top, left)
End Sub
Public Overrides Sub DrawControl( )
Console.WriteLine("Drawing a button at {0}, {1}" + ControlChars.Lf, top, left)
End Sub
End Class
Public Class Tester
Shared Sub Main( )
Dim winArray(3) As Control
winArray(0) = New Label(1, 2, "A")
winArray(1) = New Label(3, 4, "B")
winArray(2) = New Button(5, 6)
Dim i As Integer
For i = 0 To 2
winArray(i).DrawControl( )
Next i
End Sub "Main
End Class "Tester
Writing string to the listbox: A Writing string to the listbox: B Drawing a button at 5, 6
Populate Object array
Option Strict On
Imports System
Public Class Employee
Private empID As Integer
Public Sub New(ByVal empID As Integer)
Me.empID = empID
End Sub
End Class
Class Tester
Shared Sub Main()
Dim intArray As Integer()
Dim empArray As Employee()
intArray = New Integer(5) {}
empArray = New Employee(3) {}
Dim i As Integer
For i = 0 To empArray.Length - 1
empArray(i) = New Employee(i + 5)
i = i + 1
Next
End Sub
End Class
Sort a custom object array
Public Class Tester
Public Shared Sub Main
Dim arrayToSort() As Employee = { _
New Employee("F", "O"), _
New Employee("V", "O"), _
New Employee("F", "A"), _
New Employee("V", "C"), _
New Employee("F", "G")}
Array.Sort(arrayToSort)
For Each food As Employee In arrayToSort
Console.WriteLine(food.ToString())
Next food
End Sub
End Class
Public Class Employee
Implements IComparable
Public FirstName As String
Public LastName As String
Public Sub New(ByVal theGroup As String, ByVal theItem As String)
FirstName = theGroup
LastName = theItem
End Sub
Public Overrides Function ToString() As String
Return FirstName & ": " & LastName
End Function
Public Function CompareTo(ByVal obj As Object) As Integer _
Implements System.IComparable.rupareTo
Dim compareValue As String
compareValue = obj.ToString()
Return String.rupare(Me.ToString(), compareValue)
End Function
End Class
F: A F: G F: O V: C V: O