如何使用vb.net访问不同子例程中的对象数组

时间:2016-01-13 10:25:07

标签: arrays vb.net

我正在构建一个前端,我必须接受来自excel表的数据,该表将由用户动态选择。我已将这些数据复制到arrayobject。

我希望能够跨所有子例程访问此数组对象。但问题是如果我在子程序B中更改数组的值,它仍然保留子程序A中的先前值。

请查看以下代码

Public SelectedFile As String   
Public excel As New Application  
Public workbook As Workbook  
Public sheet As Worksheet  
Public r As Range  
Public Shared array(,) As Object  
Public scan As Integer  


Private Sub SelectFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectFileButton.Click

        'display selected file in text box
        SelectedFile = ComboBox1.GetItemText(ComboBox1.SelectedItem)

        If SelectedFile <> Nothing Then

            SelectedFileTextBox.Text = SelectedFile

            Dim path As String
            path = ExcelFolder & "\" & SelectedFile

            excel = New Application
            excel.Workbooks.Open(Filename:=path, [ReadOnly]:=False)
            workbook = excel.ActiveWorkbook
            excel.UserControl = True
            sheet = excel.Worksheets(1)
            r = sheet.UsedRange

            ' Load all cells into 2d array.
            static array(,) = r.Value(XlRangeValueDataType.xlRangeValueDefault)

            ' Get bounds of the array.
            Dim bound0 As Integer = array.GetUpperBound(0) 'last row number
            Dim bound1 As Integer = array.GetUpperBound(1) 'last column number

           'get total number of rows
            Dim totalrows As Integer = bound0 - 1 'since 1st row is header
            TotalRowsTextBox.Text = CStr(totalrows)

            ' Get first job to be marked in the selected file

        For scan = 2 To bound0

                If Trim(array(scan, 12)) = Trim("NO") Then

                    markinglinenotextbox.Text = CStr(scan - 1)
                    ModelNoTextBox.Text = array(scan, 1)
                    SerialNoTextBox.Text = array(scan, 3)
                    MaktxTextBox.Text = array(scan, 4)
                    MatnrTextBox.Text = array(scan, 11)
                    BaseDrawingTextBox.Text = array(scan, 2)
                    MarkButton.Visible = True
                    NextJobButton.Visible = True
                    Exit For

                ElseIf array(scan, 12) = "YES" Then

                    scan = scan + 1

                End If
            Next

            'if all files are marked 
            If scan > bound0 Then

                markinglinenotextbox.Text = ""
                ModelNoTextBox.Text = ""
                SerialNoTextBox.Text = ""
                MaktxTextBox.Text = ""
                MatnrTextBox.Text = ""
                BaseDrawingTextBox.Text = ""
                MarkButton.Visible = False
                NextJobButton.Visible = False
                SelectedFileTextBox.Text = ""
                TotalRowsTextBox.Text = ""
                workbook.Close()
                excel.Quit()
                workbook = Nothing
                excel = Nothing
                sheet = Nothing
                Erase array

                MessageBox.Show("All Jobs in this File are already Marked. Choose another File.", "File Marked", MessageBoxButtons.OK)

            End If

        End If

    End Sub

Private Sub NextJobButton_Click(sender As Object, e As EventArgs) Handles NextJobButton.Click

        Static array(,) = r.Value(XlRangeValueDataType.xlRangeValueDefault)
        scan = scan + 1

        If scan > lastrow Then

            markinglinenotextbox.Text = ""
            ModelNoTextBox.Text = ""
            SerialNoTextBox.Text = ""
            MaktxTextBox.Text = ""
            MatnrTextBox.Text = ""
            BaseDrawingTextBox.Text = ""
            MarkButton.Visible = False
            NextJobButton.Visible = False
            SelectedFileTextBox.Text = ""
            TotalRowsTextBox.Text = ""
            workbook.Close()
            excel.Quit()
            workbook = Nothing
            excel = Nothing
            sheet = Nothing
            array = Nothing

            MessageBox.Show("All Jobs in this File are already Marked. Choose another File.", "File Marked", MessageBoxButtons.OK)

        Else

            If array(scan, 12) = "NO" Then
                markinglinenotextbox.Text = CStr(scan - 1)
                ModelNoTextBox.Text = array(scan, 1)
                SerialNoTextBox.Text = array(scan, 3)
                MaktxTextBox.Text = array(scan, 4)
                MatnrTextBox.Text = array(scan, 11)
                BaseDrawingTextBox.Text = array(scan, 2)
                MarkButton.Visible = True
                NextJobButton.Visible = True
            ElseIf array(scan, 12) = "YES" Then
                markinglinenotextbox.Text = CStr(scan - 1)
                ModelNoTextBox.Text = array(scan, 1)
                SerialNoTextBox.Text = array(scan, 3)
                MaktxTextBox.Text = array(scan, 4)
                MatnrTextBox.Text = array(scan, 11)
                BaseDrawingTextBox.Text = array(scan, 2)
                MarkButton.Visible = False
                NextJobButton.Visible = True
                MessageBox.Show("This Job is already Marked.")

            End If
        End If
    End Sub

如果我在Sub SelectFileButton_Click中更改数组的值,它仍然保留NextJobButton_Click中的先前值。 我希望数组在整个代码中保持共同的价值。 一旦值在特定函数中发生变化,它应该为所有其他函数保留该值。

1 个答案:

答案 0 :(得分:0)

Public Shared array(,) As Object 

定义了一个可以访问的对象数组,没有任何对象引用。 在函数SelectFileButton中,您定义另一个对象数组变量,也称为数组:

static array(,)

此变量的任何更改都不会反映在您的共享数组中。

  1. 选项:删除静态修饰符并确保访问共享阵列。
  2. 选项:使数组不共享,但将ByRef传递给所有必需的函数