.NET 集合继承“未设置对象引用...”错误
我正在构造一个对象集合,并且需要在基类上使用继承 - 我已经以各种方式尝试了几个小时,但无法使其正常工作 - 错误始终是“对象引用未设置为实例”一个物体。”
下面示例中的想法是实例化一个对象,其中包含一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您从未初始化 p_Students 字段,它是 Nothing 并导致异常。修复:
请注意New关键字。这是快速解决方案,更明智的方法是:
You never initialized the p_Students field, it is Nothing and causes the exception. Fix:
Note the New keyword. That's the quick fix, a more sane approach would be:
您收到错误消息是因为您从未在
Students
类中初始化p_Students
。每次访问Student()
属性时,您还会不断地将相同的名称重新添加到 p_Students 数组中。您可能希望初始化数组并在构造函数中仅添加一次测试名称。You're getting the error message because you never initialise
p_Students
in the classStudents
. You also keep re-adding the same names to the p_Students array each time you access theStudent()
property. You might want to initialise the array and add the test names just once in a constructor.