c# - 我已经用 vb 和 C# 编写了确切的代码,但它们的工作方式并不相同……逻辑是相同的……我希望

标签 c# .net vb.net binaryreader

有点像 C# 的新手,正在尝试扩展我的能力。我在 VB 中有这段代码:

    Private Sub BreakdownFilesToCompare(ByRef file1BReader As BinaryReader, _
                                  ByRef file2BReader As BinaryReader, _
                                  ByRef firstFile As StandardFormatFile, _
                                  ByRef secondFile As StandardFormatFile)

    file1BReader.ReadInt32()
    file2BReader.ReadInt32()

    firstFile.numberOfSeries = file1BReader.ReadInt32
    secondFile.numberOfSeries = file2BReader.ReadInt32

    If firstFile.numberOfSeries <> secondFile.numberOfSeries Then
        WriteToConsole("The number of Elements the two files do not match...Stopping")
        Exit Sub
    End If

    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


            tempElementData1.standardX_TimeValue = file1BReader.ReadSingle
            tempElementData1.standardY_SampleValue = file1BReader.ReadSingle
            tempSeriesData1.standardDataElements.Add(tempElementData1)
        Next

        For k As Integer = 0 To tempSeriesData2.standardNumOfElements - 1
            Dim tempElementData2 As New StandardFormatFileElement

            tempElementData2.standardX_TimeValue = file2BReader.ReadSingle
            tempElementData2.standardY_SampleValue = file2BReader.ReadSingle

            tempSeriesData2.standardDataElements.Add(tempElementData2)



        Next

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


    Next
End Sub


Private Function GetSeriesName(ByRef bReader As BinaryReader) As String
    Dim enc As New System.Text.UTF8Encoding()
    Dim title As Byte()

    title = bReader.ReadBytes(128)
    Return enc.GetString(title)

End Function

现在这就是我在 C# 中的内容

    private void compareStandardFormat(ref BinaryReader file1breader,ref  BinaryReader file2breader,
                                        ref FileStructure firstfile,ref FileStructure secondfile)
    {
        file1breader.ReadInt32();
        file2breader.ReadInt32();

        firstfile.numberofseries = file1breader.ReadInt32();
        secondfile.numberofseries = file2breader.ReadInt32();

        if (firstfile.numberofseries != secondfile.numberofseries)
        {
            writeToConsole("The number of Elements the two files do not match...Stopping");
            return;
        }

        for (int i = 0; i < firstfile.numberofseries - 1; i++)
        {
            StandardFormatFileSeries tempseriesdata1 = new StandardFormatFileSeries();
            StandardFormatFileSeries tempseriesdata2 = new StandardFormatFileSeries();

            tempseriesdata1.standardnumofelements  = file1breader.ReadInt32();
            tempseriesdata1.standardseriesname  = getSeriesName(ref file1breader).Trim();

            tempseriesdata2.standardnumofelements = file2breader.ReadInt32();
            tempseriesdata2.standardseriesname = getSeriesName(ref file2breader).Trim();

            for (int j = 0; j < tempseriesdata1.standardnumofelements - 1; j++)
            {
                StandardFormatFileElement tempElementData1 = new StandardFormatFileElement();

                tempElementData1.standardx_timevalue  = Convert.ToString (file1breader.ReadSingle());
                tempElementData1.standardy_samplevalue = Convert.ToString(file1breader.ReadSingle());

                tempseriesdata1.standarddataelements.Add(tempElementData1);
            }

            for (int k = 0; k < tempseriesdata2.standardnumofelements - 1; k++)
            {
                StandardFormatFileElement tempElementData2 = new StandardFormatFileElement();

                tempElementData2.standardx_timevalue = Convert.ToString(file2breader.ReadSingle());
                tempElementData2.standardy_samplevalue = Convert.ToString(file2breader.ReadSingle());

                tempseriesdata2.standarddataelements.Add(tempElementData2);
            }

            firstfile.standardseriesdata.Add(tempseriesdata1);
            secondfile.standardseriesdata.Add(tempseriesdata2);
            }

    }

    private string getSeriesName(ref BinaryReader bReader)
    {
        UTF8Encoding enc = new UTF8Encoding();
        byte[] title;

        title = bReader.ReadBytes(128);

        return enc.GetString(title);

    }

VB 方式确实可以正确读取二进制读取器并正确索引到下一个位置……而 C# 方式则不能。它在第一次迭代后就失去了踪迹……为什么???

请帮忙。

最佳答案

我认为这些是不同的:

For i As Integer = 0 To firstFile.numberOfSeries - 1
for (int i = 0; i < firstfile.numberofseries - 1; i++)

我认为直接的 C# 翻译是:

for (int i = 0; i <= firstfile.numberofseries - 1; i++)

或更好:

for (int i = 0; i < firstfile.numberofseries; i++)

关于c# - 我已经用 vb 和 C# 编写了确切的代码,但它们的工作方式并不相同……逻辑是相同的……我希望,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3450727/

相关文章:

c# - 首先将 READ_COMMITTED_SNAPSHOT 与 EF 代码结合使用 5

c# - GZipStream zip文件无效或损坏

c# - 静态类以什么方式隐式抽象?

c# - 识别按钮并自动加载其目标的简单方法

c# - 数据网格 asp.net 中的超链接列

c# - 如何在 C# 中实现进度条?

mysql - VB.NET - MySQL 如何在应用程序强制关闭时终止来自 MySQL 服务器的查询

c# - 在依赖于同一类其他属性的属性中强制执行适当值的公认方法是什么?

c# - 字符串格式一位小数

vb.net - 如何将属性数据绑定(bind)到 Wpf 中的文本框