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

Arrays 如何使用vb.net访问不同子例程中的对象数组,arrays,vb.net,Arrays,Vb.net,我正在构建一个前端,在该前端中,我必须接受用户动态选择的excel工作表中的数据。我已将此数据复制到arrayobject 我希望能够跨所有子例程访问此数组对象。但问题是,如果我更改子程序B中数组的值,它仍然保留子程序A中以前的值 请通读下面的代码 Public SelectedFile As String Public excel As New Application Public workbook As Workbook Public sheet As Worksheet

我正在构建一个前端,在该前端中,我必须接受用户动态选择的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
Public SelectedFile作为字符串
公共excel作为新的应用程序
公共工作簿作为工作簿
公开表格作为工作表
公共区域范围
作为对象的公共共享数组(,)
作为整数的公共扫描
私有子SelectFileButton\单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理SelectFileButton。单击
'在文本框中显示所选文件
SelectedFile=ComboBox1.GetItemText(ComboBox1.SelectedItem)
如果选择File Nothing,则
SelectedFileTextBox.Text=SelectedFile
将路径设置为字符串
path=ExcelFolder&“\”&SelectedFile
excel=新应用程序
excel.Workbooks.Open(文件名:=path,[ReadOnly]:=False)
工作簿=excel.ActiveWorkbook
excel.UserControl=True
工作表=excel。工作表(1)
r=表1.UsedRange
'将所有单元格加载到二维数组中。
静态数组(,)=r.Value(XlRangeValueDataType.xlRangeValueDefault)
'获取数组的边界。
Dim bound0为整数=数组。GetUpperBound(0)'最后一行号
Dim bound1为整数=数组。GetUpperBound(1)'最后一列编号
'获取总行数
Dim totalrows As Integer=bound0-1',因为第一行是标题
TotalRowsTextBox.Text=CStr(totalrows)
'获取所选文件中要标记的第一个作业
对于扫描=2到0
如果微调(阵列(扫描,12))=微调(“否”),则
markinglinenotextbox.Text=CStr(扫描-1)
ModelNoTextBox.Text=数组(扫描,1)
SerialNoTextBox.Text=数组(扫描,3)
MaktxTextBox.Text=数组(扫描,4)
MatnrTextBox.Text=数组(扫描,11)
BaseDrawingTextBox.Text=数组(扫描,2)
MarkButton.Visible=True
NextJobButton.Visible=True
退出
ElseIf数组(扫描,12)=“是”,然后
扫描=扫描+1
如果结束
下一个
'如果标记了所有文件
如果扫描>边界0,则
markinglinenotextbox.Text=“”
ModelNoTextBox.Text=“”
SerialNoTextBox.Text=“”
MaktxTextBox.Text=“”
MatnrTextBox.Text=“”
BaseDrawingTextBox.Text=“”
MarkButton.Visible=False
NextJobButton.Visible=False
SelectedFileTextBox.Text=“”
TotalRowsTextBox.Text=“”
工作簿.关闭()
excel.Quit()
工作簿=无
excel=无
表=无
擦除数组
Show(“此文件中的所有作业都已标记。请选择其他文件。”,“已标记文件”,MessageBox按钮。确定)
如果结束
如果结束
端接头
私有子NextJobButton\单击(发件人作为对象,e作为事件参数)处理NextJobButton。单击
静态数组(,)=r.Value(XlRangeValueDataType.xlRangeValueDefault)
扫描=扫描+1
如果扫描>最后一行,则
markinglinenotextbox.Text=“”
ModelNoTextBox.Text=“”
SerialNoTextBox.Text=“”
MaktxTextBox.Text=“”
MatnrTextBox.Text=“”
BaseDrawingTextBox.Text=“”
MarkButton.Visible=False
NextJobButton.Visible=False
SelectedFileTextBox.Text=“”
TotalRowsTextBox.Text=“”
工作簿.关闭()
excel.Quit()
工作簿=无
excel=无
表=无
数组=无
Show(“此文件中的所有作业都已标记。请选择其他文件。”,“已标记文件”,MessageBox按钮。确定)
其他的
如果数组(扫描,12)=“否”,则
markinglinenotextbox.Text=CStr(扫描-1)
ModelNoTextBox.Text=数组(扫描,1)
SerialNoTextBox.Text=数组(扫描,3)
MaktxTextBox.Text=数组(扫描,4)
MatnrTextBox.Text=数组(扫描,11)
BaseDrawingTextBox.Text=数组(扫描,2)
MarkButton.Visible=True
NextJobButton.Visible=True
ElseIf数组(扫描,12)=“是”,然后
markinglinenotextbox.Text=CStr(扫描-1)
ModelNoTextBox.Text=数组(扫描,1)
SerialNoTextBox.Text=数组(扫描,3)
MaktxTextBox.Text=数组(扫描,4)
MatnrTextBox.Text=数组(扫描,11)
BaseDrawingTextBox.Text=数组(扫描,2)
MarkButton.Visible=False
NextJobButton.Visible=True
MessageBox.Show(“此作业已标记。”)
如果结束
如果结束
端接头
在这里,如果我在Sub-SelectFileButton\u-Click中更改数组的值,它仍然会在NextJobButton\u-Click中保留以前的值。 我希望数组在整个代码中都保存公共值。 也就是说,一旦某个特定函数中的值发生更改,它就应该为所有其他函数保留该值

Public Shared array(,) As Object 
定义无需任何对象引用即可访问的对象数组。 在函数
SelectFileButton
中定义另一个对象数组变量,也称为数组:

static array(,)
对此变量的任何更改都不会反映在共享数组中

  • Op