foreach loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: foreach loop

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    8

    foreach loop

    Hi All,

    I have something like this..

    foreach(Location location in locationList)
    {
    studentList.Add(new StudentDataList() {Locationcode = location .locationcode};
    }
    IntializeStudentDataValues(studentList);

    Private void IntializeStudentDataValues(List<StudentDataList> studentList)
    {
    if(studentList != null)
    {
    foreach(var student in studentList)
    {
    switch(student .StudentName.ToUpper())
    {
    case "A":
    //Do Something
    Case "B":
    //DoSomething
    }
    }
    }
    }


    i am having "IntializeStudentDataValues" for having foreach loop for "studentList". Instead of this i would like to have one foreach where i have it for locationList. I want to write one foreach for both cases. I want perform
    switch case in this foreach(Location location in locationList).

    Can anybody help me on this?

    Thanks,
    genith

  2. #2
    Join Date
    Aug 2006
    Posts
    132

    Re: foreach loop

    Please put code inside a code block ("[ c o d e ]" (no spaces) in front of it and "[ / c o d e ]" (again, no spaces) behind it), and please indent your code. Without those, it is very difficult to read your code. I have done it below in hope that if I can read your code I can understand what you are trying to do.

    Code:
    foreach(Location location in locationList)
    {
         studentList.Add(new StudentDataList() {Locationcode = location .locationcode};
    }
    IntializeStudentDataValues(studentList);
    
    Private void IntializeStudentDataValues(List<StudentDataList> studentList)
    {
        if(studentList != null)
        {
            foreach(var student in studentList)
            {
                switch(student .StudentName.ToUpper())
                {
                    case "A":
                    //Do Something
                    Case "B":
                    //DoSomething
                }
            }
        }
    }
    You said:

    "i am having "IntializeStudentDataValues" for having foreach loop for "studentList". Instead of this i would like to have one foreach where i have it for locationList. I want to write one foreach for both cases. I want perform
    switch case in this foreach(Location location in locationList)."

    I still can't figure it out. It seems to me you're doing what you need to do. You read the input data into a list, and then you walk through the list to process it. You have to choose between A and B, and there's no simpler way to do it then the way you are doing it. Some people might not bother with the list, but maybe you need it later in your program, and so you decided to make sure it's there. I can accept that.

    Perhaps you can post some pseudocode that shows what you're trying to do. Pseudocode is just writing something that looks like C# code, but it will never be compiled. It just shows the steps you are trying to get to happen. It can be quite helpful.

    Good luck!

    RobR
    Last edited by RobR; October 21st, 2012 at 10:03 PM. Reason: attempt to explain code block without having it processed as a code block

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