Reputation: 2010
I have been searching for a way to allow one element of my FileHelpers mapping 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?
Upvotes: 4
Views: 1737
Reputation: 11326
FileHelpers record classes require public fields. The record class should not be considered as a normal C# class that should follow best coding practices; rather it is just a syntax for describing an import file's structure.
The recommended procedure with FileHelpers would be to loop through the resulting Example[]
array and map the fields you need to a more normal class (with properties instead of public fields). At this point you can copy your H1-H4 values to an array property instead.
Upvotes: 1
Reputation: 1064244
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;}
Upvotes: 0