形式之间传递对象形式、对象

2023-09-05 03:30:05 作者:帝王念

这么多,都写了这个话题,但我的问题是不是我可以在previous线程发现一个有点不同。我可以通过一个对象到第二个形式,但它不是在第二形式的所有字幕可用。我需要它。在FRM2作品的第一个code座和动物名表演,但第二个code座(cmdSave)不工作 - 它说,动物没有宣布。谁能告诉我什么,我需要改变,使下面工作的最后code座(cmdSave)?

表格1 code:

 暗淡FRM2作为新FRM2(动物)
    Frm2.Show()
    Me.Close()
 

FRM2 code:

 的Public Sub New(BYVAL动物为对象)
    的InitializeComponent()
    Msgbox.show(animal.animalName)
完子

私人小组cmdSave
    Msgbox.show(animal.animalName)
完子
 

解决方案

我想你只需要在 FRM2 A(私有)成员牵着你通过动物对象,以便您能够访问它 cmdSave

java设计模式值传输对象模式

请参阅下面的code,其中 m_Animal 是私有成员。

FRM2 code:的

 公共类窗体2
    ...
    私人m_Animal为对象

    公共子新(BYVAL动物为对象)
        的InitializeComponent()
        m_Animal =动物
        Msgbox.show(animal.animalName)
    完子

    私人小组cmdSave
        Msgbox.show(m_Animal.animalName)
    完子

...
末级
 

So much as been written about this topic but my problem is a little different than what I can find in the previous threads. I can pass an object over to a second form but it is not available in all subs of the second form. I need it to be. The first code block in frm2 works and the animal name shows but the second code block (cmdSave) does not work - it says animal not declared. Can someone tell me what I need to change to make the last code block (cmdSave) below work?

Form 1 code:

    Dim frm2 As New frm2(animal)
    Frm2.Show()
    Me.Close()

frm2 code:

Public Sub New(ByVal animal As Object)
    InitializeComponent()
    Msgbox.show(animal.animalName)
End sub

Private sub cmdSave
    Msgbox.show(animal.animalName)
End sub

解决方案

I think you just need a (private) member in frm2 holding your passed animal object so that you are able to access it in cmdSave.

See the following code, where m_Animal is the private member.

frm2 code:

Public Class Form2
    ...
    Private m_Animal as Object

    Public Sub New(ByVal animal As Object)
        InitializeComponent()
        m_Animal = animal
        Msgbox.show(animal.animalName)
    End sub

    Private sub cmdSave
        Msgbox.show(m_Animal.animalName)
    End sub

...
End Class