How to debug a cast execption in vb.net?
<p>I am getting a cast exception and I have re-written this code a large number of times. I am getting the exception on the following line:</p>

<pre><code>If (CInt(hHurricaneYear) &lt; CInt(_strYears(hAverage))) Then
</code></pre>

<p>And I am only getting results in the <code>lblNumberOfHurricans</code>.  the other two labels are not showing any results.  I thought I was getting it when the cast exception showed up.</p>

<p>Can anyone suggest how to get the results and stop the exception?</p>

<p>Here is what I have so far (well at least the last try).</p>

<pre><code>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 &lt;&gt; -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 &gt;= 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 &gt;= 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) &lt; 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 " &amp;
            _strYears(intSelectedYear) &amp; " is " &amp; _intNumberOfHurricans(intSelectedYear).ToString() &amp; "."
        lblAvergeNumberHurricanes.Text = "The Average Number of Storms was " &amp;
            hHurricaneAverage &amp; " Hurricanes."

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

    End Sub
Option strict on
</code></pre>