VB.Net代码中的错误

时间:2010-03-28 19:46:44

标签: vb.net

我收到错误“格式异常未处理”Do While objectReader.Peek<> -1 “之后。任何帮助都会很棒。

'Date: Class    03/20/2010
'Program Purpose:  When code is executed data will be pulled from a text file
'that contains the named storms to find the average number of storms during the time
'period chosen by the user and to find the most active year. between the range of
'years 1990 and 2008 

Option Strict On

Public Class frmHurricane

    Private _intNumberOfHuricanes As Integer = 5
    Public Shared _intSizeOfArray As Integer = 7
    Public Shared _strHuricaneList(_intSizeOfArray) As String
    Private _strID(_intSizeOfArray) As String
    Private _decYears(_intSizeOfArray) As Decimal
    Private _decFinal(_intSizeOfArray) As Decimal
    Private _intNumber(_intSizeOfArray) As Integer

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As             System.EventArgs) Handles MyBase.Load
        'The frmHurricane load event reads the Hurricane text file and
        'fill the combotBox object with the data.

        'Initialize an instance of the StreamReader Object and declare variable page 675 on the book
        Dim objectReader As IO.StreamReader
        Dim strLocationAndNameOfFile As String = "C:\huricanes.txt"
        Dim intCount As Integer = 0
        Dim intFill As Integer
        Dim strFileError As String = "The file is not available.  Please restart application when available"

        'This is where we code the file if it exist.
        If IO.File.Exists(strLocationAndNameOfFile) Then
            objectReader = IO.File.OpenText(strLocationAndNameOfFile)
            'Read the file line by line until the file is complete
            Do While objectReader.Peek <> -1
                **_strHuricaneList(intCount) = objectReader.ReadLine()
                _strID(intCount) = objectReader.ReadLine()
                _decYears(intCount) = Convert.ToDecimal(objectReader.ReadLine())
                _intNumber(intCount) = Convert.ToInt32(objectReader.ReadLine())
                intCount += 1**
            Loop
            objectReader.Close()

        'With any luck the data will go to the Data Box
        For intFill = 0 To (_strID.Length - 1)
            Me.cboByYear.Items.Add(_strID(intFill))
        Next
    Else
        MsgBox(strFileError, , "Error")
        Me.Close()

    End If
End Sub

2 个答案:

答案 0 :(得分:1)

在这些行上设置一个断点并逐步执行代码:

_decYears(intCount) = Convert.ToDecimal(objectReader.ReadLine())
_intNumber(intCount) = Convert.ToInt32(objectReader.ReadLine())

ReadLine返回的内容在传递给转换器时不是正确的十进制或整数格式。你需要在do-while循环操作周围添加一些try / catch块来优雅地处理它,并确保你的数据按照预期的方式格式化,因为在这种情况下使用了错误的部分。

此外,每次拨打ReadLine都会返回一个全新的行,Peek检查不会考虑这些。只要你可以信任你的数据就好了。

答案 1 :(得分:0)

请尝试使用Decimal.TryParse()Int32.TryParse(),而不是使用Convert.ToDecimalConvert.ToInt32。如果它们不解析,则表示您的文件设置不正确。如果它们进行解析,那么您已将值加载到变量中供您使用。

修改

像这样使用它。

Dim myDecimal As Decimal

If Decimal.TryParse(objectReader.ReadLine(), myDecimal) Then
    'your string successfully parsed. Use myDecimal however you want. It has the parsed value.
Else
    'your string is not a decimal. Now that you know that, you can handle this situation here without having to catch an exception.
End If