vb,net 我的类中的集合属性抛出错误

如何解决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 时所做的那样 - 正则表达式 Matches 的集合立>
  • 明确财产是否为公共、私人等

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 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res