自动实现的属性导致空引用,而完整的书面属性不

时间:2019-01-24 09:46:00

标签: vb.net properties nullreferenceexception

谁能告诉我为什么要执行此操作

Module Module1
    Sub Main()
        Dim currentSourceData As New SourceData
        currentSourceData.datafiles.Add("234")
    End Sub
End Module

给我

  

System.NullReferenceException:'对象引用未设置为对象的实例。'

当我使用自动实施的功能

Public Class SourceData
    Public Property datafiles() As List(Of String)
End Class

但是当我使用完全写好的属性时,却没有:

Public Class SourceData
    Private _datafiles As New List(Of String)
    Public Property datafiles() As List(Of String)
        Get 
           Return _datafiles
        End Get
        Set(value As List(Of String))
            _datafiles = value
        End Set
    End Property
End Class

1 个答案:

答案 0 :(得分:2)

New运算符在此处缺失:

Public Class SourceData
    Public Property datafiles() As List(Of String)
End Class