views:

248

answers:

3

I am using the excellent FileHelpers library to process a fixed-length airline schedule file.

I have a date field, then a few fields later on in the record, a time field.

I want to combine both of these in the FileHelpers record class, and know there is a custom FieldConverter attribute. With this attribute, you provide a custom function to handle your field data and implement StringToField and FieldToString.

My question is: can I pass other fields (already read) to this customer FieldConverter too, so I can combine Date and Time together. FieldConverter has an implementation that allows you to refer to both a custom processing class AND 'other strings' or even an array of object. But, given this is done in the attribute definition, I am struggling to access this earlier-field reference.

[FieldFixedLength(4)]  
[FieldConverter(typeof(MyTimeConverter),"eg. ScheduledDepartureDate")]  
public DateTime scheduledDepartureTime;
+1  A: 

Hi Pete

In fact you can access previous fields because them are read in order but can be a bit strange for other reading the code.

Maybe you can implement the interface INotifyRead and on the method AfterRead do all the stuff

Your class must be something like:

public class YourRecord

...

[FieldFixedLength(6)]  
public string scheduledDepartureDate;

[FieldFixedLength(4)]  
public string scheduledDepartureTime;

[FieldIgnored]  
public DateTime scheduledDepartureDateTime;

public void AfterRead(EngineBase engine, string line)
{
    scheduledDepartureDateTime = CombineDateTime(scheduledDepartureDate, scheduledDepartureTime)                 
}

Hope this helpers

Cheers

MarcosMeli
A: 

Thanks so much Marcos - that AfterRead even will be just what the Dr ordered, except a) isn't the even AfterReadRecord, and b) it doesn't look like it's implemented for FileHelperAsyncEngine. Is this right, or have I missed something?

pete
Pete: You can download the last version from here http://teamcity.codebetter.com/repository/download/bt65/10605:id/FileHelpers_Release_2.5.0.zipthat has the events implemented for async
MarcosMeli
A: 

aaah brilliant Marcos. I went back to sync and everything was working well, but the performance was a bit odd given the sync full load of data. I'll try out the async version now. Thanks

Pete