如果不是,则返回null

时间:2015-02-08 23:59:46

标签: vb.net class properties null structure

我基本上有两种形式,一种名为'frmSettings',另一种名称'frmXLExternalFile','frmXLExternalFile'是从'frmSettings'创建的,在这两种形式之间传递数据,当我使用属性返回它时,它返回为null。香港专业教育学院尝试通过将其设置为公开来返回它,但仍然似乎没有一些奇怪的原因。我设置了断点并跟踪变量(实际上是一个结构),它肯定不是'null'

frmXLExternalFile

    Dim XL_File As frmMain.XLSheetData
Public ReadOnly Property XLFile As frmMain.XLSheetData
        Get
            Return XL_File
        End Get
    End Property

Private Sub frmXLExternalFile_formClosing(sender As Object, e As EventArgs) Handles MyBase.FormClosing
            If txtFilepath.Text <> "" And cmboName.Text <> "" Then
                XL_File = New frmMain.XLSheetData
                XL_File.name = cmboName.Text
                XL_File.filePath = txtFilepath.Text
                frmMain.settings.setXLFile()
                frmMain.settings.cmboXLSheets.Text = txtFilepath.Text
            End If
            frmMain.settings.Enabled = True
        End Sub

frmMain(这是声明结构的地方)

Public Structure XLSheetData
        Dim name As String
        Dim filePath As String
    End Structure

frmSettings

Dim XL_FileList As List(Of frmMain.XLSheetData)

    Sub setXLFile()
        Dim file As frmMain.XLSheetData = frmXLExternalFile.XLFile
        XL_FileList.Add(file)
        cmboXLSheets.Items.Add(file.filePath)
    End Sub

基本上,一旦字段--XL_File - 被填充,顶部表单将这称为底部方法,然后使用属性 - 'XLFile' - 来'获取'对象并将其放在'frmSettings'类中。正如我所说,我已经尝试将'XL_File'设置为public并尝试直接访问它,但抛出相同的异常。它为null,用于填充对象的组合框和文本框不为null。任何帮助,将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

  1. 通过传递“所有者”表单,这是使用Show()执行此操作的一种方法:
  2. 在frmSettings中:

    Dim frmX As New frmXLExternalFile
    ' ... pass some data to frmX ...
    frmX.Show(Me) ' <-- pass this form in as the "Owner"
    

    在frmXLExternalFile中:

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        If txtFilepath.Text <> "" And cmboName.Text <> "" Then
            XL_File = New frmMain.XLSheetData
            XL_File.name = cmboName.Text
            XL_File.filePath = txtFilepath.Text
    
            Dim main As frmSettings = DirectCast(Me.Owner, frmSettings) ' <-- use the instance passed in as the owner via Show()
            ' ... use "main" somehow ...
            main.settings.setXLFile()
            main.settings.cmboXLSheets.Text = txtFilepath.Text
            main.settings.Enabled = True
        Else
            ' ... possibly do something else ...
        End If
    End Sub
    
    1. 此示例演示了ShowDialog()方法:
    2. 在frmSettings中:

      Dim frmX As New frmXLExternalFile
      ' ... pass some data to frmX ...
      If frmX.ShowDialog() = Windows.Forms.DialogResult.OK Then ' <-- execution in this Form STOPS until "frmX" is dismissed
          ' Retrieve the value from out "frmX" instance:
          Dim file As frmMain.XLSheetData = frmX.XLFile
      
          ' ... do something with "file" ...
          XL_FileList.Add(file)
          cmboXLSheets.Items.Add(file.filePath)
          cmboXLSheets.Text = file.filePath
      End If
      

      在frmXLExternalFile中:

      Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
          If txtFilepath.Text <> "" And cmboName.Text <> "" Then
              XL_File = New frmMain.XLSheetData
              XL_File.name = cmboName.Text
              XL_File.filePath = txtFilepath.Text
      
              ' Set DialogResult, returning execution to the ShowDialog() line:
              Me.DialogResult = Windows.Forms.DialogResult.OK
          Else
              ' ... possibly do something else ...
          End If
      End Sub
      
相关问题