我正在使用 VS 2019 vb.net 實作創建用于傳輸某些資料的 XML 檔案的程序。為此,我按照 Microsoft 的建議構建了一系列類。該程序可以正確編譯,但在運行時會生成一個錯誤,例如“未在物件實體上設定物件參考”。到線
FileAbaco.utenti(0).utente(0).strutture(0).struttura.Add(stra)
我不明白我錯在哪里。提前感謝您提供的任何幫助。
Dim FileAbaco As New dichiarazioni
FileAbaco.comune = "I608"
Dim uti As New utenti
FileAbaco.utenti.Add(uti)
Dim ute As New utente
FileAbaco.utenti(0).utente.Add(ute)
Dim stre As New strutture
FileAbaco.utenti(0).utente(0).strutture.Add(stre)
Dim stra As New struttura
FileAbaco.utenti(0).utente(0).strutture(0).struttura.Add(stra)
Dim dr As DataRow
dr = DTTrimestrale.Rows(0)
With FileAbaco.utenti(0).utente(0)
.auth = dr.Item("auth".ToString)
.gest_codice_fiscale = dr.Item("gest_codice_fiscale")
.codice_utente = dr.Item("codice_utente")
.gest_cognome = dr.Item("gest_cognome")
.gest_nome = ""
End With
這些是類:
Imports System.Xml.Serialization
Public Class dichiarazioni
Public comune As String
<XmlElement()>
Public Property utenti As New List(Of utenti)
End Class
Public Class utenti
<XmlElement()>
Public Property utente As New List(Of utente)
End Class
Public Class utente
Public Property auth As String
Public Property gest_codice_fiscale As String
Public Property codice_utente As String
Public Property gest_cognome As String
Public Property gest_nome As String
Public Property strutture As New List(Of strutture)
Public Sub New()
End Sub
Public Sub New(auth As String, gest_codice_fiscale As String, codice_utente As String, gest_cognome As String, gest_nome As String, strutture As strutture)
Me.auth = auth
Me.gest_codice_fiscale = gest_codice_fiscale
Me.codice_utente = codice_utente
Me.gest_cognome = gest_cognome
Me.gest_nome = gest_nome
Me.strutture.Add(strutture)
End Sub
End Class
Public Class strutture
<XmlElement()>
Public Property struttura As List(Of struttura)
Public Sub New()
End Sub
Public Sub New(struttura As struttura)
Me.struttura.Add(struttura)
End Sub
End Class
Public Class struttura
<XmlElement()>
Public Property ID_struttura As String
Public Property insegna As String
Public Sub New()
End Sub
Public Sub New(ID_struttura As String, insegna As String)
Me.ID_struttura = ID_struttura
Me.insegna = insegna
End Sub
End Class
uj5u.com熱心網友回復:
顯然這是正確的,因為您必須struttura像往常一樣初始化Nothing。
Cambia questa classe come segue:
Public Class strutture
<XmlElement()>
Public Property struttura As List(Of struttura)
Public Sub New()
If Me.struttura Is Nothing Then Me.struttura = New List(Of struttura)
End Sub
Public Sub New(struttura As struttura)
If Me.struttura Is Nothing Then Me.struttura = New List(Of struttura)
Me.struttura.Add(struttura)
End Sub
End Class
否則:Altrimenti fai come sopra (nella classe che prior)
Public Property whatEver As New List(Of Whatever)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473611.html
上一篇:將資料網格中的值決議為按鈕名稱
