.NET 集合继承“未设置对象引用...”错误

发布于 2024-10-01 04:42:56 字数 1372 浏览 3 评论 0原文

我正在构造一个对象集合,并且需要在基类上使用继承 - 我已经以各种方式尝试了几个小时,但无法使其正常工作 - 错误始终是“对象引用未设置为实例”一个物体。”

下面示例中的想法是实例化一个对象,其中包含一个 Students 集合,当我尝试访问 oDemo.Students(0).Name 属性时,会引发错误。否则,该对象似乎已实例化正常。

下面是用于实例化和访问该类的代码(来自 ASP.NET)。

Dim oDemo As New MyDemo.Students
lblTest.Text = oDemo.Student(0).Name

这是类库中的示例代码:

Public Class Students
    Private p_Students As System.Collections.Generic.List(Of Test)
    ReadOnly Property Student() As System.Collections.Generic.List(Of Test)
        Get
            Dim alTestNames As New ArrayList()
            alTestNames.Add("John")
            alTestNames.Add("Brian")
            alTestNames.Add("Scott")
            For iLoop As Integer = 0 To alTestNames.Count - 1
                p_Students.Add(New Test(alTestNames(iLoop).ToString))
            Next
            Return p_Students
        End Get
    End Property
End Class

Public MustInherit Class TestBase
    Private p_Name As String
    ReadOnly Property Name() As String
        Get
            Return p_Name
        End Get
    End Property
    Sub New(ByRef sName As String)
        p_Name = sName
    End Sub
End Class

Public Class Test
    Inherits TestBase
    Sub New(ByRef sName As String)
        MyBase.New(sName)
    End Sub
End Class

我不确定我是否在某种程度上完全错过了要点 - 非常感谢您提供任何帮助。如果需要更多信息/背景,请告知。

I am constructing a collection of objects, and need to use inheritance on a base class - I have been trying for hours in various ways and can't get this to work whatsoever - the error is always "Object reference not set to an instance of an object."

The idea in the example below is that an object is instantiated, and within that lies a collection of Students when I try to access the oDemo.Students(0).Name property the error is raised. Otherwise the object appears to have instantiated OK.

Here is the code being used to instantiate and access the class (from ASP.NET).

Dim oDemo As New MyDemo.Students
lblTest.Text = oDemo.Student(0).Name

Here is the sample code from the class library:

Public Class Students
    Private p_Students As System.Collections.Generic.List(Of Test)
    ReadOnly Property Student() As System.Collections.Generic.List(Of Test)
        Get
            Dim alTestNames As New ArrayList()
            alTestNames.Add("John")
            alTestNames.Add("Brian")
            alTestNames.Add("Scott")
            For iLoop As Integer = 0 To alTestNames.Count - 1
                p_Students.Add(New Test(alTestNames(iLoop).ToString))
            Next
            Return p_Students
        End Get
    End Property
End Class

Public MustInherit Class TestBase
    Private p_Name As String
    ReadOnly Property Name() As String
        Get
            Return p_Name
        End Get
    End Property
    Sub New(ByRef sName As String)
        p_Name = sName
    End Sub
End Class

Public Class Test
    Inherits TestBase
    Sub New(ByRef sName As String)
        MyBase.New(sName)
    End Sub
End Class

I'm not sure if I am totally missing the point in some way - any help is gratefully received. If any more info/background is needed please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-10-08 04:42:56

您从未初始化 p_Students 字段,它是 Nothing 并导致异常。修复:

Private p_Students As New System.Collections.Generic.List(Of Test)

请注意New关键字。这是快速解决方案,更明智的方法是:

Public Class Students
    Private p_Students As System.Collections.Generic.List(Of Test)
    Public Sub New()
        p_Students = New System.Collections.Generic.List(Of Test)
        p_Students.Add(New Test("John"))
        '' etc..
    End Sub
    ReadOnly Property Student() As System.Collections.Generic.IList(Of Test)
        Get
            Return p_Students
        End Get
    End Property
End Class

You never initialized the p_Students field, it is Nothing and causes the exception. Fix:

Private p_Students As New System.Collections.Generic.List(Of Test)

Note the New keyword. That's the quick fix, a more sane approach would be:

Public Class Students
    Private p_Students As System.Collections.Generic.List(Of Test)
    Public Sub New()
        p_Students = New System.Collections.Generic.List(Of Test)
        p_Students.Add(New Test("John"))
        '' etc..
    End Sub
    ReadOnly Property Student() As System.Collections.Generic.IList(Of Test)
        Get
            Return p_Students
        End Get
    End Property
End Class
撧情箌佬 2024-10-08 04:42:56

您收到错误消息是因为您从未在 Students 类中初始化 p_Students。每次访问 Student() 属性时,您还会不断地将相同的名称重新添加到 p_Students 数组中。您可能希望初始化数组并在构造函数中仅添加一次测试名称。

You're getting the error message because you never initialise p_Students in the class Students. You also keep re-adding the same names to the p_Students array each time you access the Student() property. You might want to initialise the array and add the test names just once in a constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文