Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.Net代码中的错误_Vb.net - Fatal编程技术网

VB.Net代码中的错误

VB.Net代码中的错误,vb.net,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 '

我得到了错误“格式异常在”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
”日期:2010年3月20日
'程序用途:执行代码时,数据将从文本文件中提取
“其中包含命名风暴,以查找该时间段内风暴的平均数量
'由用户选择的期间,以查找最活跃的年份。介于
“1990年和2008年
选项严格限制在
公共级FRMHURRIANE
Private _intNumberOfHuricanes作为整数=5
公共共享_intSizeOfArray作为整数=7
公共共享的_strHuricaneList(_intSizeOfArray)作为字符串
Private _strID(_intSizeOfArray)作为字符串
私有十年(_intSizeOfArray)为十进制
私有的十进位(_intSizeOfArray)作为十进制
Private _intNumber(_intSizeOfArray)作为整数
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
'frmHurricane加载事件读取飓风文本文件并
'用数据填充combotBox对象。
'初始化StreamReader对象的实例,并在书的第675页声明变量
Dim objectReader作为IO.StreamReader
Dim STRLOCATIONAND NAME OFFILE As String=“C:\HURIANES.txt”
Dim intCount作为整数=0
Dim intFill作为整数
Dim strFileError As String=“文件不可用。请在可用时重新启动应用程序“
'这是我们对文件(如果存在)进行编码的地方。
如果IO.File.Exists(strLocationAndNameOfFile),则
objectReader=IO.File.OpenText(strLocationAndNameOfFile)
'逐行读取文件,直到文件完成
DowhileObjectReader.Peek-1
**_strHuricaneList(intCount)=objectReader.ReadLine()
_strID(intCount)=objectReader.ReadLine()
_十年(整数)=Convert.ToDecimal(objectReader.ReadLine())
_intNumber(intCount)=转换.ToInt32(objectReader.ReadLine())
整数计数+=1**
环
objectReader.Close()
“如果运气好,数据将进入数据框
对于整数填充=0到(_strID.Length-1)
Me.cboByYear.Items.Add(_strID(intFill))
下一个
其他的
MsgBox(strFileError,“Error”)
我
如果结束
端接头

在这些行上画一个断点,然后逐步完成代码:

_decYears(intCount) = Convert.ToDecimal(objectReader.ReadLine())
_intNumber(intCount) = Convert.ToInt32(objectReader.ReadLine())
当传递给转换器时,
ReadLine
返回的任何内容都不是正确的十进制或整数格式。您需要在do while循环操作周围添加一些try/catch块,以优雅地处理它,并确保数据的格式符合您的预期,因为在此场景中使用了错误的部分


另外,每次调用
ReadLine
都会返回一个全新的行,而
Peek
检查不会考虑这些。只要您可以信任您的数据,就可以了。

不要使用
Convert.ToDecimal
转换.ToInt32,而是尝试使用and。如果它们不解析,您就知道您的文件设置不正确。如果它们进行解析,那么您已经将该值加载到一个变量中供您使用

编辑:

像这样使用它

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

错误1重载解析失败,因为没有可访问的“TryParse”接受此数量的参数。已尝试。这是我得到的结果