views:

338

answers:

1

I have been searching for a way to allow one element of my FileHelpers mappling class to be an array of specific length.

For instance, I have a class like this:

[DelimitedRecord(",")]
public class Example
{
    public string code;
    public int month;
    public int day;
    public double h1;
    public double h2;
    public double h3;
    public double h4;
}

The values h1-h4 would really make more sense as an array simply called 'h'. It would make processing the file a little easier as well. I also know that the file I am reading will always have these, and only these, fields in it.

Has anyone figured out a way to include arrays in your FileHelper mapping classes?

+1  A: 

I don't know anything about the tool in question, but (assuming it isn't a limitation of the tool) I really doubt the wisdom of public fields. Properties would also give you the opportunity to shim the values:

[DelimitedRecord(",")]
public class Example
{
    public string Code {get;set;}
    public int Month {get;set;}
    public int Day {get;set;}

    private readonly double[] h = new double[4];

    public double H1 {get {return h[0];} set {h[0] = value;}}
    public double H2 {get {return h[1];} set {h[1] = value;}}
    public double H3 {get {return h[2];} set {h[2] = value;}}
    public double H4 {get {return h[3];} set {h[3] = value;}}
}

Again - I have no idea if the tool would support this, but it would be a viable way of implementing it. Of course, the "h" values would do just as well (actually, slightly more efficient - no array on the heap and no de-reference) as direct members:

    public double H1 {get;set;}
    public double H2 {get;set;}
    public double H3 {get;set;}
    public double H4 {get;set;}
Marc Gravell