I'm having trouble finding a nice way to parse out an object I have in C# in a fixed width format. The object has various properties. I found that the cleanest way to parse out the data itself was to use string formatting, so I need the lengths of each field. I currently have a static class set up like this:
public static class LENGTHS
{
public static int FIRST_NAME = 15;
public static int LAST_NAME = 25;
public static int ADDRESS1 = 25;
etc...
}
The lengths are then placed into an array. So like [15, 25, 25]
Then the data fields are placed into another array in the same order:
string[] info = { obj.Firstname, obj.LastName, obj.Address1, etc...};
I should also mention that every time the info string will be the exact same, and will not change overall unless I change only the order, in which case it will be changed every time.
They are then passed into a parser I made which finds the length of the field, and how long it should be from the length array, and inserts blank spaces accordingly using String.Format.
FixedWidthParser(info, Lengths);
The issue then is having to maintain the order for both of these arrays. In the event I have to change the order, I'd have to change the order of both arrays.
Am I going about this the wrong way? Is there a better way to do this? If so, can someone point me in the right direction?