/ / Як налагодити виконання виклику в vb.net? - vb.net, візуальна студія-2015

Як налагодити програму cast in vb.net? - vb.net, візуальна студія-2015

Я отримую винятковий склад, і цей код я переписував багато разів. Я отримую виняток у наступному рядку:

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

І я тільки отримую результати в lblNumberOfHurricans. інші дві мітки не дають результатів. Я думав, що я отримую це, коли з'явився виняток у ролях.

Чи може хтось запропонувати, як отримати результати та зупинити виняток?

Ось що у мене поки що (ну принаймні остання спроба).

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

Відповіді:

2 для відповіді № 1

Ваша помилка полягає в тому, що ви намагаєтеся перетворити цілий масив рядків у ціле число:

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

Вам потрібно буде зателефонувати в індекс _strYears:

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

Це також пояснить, чому інші мітки не оновлюються, оскільки hHurricanAverage ніколи не обчислюється.