3

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.

Sean P
  • 949
  • 4
  • 22
  • 41
  • 1
    What exactly do you mean by "loses track"? – CubanX Aug 10 '10 at 15:43
  • 5
    None of those parameters should be `ByRef` / `ref`. – SLaks Aug 10 '10 at 15:44
  • 5
    Some unasked for advice: Switch option strict on for your VB.Net code, get that running correctly, then port to C#. – Binary Worrier Aug 10 '10 at 15:47
  • Actually do i need them to be byref or the reader will lose its place in reading the file. Also i couldnt mark as answer cause it told me to wait 7 mins...i had an answer within 30 seconds. – Sean P Aug 10 '10 at 17:37
  • @Sean: No, the ref is not needed for that. In .NET an object (the reader) is _always_ passes as a reference. Your code passes reference-by-reference, needlessly complex. – H H Aug 10 '10 at 19:54
  • im confused why the need for the "ref" keyword then? Was I wrong thinking everything is passed by val? Time to google i guess :( – Sean P Aug 11 '10 at 23:35
  • http://msdn.microsoft.com/en-us/library/8f1hz171.aspx Im reading articles like this... why have a ref keyword when you dont need it?! – Sean P Aug 11 '10 at 23:39
  • http://www.codeproject.com/KB/cs/parameter_object_by_value.aspx Now im totally confused... whos right. This article says that default is pass by value? – Sean P Aug 11 '10 at 23:46

4 Answers4

17

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++)
Douglas
  • 36,802
  • 9
  • 76
  • 89
5

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.

Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
4

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.

McKay
  • 12,334
  • 7
  • 53
  • 76
1

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. :)

Keith Barrows
  • 24,802
  • 26
  • 88
  • 134