Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

For loop takes the the max count

 private List<SurveyDetail> GetSurveyDetails()
    {
        List<SurveyDetail> surveyDetails = new List<SurveyDetail>();
        SurveyDetail detail = new SurveyDetail();
        int cid = 0;
        for (int i = 1; i < 3; i++)
        {
           detail.choiceId = "1";
           detail.choiceDesc = "tesT";
           detail.questionId = i.ToString();
           surveyDetails.Add(detail);
        }
        return surveyDetails;
    }


 public class SurveyDetail
    {
        public string questionId { get; set; }
        public string choiceId { get; set; }
        public string choiceDesc { get; set; }
    }

when I run the code it question Id always gives me the last number of i that was run for example, in this case, it gives me 2. It gives me 2 on both counts.
Where I want the questionid to be 1 in the first count and 2 in the second.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You should move SurveyDetail initialization into the for body

 private List<SurveyDetail> GetSurveyDetails()
    {
        List<SurveyDetail> surveyDetails = new List<SurveyDetail>();
        int cid = 0;
        for (int i = 1; i < 3; i++)
        {
           SurveyDetail detail = new SurveyDetail();//<==NOTE THIS
           detail.choiceId = "1";
           detail.choiceDesc = "tesT";
           detail.questionId = i.ToString();
           surveyDetails.Add(detail);
        }
        return surveyDetails;
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading