对象引用未设置为具有类作为成员的类的对象的实例

时间:2010-08-06 00:36:16

标签: vb.net loops object

这是我的结构:

Friend Class StandardFormatFile
    Friend fileType As String
    Friend numberOfSeries As Integer
    Friend standardSeriesData As New ArrayList

End Class

Friend Class StandardFormatFileSeries

    Friend standardNumOfElements As Integer
    Friend standardSeriesName As String
    Friend standardDataElements As New ArrayList

End Class

Friend Class StandardFormatFileElement
    Friend standardX_TimeValue As Single
    Friend standardY_SampleValue As Single

End Class

现在,这是实现它的代码:

   ReDim Preserve firstFile.standardSeriesData(firstFile.numberOfSeries - 1)

    For i As Integer = 0 To firstFile.numberOfSeries - 1
        Try

            firstFile.standardSeriesData(i).standardSeriesName = GetSeriesName(file1BReader)
            secondFile.standardSeriesData(i).standardNumOfElements = GetSeriesName(file2BReader)

            ReDim Preserve firstFile.standardSeriesData(i).standardDataElements(firstFile.standardSeriesData(i).standardNumOfElements)

            For j As Integer = 0 To firstFile.standardSeriesData(i).standardNumOfElements - 1
                firstFile.standardSeriesData(i).standardDataElements(j).standardX_TimeValue = file1BReader.ReadSingle
                firstFile.standardSeriesData(i).standardDataElements(j).standardY_SampleValue = file1BReader.ReadSingle

                secondFile.standardSeriesData(i).standardDataElements(j).standardX_TimeValue = file2BReader.ReadSingle
                secondFile.standardSeriesData(i).standardDataElements(j).standardY_SampleValue = file2BReader.ReadSingle
            Next

        Catch ex As Exception
            MsgBox(ex.Message & " *** i = " & i)
        End Try

    Next

我不知道为什么我收到错误我收到错误。我是ReDim'ing ......不应该工作吗?我已经尝试过Arraylists和List(对象)并且无法正常工作。

帮助......卡住了!

3 个答案:

答案 0 :(得分:1)

由于firstFile.numberOfSeries将评估为0,

是否会将第一行重新调整为-1?

答案 1 :(得分:1)

这是一个arraylist,不要redim,只需添加。

你应该明确地使用泛型

Friend Class StandardFormatFile
    Friend fileType As String
    Friend numberOfSeries As Integer
    Friend standardSeriesData As New List(of StandardFormatFileSeries)

End Class

Friend Class StandardFormatFileSeries

    Friend standardNumOfElements As Integer
    Friend standardSeriesName As String
    Friend standardDataElements As New List(of StandardFormatFileElement)

End Class

Friend Class StandardFormatFileElement
    Friend standardX_TimeValue As Single
    Friend standardY_SampleValue As Single

End Class

同样,它是一个包含在对象中的数据结构,而不是数组。 所以使用add而不是redim。

干杯!

答案 2 :(得分:0)

好的,所以我想出来......这样的菜鸟动作。我不知道为什么我无法独自解决这个问题。我是在正确的路线...我只是有一个大脑放屁:

 For i As Integer = 0 To firstFile.numberOfSeries - 1

        Dim tempSeriesData1 As New StandardFormatFileSeries
        Dim tempSeriesData2 As New StandardFormatFileSeries

        tempSeriesData1.standardNumOfElements = file1BReader.ReadInt32
        tempSeriesData1.standardSeriesName = GetSeriesName(file1BReader)

        tempSeriesData2.standardNumOfElements = file2BReader.ReadInt32
        tempSeriesData2.standardSeriesName = GetSeriesName(file2BReader)


        For j As Integer = 0 To tempSeriesData1.standardNumOfElements - 1
            Dim tempElementData1 As New StandardFormatFileElement
            Dim tempElementData2 As New StandardFormatFileElement

            tempElementData1.standardX_TimeValue = file1BReader.ReadSingle
            tempElementData1.standardY_SampleValue = file1BReader.ReadSingle
            tempElementData2.standardX_TimeValue = file2BReader.ReadSingle
            tempElementData2.standardY_SampleValue = file2BReader.ReadSingle

            tempSeriesData1.standardDataElements.Add(tempElementData1)
            tempSeriesData2.standardDataElements.Add(tempElementData2)


        Next

        firstFile.standardSeriesData.Add(tempSeriesData1)
        secondFile.standardSeriesData.Add(tempSeriesData2)


    Next