VB.net访问另一个类的类属性

VB.net访问另一个类的类属性,vb.net,Vb.net,我需要一些帮助来了解实例和类。在下面的代码中,我有一个main_表单,我还将一个user_控件加载到main_表单中。我在main_表单中有一个属性,我将设置名为obj的数据。我的问题是,当我在user_控件内工作时,如何进入并设置main_窗体的obj属性。我尝试了main_form.obj,但一直出现错误“对象引用未设置为对象的实例”。所以,我不知道怎么做。这是简化的代码 Public Class FormControlClass Private _obj As New objCollect

我需要一些帮助来了解实例和类。在下面的代码中,我有一个main_表单,我还将一个user_控件加载到main_表单中。我在main_表单中有一个属性,我将设置名为obj的数据。我的问题是,当我在user_控件内工作时,如何进入并设置main_窗体的obj属性。我尝试了main_form.obj,但一直出现错误“对象引用未设置为对象的实例”。所以,我不知道怎么做。这是简化的代码

Public Class FormControlClass
Private _obj As New objCollection

Public Property obj As objCollection
    Get
        Return _obj
    End Get
    Set(ByVal value As objCollection)
        _obj = value
    End Set
End Property

'Load User Control Into Form from here.
me.controls.add('UserControl')

End Class


Public Class UserControlClass

'Access the obj property in the form control class from here.
FormControlClass.obj = 1

End Class

即使您可以做您正试图做的事情,这也不是一个好主意,因为您将把您的用户控件耦合到这个特定的表单,使得它在这个表单之外毫无用处

相反,您需要让您的用户控件生成一个表单可以订阅并自行处理的事件。换句话说,您希望让用户控件创建一条可以传递到表单的消息,如下所示:

Public Class UserControlClass
    ' Define event that will be raised by user control to anyone interested in handling the event
    Public Event UC_Button1Click()

    ' Mechanism to allow event to be raised by user control
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent UC_Button1Click()
    End Sub
End Class
AddHandler userControl1.UC_Button1Click, AddressOf Button1_Click
Public Sub Button1_Click(ByVal sender As Object, ByVal args As EventArgs)
    ' Do something here
End Sub
现在,在form类中,需要为用户控件引发的事件添加一个处理程序,如下所示:

Public Class UserControlClass
    ' Define event that will be raised by user control to anyone interested in handling the event
    Public Event UC_Button1Click()

    ' Mechanism to allow event to be raised by user control
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent UC_Button1Click()
    End Sub
End Class
AddHandler userControl1.UC_Button1Click, AddressOf Button1_Click
Public Sub Button1_Click(ByVal sender As Object, ByVal args As EventArgs)
    ' Do something here
End Sub
最后,您将创建
AddressOf
语法中引用的方法,如下所示:

Public Class UserControlClass
    ' Define event that will be raised by user control to anyone interested in handling the event
    Public Event UC_Button1Click()

    ' Mechanism to allow event to be raised by user control
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent UC_Button1Click()
    End Sub
End Class
AddHandler userControl1.UC_Button1Click, AddressOf Button1_Click
Public Sub Button1_Click(ByVal sender As Object, ByVal args As EventArgs)
    ' Do something here
End Sub

好的,我有那部分。现在,当我在我的用户控制中时。如何从表单控件类检索属性?如果我使用
FormControlClass.MyProperty
它会将未设置的对象抛出到对象的实例中,我认为您的意思是
userControl1.obj
,因为您必须通过用户控件的实例引用用户控件的属性,我认为您正在努力解决用户控件存在于页面(表单)内部的概念,但它们并不是为了接触父控件并控制其属性,因为这使得它们无法在特定页面的上下文之外重用。您希望您的用户控件在页面(表单)中更改/更新什么?如果您想要这种行为,那么只需在页面的用户控件部分创建内容。当我在用户控件中时,如何读取
FormControlClass.obj
。我的理论是表单控件将保存数据,但我的用户控件将处理这些数据。在用户控件中创建一个或多个属性,并在表单类中设置这些属性,然后创建用户控件并将其添加到页面集合中。页面(表单)告诉用户控件要使用哪些数据,并且当某个事件发生时,用户控件会向页面发出警报。