如何解决vb,net 我的类中的集合属性抛出错误
我正在学习集合,我有一个 Person 类
Imports System
Imports System.Collections.Generic
Imports System.Text
Public Class Person
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer,ByVal first_name As String,ByVal mid_name As String,ByVal last_name As String,ByVal age As Short,ByVal sex As Char)
Me.p_id = id
Me.first_name = first_name
Me.mid_name = mid_name
Me.last_name = last_name
Me.p_age = age
Me.p_sex = sex
End Sub
Private p_id As Integer = -1
Private first_name As String = String.Empty
Private mid_name As String = String.Empty
Private last_name As String = String.Empty
Private p_age As Short = 0
Private p_sex As Nullable(Of Char) = Nothing
Public Property ID() As Integer
Get
Return p_id
End Get
Set(ByVal value As Integer)
p_id = value
End Set
End Property
Public Property FirstName() As String
Get
Return first_name
End Get
Set(ByVal value As String)
first_name = value
End Set
End Property
Public Property MiddleName() As String
Get
Return mid_name
End Get
Set(ByVal value As String)
mid_name = value
End Set
End Property
Public Property LastName() As String
Get
Return last_name
End Get
Set(ByVal value As String)
last_name = value
End Set
End Property
Public Property Age() As Short
Get
Return p_age
End Get
Set(ByVal value As Short)
p_age = value
End Set
End Property
Public Property Sex() As Nullable(Of Char)
Get
Return p_sex
End Get
Set(ByVal value As Nullable(Of Char))
p_sex = value
End Set
End Property
End Class
和我定义 Person 属性的 Employee 类
Imports System
Imports System.Collections.Generic
Imports System.Text
Public Class Employee
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer,ByVal companyName As String,ByVal office As String,colPerson As Person)
'Me._employeeid = id
'Me._companyName = companyName
'Me._office = office
End Sub
Private _employeeid As Integer = -1
Private _companyName As String = String.Empty
Private _office As String = String.Empty
Public Property Empoyee_ID() As Integer
Get
Return _employeeid
End Get
Set(ByVal value As Integer)
_employeeid = value
End Set
End Property
Public Property CompanyName() As String
Get
Return _companyName
End Get
Set(ByVal value As String)
_companyName = value
End Set
End Property
Public Property Office() As String
Get
Return _office
End Get
Set(ByVal value As String)
_office = value
End Set
End Property
Property colPerson As List(Of Person)
End Class
我怎样才能填充person类
Sub Main()
Dim pList As List(Of Person) = New List(Of Person)()
Dim thePerson As New List(Of Person) From
{
New Person With {.Age = 29,.FirstName = "John",.LastName = "Shields",.MiddleName = "",.Sex = "M",.ID = 1},New Person With {.Age = 34,.FirstName = "Mary",.LastName = "Matthew",.MiddleName = "L",.Sex = "F",.ID = 2},New Person With {.Age = 55,.FirstName = "Amber",.LastName = "Carl",.MiddleName = "P",.ID = 3},New Person With {.Age = 12,.FirstName = "Kathy",.LastName = "Berry",.MiddleName = "O",.ID = 4}
}
'pList.Add(New Person(1,"John","","Shields",29,"M"c))
'pList.Add(New Person(2,"Mary","Matthew","Jacobs",35,"F"c))
'pList.Add(New Person(3,"Amber","Carl","Agar",25,"M"c))
'pList.Add(New Person(4,"Kathy","Berry",21,"F"c))
'pList.Add(New Person(5,"Lena","Ashco","Bilton",33,"F"c))
'pList.Add(New Person(6,"Susanne","Buck",45,"F"c))
'pList.Add(New Person(7,"Jim","Brown",38,"M"c))
'pList.Add(New Person(8,"Jane","G","Hooks",32,"F"c))
'pList.Add(New Person(9,"Robert",31,"M"c))
'pList.Add(New Person(10,"Cindy","Preston","Fox","F"c))
'pList.Add(New Person(11,"Gina","Austin",27,"F"c))
'pList.Add(New Person(12,"Joel","David","Benson","M"c))
'pList.Add(New Person(13,"George","R","Douglas",55,"M"c))
'pList.Add(New Person(14,"Richard","Banks",22,"M"c))
'pList.Add(New Person(15,"C","Shaw",39,"F"c))
'
'loop through the list
' PrintOnConsole(pList,"1. --- Looping through all items in the List<T> ---")
'
'Filtering List(T) using a single condition - (Age > 35)
'Dim filterOne As List(Of Person) = pList.FindAll(Function(p As Person) p.Age > 35)
'PrintOnConsole(filterOne,"2. --- Filtering List<T> on single condition (Age > 35) ---")
''
'' Filtering List(T) on multiple conditions (Age > 35 and Sex is Female)
'Dim filterMultiple As List(Of Person) = pList.FindAll(Function(p As Person) p.Age > 35 AndAlso p.Sex = "F"c)
'PrintOnConsole(filterMultiple,"3. --- Filtering List<T> on multiple conditions (Age > 35 and Sex is Female) ---")
''
''Sorting List(T) (Sort on FirstName)
'Dim sortFName As List(Of Person) = pList
'sortFName.Sort(Function(p1 As Person,p2 As Person) p1.FirstName.CompareTo(p2.FirstName))
'PrintOnConsole(sortFName,"4. --- Sort List<T> (Sort on FirstName) ---")
'
'Sorting List(T) descending (Sort on LastName descending)
'Dim sortLNameDesc As List(Of Person) = pList
'sortLNameDesc.Sort(Function(p1 As Person,p2 As Person) p2.LastName.CompareTo(p1.LastName))
'PrintOnConsole(sortLNameDesc,"5. --- Sort List<T> descending (Sort on LastName descending) ---")
''Add new List(T) to existing List(T)
'Dim newList As List(Of Person) = New List(Of Person)()
'newList.Add(New Person(16,"Geoff","Fisher","M"c))
'newList.Add(New Person(17,"Samantha","Baxer","F"c))
'pList.AddRange(newList)
'PrintOnConsole(pList,"6. --- Add new List<T> to existing List<> ---")
''Remove multiple items from List(T) based on condition (remove male employees)
'Dim removeList As List(Of Person) = pList
'removeList.RemoveAll(Function(p As Person) p.Sex = "M"c)
'PrintOnConsole(removeList,"7. --- Remove multiple items from List<> based on condition ---")
'' Create Read Only List(T)
'Console.WriteLine("Create Read Only List<>")
'Dim personReadOnly As IList(Of Person) = pList
'Console.WriteLine("Before - Is List Read Only? True or False : " & personReadOnly.IsReadOnly)
'personReadOnly = pList.AsReadOnly()
'Console.WriteLine("After - Is List Read Only? True or False : " & personReadOnly.IsReadOnly & "</br>")
'
'Dim pList1 As New Person
Dim emp As New List(Of Employee)
Dim r As New Employee
r.CompanyName = "zac"
r.Office = "home"
r.Empoyee_ID = 1
'Dim pList1 = New Person(1,"M"c)
r.colPerson.Add(New Person(1,"M"c))---> Gives error
emp.Add(r)
'
Dim i As New Employee
i.CompanyName = "zac1"
i.Office = "home1"
i.Empoyee_ID = 2
i.colPerson.Add(New Person(3,"M"c))
pList.Add(New Person(4,"F"c))
emp.Add(i)
'
Dim t As New Employee
t.CompanyName = "zac2"
t.Office = "home2"
t.Empoyee_ID = 2
pList.Add(New Person(5,"F"c))
pList.Add(New Person(6,"F"c))
emp.Add(t)
For Each item In emp
'item.CompanyName = "zac"
'item.Office = "home"
'item.colperson.Where(Function(x) x.ID = 17)
'Console.WriteLine("employee with person collection: " & item.CompanyName & " " & item.Office & " " & item.colperson.Where(Function(x) x.ID = 17).ToString & "</br>")
Console.WriteLine("employee with person collection: " & item.CompanyName & " " & item.Office & "</br>")
Next
End Sub
解决方法
r.colPerson.Add(New Person(1,"John","","Shields",29,"M"c))---> Gives error
它给出了一个错误,因为即使 colPerson 被声明为能够容纳人员列表,它实际上还没有被设置为 成为 人员列表,所以它目前是 Nothing,并且你不能在没有任何东西的东西上调用方法
Property colPerson As New List(Of Person)
^^^
添加一个 New
指令以确保它被声明并初始化为一个 List 的实例
另外,请:
- 不要在名字中加上“col”
- 对
List(Of Thing)
使用复数名称 - 这是一个人的列表,因此它至少应该被称为 People,但也可能说明他们是什么类型的人。例如,如果此 Employee 是由几个人推荐的,则称其为 RcommendedByPeople - 如果您正在编写一个是集合的类,则仅在名称中使用 Collection,例如 Microsoft 在编写 MatchCollection 时所做的那样 - 正则表达式
Match
es 的集合立> - 明确财产是否为公共、私人等
即
Public Property RcommendedByPeople As New List(Of Person)
,
如何访问嵌入类中的数据我尝试了以下但它不喜欢它
For Each item In emp
For Each item.Persons
Console.WriteLine("employee with person collection: " & item.CompanyName & " " & item.Office & "</br>")
Console.ReadLine()
Next
Next
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。