7

I downloaded FileHelpers from nuget but I am not sure if this feature does not exist or if I don't have the right version or what.

I been looking around and it seems that FileHelpers may have an attribute to specify the field order.

I downloaded this one however when I was looking in nuget there seems to be another version

Community
  • 1
  • 1
chobo2
  • 75,304
  • 170
  • 472
  • 780
  • Based on the code in the bitbucket repository it looks like the `FieldOrderAttribute` has been in there since 03/02/2010 which is **much** older than the package you have installed. – M.Babcock Jan 12 '12 at 00:35
  • Also the more active [SourceForge repository](http://filehelpers.svn.sourceforge.net/viewvc/filehelpers/trunk/FileHelpers/Attributes/FieldOrderAttribute.cs?view=log) shows the file has existed since at least March 2010. – M.Babcock Jan 12 '12 at 00:44
  • So the nuget ones are way out of date then? – chobo2 Jan 12 '12 at 01:41
  • The nuget package you installed is dated Jan 2011 so I would guess they include the functionality. If it isn't in there then I would bet so. – M.Babcock Jan 12 '12 at 01:43
  • 2
    HI there, the problem with NuGet is that we (The FileHelpers developers) don't create the package, I don't know who do it, so we can add the last beta versions :( You can download the last stable build from here: http://teamcity.codebetter.com/viewLog.html?buildId=lastSuccessful&buildTypeId=bt66&tab=artifacts&guest=1 Cheers – Marcos Meli Jan 12 '12 at 12:41
  • @MarcosMeli - well since your on the team can't you guys make an offical nuget package? I mean there is already 2 made might as well call make it 3 and have yours as the official one. Or get there removed and only have yours. It's not good that these people have unofficial ones that never get updated as it can confuse user and if they are like me and try to use nuget for everything they might not notice until they start looking for features and find they do exist. – chobo2 Jan 12 '12 at 16:59
  • @chobo2 You are right, I want to create the package for version 3.0 after vacations in a month or so, we will ask the nuget admins to remove the others or give us admin control of them – Marcos Meli Jan 13 '12 at 15:36
  • Cool. Can't wait to see what 3.0 brings to the table. Can you update this post when it comes out? – chobo2 Jan 13 '12 at 17:17
  • @MarcosMeli - how can I submit a patch for Filehelpers? I tried via email and sourceforge trac. Not required, but if you could move (or sync) to github, I can then just submit pull requests. :) – James Manning Jan 15 '12 at 15:30

1 Answers1

8

Firstly, the FieldOrder attribute does not exist in FileHelpers 2.0. In FileHelpers 2.9.9 (also available via NuGet), the attribute exists but if you specify it for any field, you must specify it for all fields. In general, however, use of the attribute is no necessary, since the order of the fields is defined by the format.

When using FileHelpers you provide a class to describe your format, e.g.,

[DelimitedRecord("|")] 
public class Order 
{ 
   // First field
   public int OrderID; 

   // Second field
   public string CustomerID; 

   // Third field
   [FieldConverter(ConverterKind.Date, "ddMMyyyy")]   
   public DateTime OrderDate;    
}

This describes a format with three fields, separated by vertical bars. If you like, it is the specification of the format. Once defined you can use it to import and export:

FileHelperEngine engine = new FileHelperEngine(typeof(Order)); 

// To read use: 
Order[] orders = engine.ReadFile("FileIn.txt") as Order[]; 

// To write use: 
engine.WriteFile("FileOut.txt", orders); 

So, if you want your fields in a different order, you should modify your Order class.

Now if you really wanted to, (with FileHelpers 2.9.9), you could change the order of the fields as follows:

[DelimitedRecord("|")] 
public class Order 
{ 
   // Third field
   [FieldOrder(3)]
   public int OrderID; 

   // Second field
   [FieldOrder(2)]
   public string CustomerID; 

   // First field
   [FieldOrder(1)]
   [FieldConverter(ConverterKind.Date, "ddMMyyyy")]   
   public DateTime OrderDate;    
}

but it is cleaner to avoid the use of the FieldOrder attribute and modify the order of the fields within the class instead.

On the other hand, if you need to specify the field order at runtime, you should build the Order class at using runtime records. You can use a string

Type orderType = ClassBuilder.ClassFromString(stringContainingOrderClassInCSharp); 

FileHelperEngine engine = new FileHelperEngine(orderType); 
Order[] orders = engine.ReadFile("FileIn.txt") as Order[]; 

Or you can use a ClassBuilder:

DelimitedClassBuilder cb = new DelimitedClassBuilder("Order");
// First field
cb.AddField("OrderID", typeof(int));
// Second field
cb.AddField("CustomerID", 8, typeof(string));
// Third field
cb.AddField("OrderDate", typeof(DateTime));
cb.LastField.Converter.Kind = ConverterKind.Date; 
cb.LastField.Converter.Arg1 = "ddMMyyyy";

engine = new FileHelperEngine(cb.CreateRecordClass());
Order[] orders = engine.ReadFile("FileIn.txt") as Order[]; 

You can use whatever logic you like in order to add your fields in the necessary order.

shamp00
  • 10,504
  • 3
  • 31
  • 76
  • What is the difference? FileHelpers 2.9.9 - This version seems to be missing this attribute "FieldTitle" (what was in one of the links I linked too) – chobo2 Jan 12 '12 at 16:54
  • 1
    FileHelpers 2.0.0.0 was developed between 2005 and 2007 and is stable and has lots of documentation. The more recent version 2.9.9 has less documentation and I'm not sure what the new features are. `FieldTitle` has been replaced with an `engine.HeaderText` property which you can set as explained in [the answer to your linked post](http://stackoverflow.com/a/4827625/1077279) – shamp00 Jan 12 '12 at 17:52
  • I was also looking at your code and realized your not using properties. Can you not use properties. I see that automatic properties do not work with FieldOrder – chobo2 Jan 12 '12 at 17:57
  • No FileHelpers classes (in version 2.0 at least) do not work with properties. They use public fields instead. – shamp00 Jan 12 '12 at 18:06
  • can I use property with a backing? Also I am not sure if this will have to be a new question but I am having problems with my header row. "Error Converting '"Date"(my propertyName)' to type: 'DateTime'. There are less chars in the Input String than in the Format string: 'mmddyyyy'" – chobo2 Jan 12 '12 at 18:15
  • No you cannot use properties in the FileHelpers classes. (The FileHelpers classes are for describing a format, not for any logic you might want to put in a getter or setter.) If you need properties, you should enumerate the objects returned by engine.ReadFile and copy the imported fields to some other class's properties. Are you trying to import or export? If you are importing you should not need to worry about the header - just use `[IgnoreFirst(1)]` to skip the first line altogether. – shamp00 Jan 12 '12 at 18:23
  • I always make properties(hence why they got automatic properties) even if there is no logic as I thought that was best practice). It seems that fully backed properties do work though. However [IgnoreFirst(1)] does not seem to work(even if I use field variables). It does not want to skip the first record. – chobo2 Jan 12 '12 at 18:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6662/discussion-between-shamp00-and-chobo2) – shamp00 Jan 12 '12 at 18:32
  • @shamp00 Old post, but your `ClassBuilder` example casts run time class "Order" to compile time class `Order`. They are different classes and `as Order[]` will always return null. I'm not aware of any straight forward way of getting back compile time class rows (which would be ideal to do generics etc.), although there is an [FH issue on this](http://github.com/MarcosMeli/FileHelpers/issues/122). – Kristian Wedberg Feb 24 '17 at 11:40