Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 自动实现的属性导致空引用,而完整写入的属性不会导致空引用_Vb.net_Properties_Nullreferenceexception - Fatal编程技术网

Vb.net 自动实现的属性导致空引用,而完整写入的属性不会导致空引用

Vb.net 自动实现的属性导致空引用,而完整写入的属性不会导致空引用,vb.net,properties,nullreferenceexception,Vb.net,Properties,Nullreferenceexception,谁能告诉我为什么要这样做 Module Module1 Sub Main() Dim currentSourceData As New SourceData currentSourceData.datafiles.Add("234") End Sub End Module 给我 System.NullReferenceException:“对象引用未设置为对象的实例。” 当我使用自动实现的PRPoerty时 Public Class Source

谁能告诉我为什么要这样做

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

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

当我使用自动实现的PRPoerty时

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
此处缺少操作员:

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

由于新的差异,它确保了支持变量不是空的。您需要一个构造函数子New来处理自动实现的属性。拥有集合类型的可设置属性几乎总是一个可疑的设计。您真的希望此类用户能够使用自己选择的列表替换现有列表吗?重复的,在C中,但在本例中无关紧要,并解释了原因。在这里,SO也将提供帮助。