我已经用 vb 和 C# 编写了确切的代码,但它的工作方式不一样...逻辑是相同的...我希望

发布于 2024-09-13 21:30:44 字数 4984 浏览 10 评论 0原文

对 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# 方式则不能。第一次迭代后它就失去了踪迹......为什么???

请帮忙。

Kind of new to C# and trying to broaden my abilities a bit. I have this code in 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

Now this is what i have in 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);

    }

The VB way does correctly read the binary reader and correctly index to the next position...the C# way doesnt. It loses track after the first iteration...why???

please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天的雪花 2024-09-20 21:30:44

我认为这些是不同的:

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++)

I think these are different:

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

I think that the direct C# translation is:

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

or better:

for (int i = 0; i < firstfile.numberofseries; i++)
旧城烟雨 2024-09-20 21:30:44

我不熟悉 VB,但看起来您确实在集合末尾停止了,因为您既从元素计数中减去 1,又在循环中使用“小于”(<) 运算符。这意味着您的循环将在最后一项之前退出。

我建议从循环条件中删除“- 1”。或者,您可以切换到“小于或等于”(<=),但在这种情况下前者更有意义。

I'm not familiar with VB, but it does appear you are stopping short of the end of your collections as you are both subtracting 1 from the count of elements and using the 'less than' (<) operator in your loop. This means that your loops will exit before the last item.

I would suggest removing the '- 1' from the loop conditions. Alternatively you can switch to 'less than or equal to' (<=) but the former makes more sense in this case.

帅哥哥的热头脑 2024-09-20 21:30:44

我认为这是 for 运算符?

在 C# 中

for (int i = 0; i < 10; i++)
{
   // inner loop
}

这将运行 10 次,

在 vb 中

For i As Integer = 0 To 10
   rem inner loop
Next

这将运行 11 次。

因此解决方法是:

放弃 C# 循环检查上的“- 1”

为了清楚起见,我将 VB 代码编写为

For i As Integer = 1 To firstFile.numberOfSeries

,将 C# 代码编写为

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

,因为在每种情况下,firstFile.numberOfSeries 都是循环运行的次数。

I think it's the for operator?

In C#

for (int i = 0; i < 10; i++)
{
   // inner loop
}

This will run 10 times

in vb

For i As Integer = 0 To 10
   rem inner loop
Next

This will run 11 times.

So the fix is:

Ditch the "- 1" on the C# loop check

For clarity, I'd write the VB code as

For i As Integer = 1 To firstFile.numberOfSeries

and the C# code as

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

because, in each case, firstFile.numberOfSeries is the number of times the loop is run.

ζ澈沫 2024-09-20 21:30:44

尝试更改:

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

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

不知道它定义的结构类型的情况下,我猜测当您添加到第一个文件/第二个文件结构时,循环会变得不正常。

另外,请接受解决您的挑战的答案。很高兴知道它已经被修复,而不是猜测并试图深入挖掘一个不存在的问题。 :)

try changing:

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

to

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

Without knowing what type of structure it is defined as I am guessing that when you add to the firstfile/secondfile structures the loop is thrown out of whack.

Also, please accept the answer that fixes your challenge. It is good to know it is already fixed instead of guessing and trying to dig deeper into a non-existent problem. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文