-1 \" and after.any help would be wonderful.开发者_运维问答" />
开发者

Error In VB.Net code

开发者 https://www.devze.com 2022-12-25 02:24 出处:网络
I am getting the error \"Format Exception was unhandled at \"Do While objectReader.Peek <> -1 \" and after.any help would be wonderful.开发者_运维问答

I am getting the error "Format Exception was unhandled at "Do While objectReader.Peek <> -1 " and after. any help would be wonderful.开发者_运维问答

'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


Put a break-point on these lines and step through your code:

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

Whatever is being returned by ReadLine isn't in the proper decimal or integer format when passed to the converters. You'll need to add some try/catch blocks around the do-while loop operations to handle it gracefully and make sure your data is formatted the way you expected since the wrong parts are being used in this scenario.

Also, each call to ReadLine is returning an entirely new line and the Peek check won't account for those. As long as you can trust your data that's fine.


Instead of using Convert.ToDecimal an Convert.ToInt32, try using Decimal.TryParse() and Int32.TryParse(). If they don't parse, you know your file isn't setup correctly. If they do parse, then you've loaded the value into a variable for your use.

EDIT:

Use it like this.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消