0

当我在我正在工作的“Visual Studio 2011 Ultimate 12”中按开始时,它说:

“InvalidOperationException 未处理,调试器捕获了异常,用户设置表明应该发生中断。此线程停止,调用堆栈上只有外部代码帧。外部代码帧通常来自框架代码,但也可以包括其他在目标进程中加载​​的优化模块。"`

我的代码:

    Public Class Form1
    Private matrix As Integer(,) = PopulateMatrix()

    Private Sub ClickMouse(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseClick
        MsgBox(e.Clicks & e.ColumnIndex & e.RowIndex)

        matrix(e.ColumnIndex, e.RowIndex) = 0

        Matrixtomatrixdef(matrix)
    End Sub

    Private Function PopulateMatrix() As Integer(,)  
        Dim matrix(10, 10) As Integer
        For rown = 0 To 9
            For columnn = 0 To 9
                matrix(columnn, rown) = 1
            Next
        Next
        Return matrix
    End Function

    Private Sub Matrixtomatrixdef(matrix As Integer(,))     
        Dim Matrixdef(10, 10) As Integer
        For rown = 0 To 9
            For columnn = 0 To 9
                Matrixdef(columnn, rown) = matrix(columnn, rown)
                Debug.Write(Matrixdef(columnn, rown).ToString & " ")
            Next
            Debug.WriteLine("")
        Next
    End Sub
End Class
4

3 回答 3

1

因为您试图在初始化类时调用该函数,而您不能这样做。声明变量,但稍后在构造函数中或在另一个适当的位置设置它。

Private matrix As Integer(,)

Public Sub New() 'constructor
  matrix = PopulateMatrix
End Sub 
于 2013-05-22T18:08:13.963 回答
0

正确的代码:

   Private matrix As Integer(,)
Private Sub populate1s(sender As Object, e As EventArgs) Handles Button3.Click
    matrix = PopulateMatrix()
End Sub

    Private Function PopulateMatrix() As Integer(,)
        Dim matrix(10, 10) As Integer
        For rown = 0 To 9
            For columnn = 0 To 9
                matrix(columnn, rown) = 1
            Next
        Next
        Return matrix
    End Function


Public Sub ClickMouse(sender As Object, e As DataGridViewCellMouseEventArgs) Handles LRInc.CellMouseClick
    matrix(e.ColumnIndex, e.RowIndex) = 0
    For rown = 0 To 9
        For columnn = 0 To 9
            Debug.Write(matrix(columnn, rown).ToString & " ")
        Next
        Debug.WriteLine("")
    Next
End Sub
于 2013-05-23T18:32:28.040 回答
0

您不能在作为字段定义的一部分的同一对象上调用方法。实例方法只有在所有字段都初始化后才可用。

于 2013-05-22T18:12:42.880 回答