方法如下:
首先创建你的类
班级员工
公共名称作为字符串
……
结束课
学习使用List(t)如何使用以及如何使用列表对自己的数据进行排序(您应该实现iComparable接口或在排序和查找时传递您的Icompare)或自己编写这些函数
首先使用虚拟数据,看看一切是否如你所愿
然后最后保存和恢复您的数据。
你有很多选择
使用 sql
使用简单的 xml
等等
我建议你先使用xml。读取xml并编写它。或者您可以直接将您的数据序列化为 xml 文件并在恢复时对其进行反序列化
.
这是我在搜索时发现的。它使用 Linq 和 xml 序列化程序。顺便说一句,该解决方案与您的解决方案相同。
http://www.codeproject.com/Articles/26652/Using-LINQ-to-Objects-in-Visual-Basic
这是我的简单解决方案。我用Linq 和没有Linq 编写了它的排序和查找两种方式。您将需要存储数据。为此,请检查上面的链接或来自 msdn msdn serialize example
的链接
Imports System.Linq
Module Module1
Sub Main()
Dim employee_list As New List(Of Employee)
employee_list.Add(New Employee("Abdurrauf", 1, 1, "programmer"))
employee_list.Add(New Employee("John", 5, 2, "programmer"))
employee_list.Add(New Employee("Teylor", 10, 3, "programmer"))
employee_list.Add(New Employee("John", 9, 4, "student"))
employee_list.Add(New Employee("Jorj", 6, 5, "programmer"))
employee_list.Add(New Employee("Samir", 1, 6, "programmer"))
employee_list.Add(New Employee("Orxan", 3, 7, "programmer"))
'sort by annual and display
employee_list.Sort(Employee.GetSorter(SortType.AnualDesc))
Console.WriteLine("Sorted by annual leave {descending}:")
For Each x As Employee In employee_list
Console.WriteLine(" Name : {0} Annual : {1} ", x.Name, x.Annual_leave)
Next
'SORTING WITH LINQ
'using LINQ (this time you dont need to write sort class and you can avoid using it)
Dim employeesByNameDesc = From x In employee_list _
Order By x.Name Descending _
Select x
Console.WriteLine("Sorted with using Linq by Name Descending")
For Each x As Employee In employeesByNameDesc
Console.WriteLine(" Name : {0} Annual : {1} ", x.Name, x.Annual_leave)
Next
'find by name without lambda
Dim em As Employee = findemp(employee_list ,"Samir")
If em IsNot Nothing Then
Console.WriteLine("found : emp Name {0} desc {1} ", em.Name, em.Description)
End If
'find by name with lambda
Dim emp As Employee = employee_list.Find(Function(x) (x.Name = "John"))
If emp IsNot Nothing Then
Console.WriteLine("found : emp Name {0} desc {1} ", emp.Name, emp.Description)
End If
Console.Read()
End Sub
Function findemp(emlist As List(Of Employee), name As String) As Employee
For Each x In emlist
If (x.Name = name) = True Then
Return x
End If
Next
Return Nothing
End Function
<Serializable()> _
Class Employee
Implements IComparable(Of Employee)
Private _Name As String
Private _Annual_leave As Integer
Private _Sick_leave As Integer
Private _Description As String
Sub New(name As String, ann As Integer, sl As Integer, desc As String)
With Me
._Name = name
.Annual_leave = ann
.Sick_leave = sl
.Description = desc
End With
End Sub
Property Name As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
Property Description As String
Get
Return _Description
End Get
Set(value As String)
_Description = value
End Set
End Property
Property Annual_leave As Integer
Get
Return _Annual_leave
End Get
Set(value As Integer)
_Annual_leave = value
End Set
End Property
Property Sick_leave As Integer
Get
Return _Sick_leave
End Get
Set(value As Integer)
_Sick_leave = value
End Set
End Property
'default compare
Public Overloads Function CompareTo(ByVal other As Employee) As Integer _
Implements IComparable(Of Employee).CompareTo
If other Is Nothing Then Return 1
Return Name.CompareTo(other.Name)
End Function
Public Shared Function GetSorter(sortType As SortType) As IComparer(Of Employee)
Return CType(New sortClass(sortType), IComparer(Of Employee))
End Function
End Class
''our comparer
Public Enum SortType
AnualAsc
AnualDesc
SickAsc
SickDesc
NameAsc
NameDesc
End Enum
Private Class sortClass
Implements IComparer(Of Employee)
Dim _type As SortType
Private _sortType As SortType
Sub New(sortType As SortType)
_sortType = sortType
End Sub
Private Function compareint(xx As Integer, yy As Integer) As Integer
If (xx < yy) = True Then
Return 1
ElseIf (xx > yy) = True Then
Return -1
Else
Return 0
End If
End Function
Public Overloads Function Compare(x As Employee, y As Employee) As Integer _
Implements IComparer(Of Employee).Compare
Dim res As Integer = 0
Select Case _type
Case SortType.NameAsc
res = String.Compare(x.Name, y.Name)
Case SortType.NameDesc
res = String.Compare(y.Name, x.Name)
Case SortType.AnualAsc
res = compareint(x.Annual_leave, y.Annual_leave)
Case SortType.AnualDesc
res = compareint(y.Annual_leave, x.Annual_leave)
Case SortType.SickAsc
res = compareint(x.Sick_leave, y.Sick_leave)
Case SortType.SickDesc
res = compareint(y.Sick_leave, x.Sick_leave)
Case Else
res = String.Compare(x.Name, y.Name)
End Select
Return res
End Function
End Class
End Module