3 Replies - 716 Views - Last Post: 14 November 2011 - 09:05 PM Rate Topic: -----

#1 squareCodeInaRoundApp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-October 07

What data type should I use for 3 repeating fields?

Posted 11 November 2011 - 09:46 AM

I am relearning C# to get experience at work and am unsure what to do. I am reading a binary file putting the fields of a record into an object. I am unsure what I should use for a section of data that gets repeated, as shown below.

1 byte field
2 byte field (length of data for next field below)
x byte field
(now rinse and repeat unknown number of times averaging around 40 times)

What should I read these repeated 3 fields into? Should it be a List due to ability to grow (but thought not because it would need 3 fields per element and it looks like a List only will do 1 field per element)? Maybe an Array, a Generic Collection, or what? And could you explain the reasoning for your answer?

Here is a sample of the class:

public class SAFLOGRecord
{
private Int16 Fld1; //2 bytes
private Int16 Fld2; //2 bytes
private Int16 Fld3; //2 bytes
private Int16 Fld4; //2 bytes, length of all repeating data below
???? (see layout above) ???
.....
}

Any help will be greatful!

Is This A Good Question/Topic? 0
  • +

Replies To: What data type should I use for 3 repeating fields?

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5465
  • View blog
  • Posts: 14,418
  • Joined: 18-April 07

Re: What data type should I use for 3 repeating fields?

Posted 11 November 2011 - 10:43 AM

Well if you are reading a binary file a byte at a time, the ideal structure would be an array of bytes. After all, this is what a stream is right? Bytes are short on storage (unlike a collection of objects) and they are easy to iterate over.

I am not exactly sure what is the complication or hesitation here. :)
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: What data type should I use for 3 repeating fields?

Posted 11 November 2011 - 10:54 AM

Personally I think your goal is to make a class for the data with properties that are more convenient to use.

As Martyr2 said, just read the stream/data using bytes.

But I think you are really asking "What should I do with the data after I get it?"

Make a class with one property being the data as bytes. This way you can always refer to it.

Then make meaningful properties that are easy to use throughout your program. If the structure of the data changes you don't have to change your entire program, just the parsing of the one object.


public class Widget
{
    public byte[] RawData;

    public int16 RecordNumber
    {
          return ByteToInt(RawData[0];
    }

    public int16 DataLength
    {
          return byteToInt(RawData[1];
    }

    public byte[] DataMinusHeader
    {
         //return all the bytes other than the first 8
    }

}

Was This Post Helpful? 0
  • +
  • -

#4 squareCodeInaRoundApp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-October 07

Re: What data type should I use for 3 repeating fields?

Posted 14 November 2011 - 09:05 PM

View PostMartyr2, on 11 November 2011 - 12:43 PM, said:

Well if you are reading a binary file a byte at a time, the ideal structure would be an array of bytes. After all, this is what a stream is right? Bytes are short on storage (unlike a collection of objects) and they are easy to iterate over.

I am not exactly sure what is the complication or hesitation here. :)


The hesitation that I had had to do with the unknown number of repeating fields. Right now I will try the array bit and will try to estimate the max number of entries. Work in progress...thanks guys.

View PosttlhIn`toq, on 11 November 2011 - 12:54 PM, said:

Personally I think your goal is to make a class for the data with properties that are more convenient to use.

As Martyr2 said, just read the stream/data using bytes.

But I think you are really asking "What should I do with the data after I get it?"

Make a class with one property being the data as bytes. This way you can always refer to it.

Then make meaningful properties that are easy to use throughout your program. If the structure of the data changes you don't have to change your entire program, just the parsing of the one object.


public class Widget
{
    public byte[] RawData;

    public int16 RecordNumber
    {
          return ByteToInt(RawData[0];
    }

    public int16 DataLength
    {
          return byteToInt(RawData[1];
    }

    public byte[] DataMinusHeader
    {
         //return all the bytes other than the first 8
    }

}


Just so that you know, I am using a Class to define the member fields. It was just a matter of the last portion (the repeating portion) that I was unsure of.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1