Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:06:55+00:00 2026-06-12T01:06:55+00:00

I want to add new properties to an object based on loop iterations, is

  • 0

I want to add new properties to an object based on loop iterations, is this possible in .Net? The reason I want to do this is I am looping through rows in an excel spreadsheet and for every successfully read row I want to create a new dynamic object property. So when the loop is complete I can simply pass the object to a method and log all the records.

See below for code so far:

    protected void ReadData(string filePath, bool upload)
    {
        StringBuilder sb = new StringBuilder();

        #region upload
        if (upload == true) // CSV file upload chosen
        {

            using (CsvReader csv = new CsvReader(new StreamReader(filePath), true)) // Cache CSV file to memory
            {
                int fieldCount = csv.FieldCount; // Total number of fields per row
                string[] headers = csv.GetFieldHeaders(); // Correct CSV headers stored in array 

                SortedList<int, string> errorList = new SortedList<int, string>(); // This list will contain error values
                ORCData data = new ORCData();

                bool errorFlag = false;
                int errorCount = 0;

                // Check if headers are correct first before reading data
                if (headers[0] != "first name" || headers[1] != "last name" || headers[2] != "job title" || headers[3] != "email address" || headers[4] != "telephone number" || headers[5] != "company" || headers[6] != "research manager" || headers[7] != "user card number")
                {
                    sb.Append("Headers are incorrect");
                }

                else
                {
                    while (csv.ReadNextRecord())
                        try
                        {
                            //Check csv obj data for valid values
                            for (int i = 0; i < fieldCount; i++)
                            {
                                if (i == 0 || i == 1) // FirstName and LastName
                                {
                                    if (Regex.IsMatch(csv[i].ToString(), "^[a-z]+$", RegexOptions.IgnoreCase) == false) //REGEX letters only min of 5 char max of 20
                                    {
                                        errorList.Add(errorCount, csv[i]);
                                        errorCount += 1;
                                        errorFlag = true;
                                        string text = csv[i].ToString();
                                    }
                                }
                            }

                            if (errorFlag == true)
                            {
                                sb.Append("<b>" + "Number of Error: " + errorCount + "</b>");
                                sb.Append("<ul>");
                                foreach (KeyValuePair<int, string> key in errorList)
                                {
                                    sb.Append("<li>" + key.Value + "</li>");
                                }
                            }
                            else // All validation checks equaled to false. Create User
                            {

                                string message = ORCLdap.CreateUserAccount(rootLDAPPath, svcUsername, svcPassword, csv[0], csv[1], csv[2], csv[3], csv[4], csv[5], csv[7]);
                                // TODO: Add to object here
                                sb.Append(message);
                                //sb.Append("<b>New user data uploaded successfully</b>");
                            }

                        }// end of try



                        catch (Exception ex)
                        {
                            sb.Append(ex.ToString());
                        }

                        finally
                        {
                            lblMessage.Text = sb.ToString();
                            sb.Remove(0, sb.Length);
                            hdnRdoSelection.Value = "1";
                        }
                }
            }

        }
        #endregion

I have never tried to do this before so I am not sure how I would approach it but any help would be great. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:06:56+00:00Added an answer on June 12, 2026 at 1:06 am

    I want to add new properties to an object based on loop iterations, is this possible in .Net?

    Sort of. You probably want to use ExpandoObject, treating it as an IDictionary<string, object> when you’re adding the properties.

    Having said that, if you’re not going to try to use those properties as properties later, do you actually need them to be properties at all? Why not just use a Dictionary<string, object> to start with?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want add new Feed item on entity persist and update. I write this
how can I add two new properties to the sealed Arc class. I want
I want to add new obj of JSON like: 128: { Msg: [{ me:
I am working with an NSOperationQueue and I want to add new NSOperations to
I've following Grid to display data, now when I want to add new record
I want to test Add new item activity in my application. When user fills
I want to add a new property bool IsValid to my class below. I
I want to add a new row to my JSON when the user clicks
I want to add a new tab page for every newly opened form. Example:
I want to add a new filter to the Project Explorer, to hide from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.