/ / ¿Cómo depurar una execption cast en vb.net? - vb.net, visual-studio-2015

Cómo depurar una ejecución de lanzamiento en vb.net? - vb.net, visual-studio-2015

Recibo una excepción de reparto y he reescrito este código muchas veces. Estoy recibiendo la excepción en la siguiente línea:

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

Y sólo estoy obteniendo resultados en el lblNumberOfHurricans. Las otras dos etiquetas no muestran ningún resultado. Pensé que lo estaba consiguiendo cuando apareció la excepción del reparto.

¿Alguien puede sugerir cómo obtener los resultados y detener la excepción?

Aquí está lo que tengo hasta ahora (bueno, al menos el último intento).

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

Respuestas

2 para la respuesta № 1

Su error radica en que está intentando convertir una matriz de cadenas completa en un entero:

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

Deberá llamar al índice de _strYears:

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

Esto también explicará por qué las otras etiquetas no se actualizan, porque hHurricanAverage nunca se calcula.