Changing field names within a loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Changing field names within a loop

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Changing field names within a loop

    I am working on a data conversion job which comes in '^' delimited with several rows per record depending on how many transactions they have made. The first field in each transaction row starts with "0411". There could be up to 27 transaction rows per record, perhaps more in futire data files. I think there is a much simpler way of reading in the transaction rows so that it just loops round until the final transaction.

    I'd be happy creating the actual loop, but the bit I am stuck on is this - can you loop round & add a sequential number to field names? Each transaction has a date, a balance, an 'in' & an 'out'. For example, could I change the below to a loop which changes the number on the end of the field names (e.g. process DateYear1 the first time round and then change it to DateYear2 the next time round the loop?).

    I assume I'd have to create all of them in the class still, but if there's a quicker way than copying the below 27 times and changing the number I'd like to try it!

    Thanks in advance,

    Greg




    if (fields[0] == "0411")

    {




    data.RowType = fields[0];

    data.DateYear1 = fields[4];

    data.DateMonth1 = fields[5];

    data.DateDay1 = fields[6];

    data.Description1 = fields[7];

    data.In1 = fields[8];

    data.Out1 = fields[9];

    data.Balance1 = fields[10];

    oneLine = reader.ReadLine();

    fields = oneLine.Split('^');

    }

  2. #2
    Join Date
    Jul 2012
    Posts
    90

    Re: Changing field names within a loop

    Create a class to hold a row of data...

    Code:
    public class DataTransaction {
        private string m_RowType = string.Empty;
        private string m_Year = string.Empty;
        private string m_Month = string.Empty;
        private string m_Day = string.Empty;
        private string m_Description = string.Empty;
        private string m_In = string.Empty;
        private string m_Out = string.Empty;
        private string m_Balance = string.Empty;
    
        public string RowType { get { return m_RowType; } set { m_RowType = value; } }
        public string Year { get { return m_Year; } set { m_Year = value; } }
        public string Month { get { return m_Month; } set { m_Month = value; } }
        public string Day { get { return m_Day; } set { m_Day = value; } }
        public string Description { get { return m_Description; } set { m_Description = value; } }
        public string In { get { return m_In; } set { m_In = value; } }
        public string Out { get { return m_Out; } set { m_Out = value; } }
        public string Balance { get { return m_Balance; } set { m_Balance = value; } }
    }
    Create a list of that class to hold the transactions for a record...

    Code:
    IList<DataTransaction> transactions = new List<DataTransaction>();
    Now as you read in a line, split it as you were, set the values in a new DataTransaction and add it to the List. Once you have all of the transactions for a record in the list, use foreach to iterate through the loop and write the data to your database.

    I made all fields a string just for illustration, but you should probably make them the appropriate types and do the conversions as you write the data to each instance of the class.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center