好的,基本上,我在类的函数中分配一个数组值。但是在类执行之后,数组将重置为空。这是我的代码:
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)
。问题仍然存在,NullReferenceException
被抛出并在包含我的第二个代码段的代码中处理。
答案 0 :(得分:2)
它没有被重置,它从未被设置在第一位。
Using sizeDialog As New MoisacDialog
If MoisacDialog.ShowDialog() = DialogResult.OK Then
Dim ImageArray(,) As Bitmap = sizeDialog.imgArray
请注意,您不加区别地使用sizeDialog
和MoisacDialog
。 MoisacDialog
用作对象时,是同名类的默认实例。不幸的是VB允许这样做而不是在编译时捕获明显的错误。
要更正错误,只需使用sizeDialog.ShowDialog()
。