1

好的,基本上,我在类的函数中分配了一个数组值。但是在类执行之后,数组重置为空。这是我的代码:

Public Class MoisacDialog
Public imgArray(,) As Bitmap

Private Sub cmdCancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click
    DialogResult = DialogResult.Cancel
End Sub

Private Sub cmdOK_Click(sender As Object, e As EventArgs) Handles cmdOK.Click
    Try
        Dim rows As Integer = Convert.ToInt32(txtRows.Text)
        Dim cols As Integer = Convert.ToInt32(txtCols.Text)
        If rows > 0 And cols > 0 Then
            ReDim imgArray(rows - 1, cols - 1)
            For i As Integer = 0 To cols - 1
                For j As Integer = 0 To rows - 1
                    Using fileImage As New OpenFileDialog
                        If fileImage.ShowDialog() = DialogResult.OK Then
                            imgArray(i, j) = CType(Bitmap.FromFile(fileImage.FileName), Bitmap)
                        End If
                    End Using
                Next
            Next
            DialogResult = DialogResult.OK
        Else
            MessageBox.Show("Rows/columns entered are out of range.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    Catch ex As FormatException
        MessageBox.Show("Invalid rows/columns entered.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
End Class

执行后cmdOK_Click,整个数组imgArray重置为空。如果我在调用表单中这样使用它:

Using sizeDialog As New MoisacDialog
    If MoisacDialog.ShowDialog() = DialogResult.OK Then
        Dim ImageArray(,) As Bitmap = sizeDialog.imgArray
        _img = ImProc.PixelEffects.Moisac(ImageArray)
        picImage.Image = CType(_img, Image)
    End If
End Using

并使用调试视图,ImageArray设置Nothing在第三行之后,而在cmdOK_Click.

更新:我将第二个片段的第 4 行更改为_img = ImageArray(0,0). 问题仍然存在,aNullReferenceException在包含我的第二个代码段的代码中被抛出和处理。

4

1 回答 1

2

它没有被重置,它从来没有被设置在首位。

Using sizeDialog As New MoisacDialog
    If MoisacDialog.ShowDialog() = DialogResult.OK Then
        Dim ImageArray(,) As Bitmap = sizeDialog.imgArray

请注意,您正在不sizeDialog加选择地使用MoisacDialogMoisacDialog,当用作对象时,是同名类的默认实例。不幸的是,VB 允许这样做,而不是在编译时捕捉到明显的错误。

要更正错误,只需使用sizeDialog.ShowDialog().

于 2012-11-04T12:01:25.343 回答