问题描述
首先,我知道这是一个非常常见的错误!我之前已经处理并修复了此错误消息,我理解它的含义,但是在这种情况下,我无法弄清楚我做错了什么.我确实努力首先搜索类似的答案.下面我将非常具体地描述我的问题,并认真努力理解这里涉及的概念.
First, I know this is a very common error! I have dealt with and fixed this error message before, and I understand what it means, however in this case I cannot figure out what I am doing wrong. I did make an effort to search for similar answers first. I will very specifically describe my problem below, and make an earnest effort to understand the concepts involved herein.
这是我的 MainForm.vb,运行 Windows 窗体应用程序时弹出的窗体:
Here is my MainForm.vb, the Form which pops up when running the Windows Forms app:
Public Class MainForm
Inherits System.Windows.Forms.Form
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim f2 As New Introduction
MainPanel.Controls.Add(f2)
f2.Show()
End Sub
' This is used for keeping track of forward/backward data:
Public Property FormDataCollection As DataSet
Get
Return Me.FormData.Copy '<-- Issue
End Get
Set(value As DataSet)
Me.FormData = value
End Set
End Property
Public FormData As DataSet
End Class
如您所见,我创建了一个 DataSet 集合作为应用程序主窗体的属性.稍后我将访问此属性以向数据集中添加/删除表以实现前进/后退按钮以及跟踪自动填充信息.但是,当我稍后尝试访问该属性时遇到了问题:
As you can see, I have created a DataSet collection as a property of the main Form of the application. I will access this property later on to Add/Remove tables to the DataSet to implement Forward/Back buttons as well as keeping track of AutoFill information. However, I run into a problem when I try to access the property later on:
Public Class Introduction
Inherits System.Windows.Forms.UserControl
Private Sub Introduction_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
End Sub
Private Sub AddOrRemoveButton_Click(sender As System.Object, e As System.EventArgs) Handles AddOrRemoveButton.Click
Dim AddRemoveCompanyControl = New AddRemoveCompany
MainForm.MainPanel.Controls.Clear()
MainForm.MainPanel.Controls.Add(AddRemoveCompanyControl)
AddRemoveCompanyControl.Show()
Dim AddRemoveCompany As DataTable = New DataTable("AddRemoveCompany")
MainForm.FormDataCollection.Tables.Add(AddRemoveCompany) 'ERROR T-Minus 10, 9, 8...
End Sub
End Class
当我尝试添加到数据集时,出现错误.我不确定这是怎么回事,因为我清楚地在属性的 Get 部分创建了 DataSet 的复制实例.
When I try to do a Add to the DataSet, I get the error. I am not sure how this can be, as I am clearly creating a copied instance of the DataSet in my Get part of the property.
推荐答案
您的集合未初始化:
Public FormData As New DataSet
该字段可能应该是私有的,因为您是通过公共属性访问它的.
That field should probably be private since you are accessing it through a public property.
这篇关于Windows 窗体:访问窗体的属性时出现对象引用未设置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!