【问题标题】:Code gets skip when my program loads我的程序加载时代码被跳过
【发布时间】:2014-02-16 02:53:28
【问题描述】:

我正在使用 Visual Studio,但我遇到了一个似乎找不到任何解决方案的问题。在与我的程序加载相关的私有子中,我必须重新调整两个不同的数组,以便将新值添加到这两个二维数组中的第五个位置。可悲的是,我的程序似乎无缘无故地跳过了最后一个数组代码。例如,此代码仅对 tbNoteIniGr2 进行 redim 并跳过 tbNoteIniGr1:

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Width = 380


    ReDim Preserve tbNoteIniGr2(tbNoteIniGr2.GetLength(0) - 1, tbNoteIniGr2.GetLength(1))
    For i = 0 To tbNoteIniGr2.GetLength(0)
        inNoteExamens1 = CInt(tbNoteIniGr2(i, 1)) + CInt(tbNoteIniGr2(i, 2)) + CInt(tbNoteIniGr2(i, 4))
        If inNoteExamens1 >= 45 Then
            inNoteFinale1 = inNoteExamens1 + CInt(tbNoteIniGr2(i, 3))
        Else
            inNoteFinale1 = inNoteExamens1 + CInt(CInt(tbNoteIniGr2(i, 3)) * inNoteExamens1 / 75)
        End If
        tbNoteIniGr2(i, 5) = inNoteFinale1
    Next

    ReDim Preserve tbNoteIniGr1(tbNoteIniGr1.GetLength(0) - 1, tbNoteIniGr1.GetLength(1))
    For i = 0 To tbNoteIniGr1.GetLength(0)
        inNoteExamens = CInt(tbNoteIniGr1(i, 1)) + CInt(tbNoteIniGr1(i, 2)) + CInt(tbNoteIniGr1(i, 4))
        If inNoteExamens >= 45 Then
            inNoteFinale = inNoteExamens + CInt(tbNoteIniGr1(i, 3))
        Else
            inNoteFinale = inNoteExamens + CInt(CInt(tbNoteIniGr1(i, 3)) * inNoteExamens / 75)
        End If
        tbNoteIniGr1(i, 5) = inNoteFinale
    Next
End Sub

与 redim tbNoteIniGr1 并跳过 tbNoteIniGr2 的代码不同

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Width = 380

ReDim Preserve tbNoteIniGr1(tbNoteIniGr1.GetLength(0) - 1, tbNoteIniGr1.GetLength(1))
For i = 0 To tbNoteIniGr1.GetLength(0)
    inNoteExamens = CInt(tbNoteIniGr1(i, 1)) + CInt(tbNoteIniGr1(i, 2)) + CInt(tbNoteIniGr1(i, 4))
    If inNoteExamens >= 45 Then
        inNoteFinale = inNoteExamens + CInt(tbNoteIniGr1(i, 3))
    Else
        inNoteFinale = inNoteExamens + CInt(CInt(tbNoteIniGr1(i, 3)) * inNoteExamens / 75)
    End If
    tbNoteIniGr1(i, 5) = inNoteFinale
Next

ReDim Preserve tbNoteIniGr2(tbNoteIniGr2.GetLength(0) - 1, tbNoteIniGr2.GetLength(1))
For i = 0 To tbNoteIniGr2.GetLength(0)
    inNoteExamens1 = CInt(tbNoteIniGr2(i, 1)) + CInt(tbNoteIniGr2(i, 2)) + CInt(tbNoteIniGr2(i, 4))
    If inNoteExamens1 >= 45 Then
        inNoteFinale1 = inNoteExamens1 + CInt(tbNoteIniGr2(i, 3))
    Else
        inNoteFinale1 = inNoteExamens1 + CInt(CInt(tbNoteIniGr2(i, 3)) * inNoteExamens1 / 75)
    End If
    tbNoteIniGr2(i, 5) = inNoteFinale1
Next

结束子 我使用分步调试,它只是跳过了最后一部分,所以我很困惑。哦,顺便说一句,这些是我的变量

Dim tbNoteIniGr1(,) As String = {{"Jean Narrace", "16", "8", "13", "10"}, {"Chu Paspire", "20", "20", "23", "24"}, {"Yésuis Parfait", "25", "25", "25", "25"},
                                 {"Moyende Moyenner", "20", "18", "20", "12"}, {"Ia Rienla", "19", "24", "21", "22"}, {"Chue Troisième", "21", "22", "24", "24"},
                                 {"Pépé Lacasse", "21", "21", "21", "11"}, {"Jvatu Couler", "19", "18", "14", "10"}, {"Ungars Sessaye", "0", "0", "0", "25"},
                                 {"Mpassetu Tonlab", "10", "10", "25", "10"}, {"Cava Mieux", "10", "15", "20", "25"}, {"Quéyé Suisbonnw", "24", "24", "24", "24"},
                                 {"Sexy Body", "24", "6", "15", "24"}, {"Yvon Meravoir", "12", "11", "18", "15"}, {"Jeannez Assez", "25", "15", "5", "0"},
                                 {"Téreize Constance", "13", "13", "13", "13"}, {"Déhaut Etdébas", "20", "5", "25", "9"}, {"Jpasse Saligne", "13", "17", "14", "16"},
                                 {"Passez Moidonc", "18", "17", "13", "10"}}
Dim tbNoteIniGr2(,) As String = {{"Lucienne Vienne", "16", "24", "19", "21"}, {"Adolf Kirpoupov", "20", "20", "23", "24"}, {"Miville St-Roche", "20", "18", "20", "12"},
                                 {"Gastonne Rochon", "19", "24", "21", "22"}, {"Luc Delaqueduc", "24", "16", "15", "24"}, {"Raphael Angelie", "21", "18", "15", "20"}}
Dim inNoteExamens, inNoteFinale, inEchec, inNoteExamens1, inNoteFinale1, inNombreEtudiant, inTotal As Integer
Dim stChaine As String

编辑:这是整个代码

Public Class frmMain
    Dim tbNoteIniGr1(,) As String = {{"Jean Narrace", "16", "8", "13", "10"}, {"Chu Paspire", "20", "20", "23", "24"}, {"Yésuis Parfait", "25", "25", "25", "25"},
                                     {"Moyende Moyenner", "20", "18", "20", "12"}, {"Ia Rienla", "19", "24", "21", "22"}, {"Chue Troisième", "21", "22", "24", "24"},
                                     {"Pépé Lacasse", "21", "21", "21", "11"}, {"Jvatu Couler", "19", "18", "14", "10"}, {"Ungars Sessaye", "0", "0", "0", "25"},
                                     {"Mpassetu Tonlab", "10", "10", "25", "10"}, {"Cava Mieux", "10", "15", "20", "25"}, {"Quéyé Suisbonnw", "24", "24", "24", "24"},
                                     {"Sexy Body", "24", "6", "15", "24"}, {"Yvon Meravoir", "12", "11", "18", "15"}, {"Jeannez Assez", "25", "15", "5", "0"},
                                     {"Téreize Constance", "13", "13", "13", "13"}, {"Déhaut Etdébas", "20", "5", "25", "9"}, {"Jpasse Saligne", "13", "17", "14", "16"},
                                     {"Passez Moidonc", "18", "17", "13", "10"}}
    Dim tbNoteIniGr2(,) As String = {{"Lucienne Vienne", "16", "24", "19", "21"}, {"Adolf Kirpoupov", "20", "20", "23", "24"}, {"Miville St-Roche", "20", "18", "20", "12"},
                                     {"Gastonne Rochon", "19", "24", "21", "22"}, {"Luc Delaqueduc", "24", "16", "15", "24"}, {"Raphael Angelie", "21", "18", "15", "20"}}
    Dim inNoteExamens, inNoteFinale, inEchec, inNoteExamens1, inNoteFinale1, inNombreEtudiant, inTotal As Integer
    Dim stChaine As String

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Width = 380

        ReDim Preserve tbNoteIniGr1(tbNoteIniGr1.GetLength(0) - 1, tbNoteIniGr1.GetLength(1))
        For i = 0 To tbNoteIniGr1.GetLength(0)
            inNoteExamens = CInt(tbNoteIniGr1(i, 1)) + CInt(tbNoteIniGr1(i, 2)) + CInt(tbNoteIniGr1(i, 4))
            If inNoteExamens >= 45 Then
                inNoteFinale = inNoteExamens + CInt(tbNoteIniGr1(i, 3))
            Else
                inNoteFinale = inNoteExamens + CInt(CInt(tbNoteIniGr1(i, 3)) * inNoteExamens / 75)
            End If
            tbNoteIniGr1(i, 5) = inNoteFinale
        Next

        ReDim Preserve tbNoteIniGr2(tbNoteIniGr2.GetLength(0) - 1, tbNoteIniGr2.GetLength(1))
        For i = 0 To tbNoteIniGr2.GetLength(0)
            inNoteExamens1 = CInt(tbNoteIniGr2(i, 1)) + CInt(tbNoteIniGr2(i, 2)) + CInt(tbNoteIniGr2(i, 4))
            If inNoteExamens1 >= 45 Then
                inNoteFinale1 = inNoteExamens1 + CInt(tbNoteIniGr2(i, 3))
            Else
                inNoteFinale1 = inNoteExamens1 + CInt(CInt(tbNoteIniGr2(i, 3)) * inNoteExamens1 / 75)
            End If
            tbNoteIniGr2(i, 5) = inNoteFinale1
        Next
    End Sub

    Private Sub btnNoteGr1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNoteGr1.Click
        rtbText.Location = New Point(98, 13)
        gbxStats.Location = New Point(466.13)

        inEchec = 0
        stChaine = ""
        stChaine = "Notes finales pour le groupe 01" & vbCrLf & "Cours 420-246" & vbCrLf & "Session Hiver 2011" &
            vbCrLf & vbCrLf & "Nom" & Space(20) & "NoteF" & vbCrLf & vbCrLf
        For i = 0 To tbNoteIniGr1.GetLength(0) - 1
            stChaine &= tbNoteIniGr1(i, 0) & Space(25 - tbNoteIniGr1(i, 0).Length) & tbNoteIniGr1(i, 5).PadLeft(3) & vbCrLf
            If tbNoteIniGr1(i, 5) < 60 Then
                inEchec += 1
            End If
        Next
        rtbText.Text = stChaine
        tbxEhcas.Text = CStr(inEchec)
    End Sub

    Private Sub btnNoteGr2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNoteGr2.Click
        rtbText.Location = New Point(98, 13)
        gbxStats.Location = New Point(466.13)

        inEchec = 0
        stChaine = ""
        stChaine = "Notes finales pour le groupe 02" & vbCrLf & "Cours 420-246" & vbCrLf & "Session Hiver 2011" &
            vbCrLf & vbCrLf & "Nom" & Space(20) & "NoteF" & vbCrLf & vbCrLf
        For i = 0 To tbNoteIniGr2.GetLength(0) - 1
            stChaine &= tbNoteIniGr2(i, 0) & Space(25 - tbNoteIniGr2(i, 0).Length) & tbNoteIniGr2(i, 4).PadLeft(3) & vbCrLf
            If tbNoteIniGr2(i, 5) < 60 Then
                inEchec += 1
            End If
        Next
        rtbText.Text = stChaine
        tbxEhcas.Text = CStr(inEchec)
    End Sub

    Private Sub btnStatGr1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatGr1.Click
        rtbText.Location = New Point(466.13)
        gbxStats.Location = New Point(98, 13)

        inTotal = 0
        inNombreEtudiant = 0
        inEchec = 0

        For i = 0 To tbNoteIniGr1.GetLength(0) - 1
            inNombreEtudiant += 1
            inTotal += CInt(tbNoteIniGr1(i, 5))
            If tbNoteIniGr1(i, 5) < 60 Then
                inEchec += 1
            End If
        Next

        tbxGroupe.Text = "01"
        tbxMoyenne.Text = inTotal / inNombreEtudiant
        tbxNombreEchec.Text = inEchec
        tbxNombreEtudiant.Text = inNombreEtudiant
    End Sub

    Private Sub btnStatGr2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatGr2.Click
        rtbText.Location = New Point(466.13)
        gbxStats.Location = New Point(98, 13)

        inTotal = 0
        inNombreEtudiant = 0
        inEchec = 0

        For i = 0 To tbNoteIniGr2.GetLength(0) - 1
            inNombreEtudiant += 1
            inTotal += CInt(tbNoteIniGr2(i, 5))
            If tbNoteIniGr2(i, 5) < 60 Then
                inEchec += 1
            End If
        Next

        tbxGroupe.Text = "02"
        tbxMoyenne.Text = inTotal / inNombreEtudiant
        tbxNombreEchec.Text = inEchec
        tbxNombreEtudiant.Text = inNombreEtudiant
    End Sub
End Class

【问题讨论】:

  • 你试过使用断点吗?
  • 调试的重点是让您可以检查您的工作。试一试。
  • 是的,我使用了它,当我在第二个 redim 数组上添加断点时,它根本就没有停止过。
  • 我不知道这是否与我的问题有关,但这是我第一次使用 strict&explicit 选项。
  • 奇怪的是您没有收到错误,因为您超出了数组边界

标签: arrays vb.net visual-studio-2010


【解决方案1】:

来自 MSDN:

使用 Preserve 调整大小。如果您使用Preserve,您可以仅调整数组的最后一个维度。对于每个其他维度,您必须指定现有数组的边界。 例如,如果您的数组只有一个维度,您可以调整该维度的大小并仍然保留数组的所有内容,因为您正在更改最后一个也是唯一的维度。但是,如果您的数组有两个或多个维度,如果您使用 Preserve,则可以仅更改最后一个维度的大小。

变态

【讨论】:

  • 不,这不是真正的问题,因为我正在调整最后一个维度的大小。 ReDim Preserve tbNoteIniGr2(tbNoteIniGr2.GetLength(0) - 1, tbNoteIniGr2.GetLength(1)) 相当于 ReDim Preserve tbNoteIniGr2(5,5) 如果你愿意,我可以上传我的 Visual Studio 项目
  • @user2973533 将两个 for 循环 tbNoteIniGr1.GetLength(0) and tbNoteIniGr2.GetLength(0) 更改为 tbNoteIniGr1.GetLength(0) - 1 and tbNoteIniGr2.GetLength(0) - 1。现在可以了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多