/ / Ako odladiť obsadenie popravy v vb.net? - vb.net, visual-studio-2015

Ako ladiť vykonanie cast v vb.net? - vb.net, vizuálne štúdio-2015

Dostávam výnimku pre obsadenie a tento kód som veľakrát prepísal. Výnimku dostávam v nasledujúcom riadku:

If (CInt(hHurricaneYear) < CInt(_strYears(hAverage))) Then

A ja dostávam výsledky iba v lblNumberOfHurricans, ďalšie dve štítky nezobrazujú žiadne výsledky. Myslel som, že to dostávam, keď sa objaví výnimka v obsadení.

Môže niekto navrhnúť, ako dosiahnuť výsledky a zastaviť výnimku?

Tu je to, čo mám zatiaľ (aspoň aspoň posledný pokus).

Option Strict On

Public Class frmHurricaneStatistics

"   Class level Private variables.
Public Shared _intSizeOfArray As Integer = 20
Private _strYears(_intSizeOfArray) As String
Private _intNumberOfHurricans(_intSizeOfArray) As Integer

Private Sub frmHurricaneStatistics_Load(sender As Object, e As EventArgs
) Handles MyBase.Load

"   This load event reads the inventory text file and fills
"   the ComboBox object with the Hurricane Statistics.

"   Initialize an instace of the streamreader object and declare variables.
Dim objReader As IO.StreamReader
Dim strHurricaneStatistics As String = "Hurricanes.txt"
Dim intCount As Integer = 0
Dim intFill As Integer
Dim strFileError As String = "The file is not available. Please restart the
application when the file is available."

"   Verify the Hurricane.txt file exists.
If IO.File.Exists(strHurricaneStatistics) Then
objReader = IO.File.OpenText(strHurricaneStatistics)

"   Read the file line by line until the file is completed.
Do While objReader.Peek <> -1
_strYears(intCount) = objReader.ReadLine()
_intNumberOfHurricans(intCount) = Convert.ToInt32(objReader.ReadLine())
intCount += 1
Loop
objReader.Close()

"   The ComboBox objext is filled with the Years for Hurricanes.
For intFill = 0 To (_strYears.Length - 1)
cmbYears.Items.Add(_strYears(intFill))
Next
Else
MsgBox(strFileError, , "Error")
Close()

"  If ComboBox is filled then enable the Display Statistics button.
"btnDisplayStatistics.Enabled = True
End If
End Sub

Private Sub btnDisplayStatistics_Click(sender As Object, e As EventArgs
) Handles btnDisplayStatistics.Click

"   This click event calls the sub procedures for the selected years and
"   the number of hurricans in that year.
Dim intSelectedYear As Integer
Dim strMissingSelection As String = "Missing Selection"
Dim strSelectAYearError As String = "Please Select a Year"

"   If the ComboBox object has a selection, Display Statistics.
If cmbYears.SelectedIndex >= 0 Then
intSelectedYear = cmbYears.SelectedIndex
Else
MsgBox(strSelectAYearError, , strMissingSelection)
End If


Private Sub btnDisplayStatistics_Click(sender As Object, e As EventArgs
) Handles btnDisplayStatistics.Click

"   This click event calls the sub procedures for the selected years and
"   the number of hurricans in that year.
Dim intSelectedYear As Integer
Dim strMissingSelection As String = "Missing Selection"
Dim strSelectAYearError As String = "Please Select a Year"

"   If the ComboBox object has a selection, call the Display Statistics procedure.
If cmbYears.SelectedIndex >= 0 Then
intSelectedYear = cmbYears.SelectedIndex
Else
MsgBox(strSelectAYearError, , strMissingSelection)
End If

"   This procedure MakeLabelsVisible Is called to display the labels
"   And the results.
MakeLabelsVisible()

Dim hHurricaneAverage As Integer
Dim hHurricaneYear As Integer = 0

For hAverage As Integer = 0 To _strYears.Length - 1
If (CInt(hHurricaneYear) < CInt(_strYears(hAverage))) Then
hHurricaneYear = CInt(CType(CInt(_strYears(hAverage)), String))
End If
hHurricaneAverage = hHurricaneAverage + CInt((_strYears.ToString))

hHurricaneAverage = CInt(hHurricaneAverage / _strYears.Length)
Next

"   Display the statistics for the Storm Average in the selected Year
"   and the most active year within the range of year.

lblNumberOfHurricanes.Text = "The Number of Hurricanes in the Year " &
_strYears(intSelectedYear) & " is " & _intNumberOfHurricans(intSelectedYear).ToString() & "."
lblAvergeNumberHurricanes.Text = "The Average Number of Storms was " &
hHurricaneAverage & " Hurricanes."

Dim intSizeOfArray As Integer = Nothing
lblMostStorms.Text = "The Year "(CInt(_strYears(CInt(hHurricaneYear.ToString())) & "
Had The Most Storms Between " & (_strYears(0) & _strYears(20).ToString)))

End Sub
Option strict on

odpovede:

2 pre odpoveď č. 1

Vaša chyba spočíva v tom, že sa snažíte previesť celé pole reťazcov na celé číslo:

hHurricaneAverage = hHurricaneAverage + CInt((_strYears.ToString))

Budete musieť zavolať do indexu _strYears:

hHurricaneAverage = hHurricaneAverage + CInt((_strYears(hAverage).ToString))

To tiež vysvetlí, prečo sa ostatné štítky neaktualizujú, pretože hHurricanA Bever sa nikdy nevypočítava.