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

Threaded View

  1. #4
    Join Date
    Jul 2012
    Posts
    90

    Re: if condition in foreach loop

    Assuming you actually wanted to replace the if construct (instead of the foreach as stated in your reply to TGC - foreach is the best way to iterate through a List)...

    Code:
        foreach (var name in Students) {
            switch (name.StudentName.ToLower()) {
                case "john":
                    // Do something specific to John
                    break;
                case "alex":
                    // Do something specific to Alex
                    break;
                case "james":
                case "robert":
                    // Do something specific to both James and Robert
                    break;
                default:
                    // Do something if not John, Alex, James, or Robert
                    break;
            }
            // If you need - you can do something here common to all
        }
    Last edited by CGKevin; October 12th, 2012 at 08:17 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured