Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i have a situation where i am stuck

i have a switch case inside two nested for each loops. i want to get out of the inner loop if switch case is true.


C#
foreach(DataRow dr in dt.Rows)
  {
      foreach (DataColumn dc in dt.Columns) 
       {
           switch (dc.Ordinal)
                  {
                     case 0:
                           {
                            if( string.IsNullOrEmpty(dr[dc].ToString()))
                               {
                                 continue;
                               }  
                           }
                   }
       }
  }



What i need to do is if the switch case is true then i need to getout of the DataColumn loop and go to dataRow loop. But continue just takes me to dc loop again.
Posted
Updated 17-Sep-15 22:12pm
v3
Comments
Krunal Rohit 18-Sep-15 4:05am    
Use break instead.
Or set a boolean flag and based on your condition set it to true and at the end of the inner loop check it for flag, if its true, break it.

-KR
CPallini 18-Sep-15 4:08am    
My (virtual) 5.

C#
bool exitLoop;
foreach(DataRow dr in dt.Rows)
{
    foreach (DataColumn dc in dt.Columns)
     {
         switch (dc.Ordinal)
                {
                   case 0:
                         {
                          if( string.IsNullOrEmpty(dr[dc].ToString()))
                             {
                               exitLoop = true;
                               break;
                             }
                         }
                 }
           if (exitLoop) break;
           
     }
   
}
 
Share this answer
 
Hi,

use below sample

C#
bool isExists=false;
foreach(DataRow dr in dt.Rows)
  {
      foreach (DataColumn dc in dt.Columns)
       {
           switch (dc.Ordinal)
                  {
                     case 0:
                           {
                            if( string.IsNullOrEmpty(dr[dc].ToString()))
                               {
                                   isExists=true;
                                   break;
                               }
                           }
                   }
       }
  }


Continue, statement will continue the next statement, instead of that better to use break; it's breaks the current looping and give you the result set.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900