Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

Can anybody tell me how to check for data in an array with a fixed length & And I should check data only for few indices's and neglect the rest even if the array length is more than the index specified.

Like I have an array with fixed length of 72, since the index is user defined and is varied from 4-72 I have written conditions for all the 72 values of the array. So in the 1ast iteration I should check for some condition only till 12 indices length of that array and in the next iteration the index may be 36 so I need to check only 1st last 36 indices's of that array n neglect the rest. This index is user defined it may vary from 4-72. So how to handle this?

The code is shown below:

C#
int[] errorIndexOne = new errorIndexOne[72];

if (errorIndexOne[0] == 1)
           {
               if (btn[0].BackColor == Color.LimeGreen)
               {
                   btn[0].BackColor = Color.Red;
               }
               else
               {
                   btn[0].BackColor = Color.LimeGreen;
               }
           }

           if (errorIndexOne[1] == 1)
           {
                if (btn[1].BackColor == Color.LimeGreen)
                {
                    btn[1].BackColor = Color.Red;
                }
                else
                {
                    btn[1].BackColor = Color.LimeGreen;
                }
           }

           if (errorIndexOne[2] == 1)
           {
               if (btn[2].BackColor == Color.LimeGreen)
               {
                   btn[2].BackColor = Color.Red;
               }
               else
               {
                   btn[2].BackColor = Color.LimeGreen;
               }
           }

           if (errorIndexOne[3] == 1)
           {
               if (btn[3].BackColor == Color.LimeGreen)
               {
                   btn[3].BackColor = Color.Red;
               }
               else
               {
                   btn[3].BackColor = Color.LimeGreen;
               }
           }

           if (errorIndexOne[4] == 1)
           {
               if (btn[4].BackColor == Color.LimeGreen)
               {
                   btn[4].BackColor = Color.Red;
               }
               else
               {
                   btn[4].BackColor = Color.LimeGreen;
               }
           }

           if (errorIndexOne[5] == 1)
           {
               if (btn[5].BackColor == Color.LimeGreen)
               {
                   btn[5].BackColor = Color.Red;
               }
               else
               {
                   btn[5].BackColor = Color.LimeGreen;
               }
           }

.
.
.
.
.
.


         if (errorIndexOne[72] == 1)
           {
               if (btn[72].BackColor == Color.LimeGreen)
               {
                   btn[72].BackColor = Color.Red;
               }
               else
               {
                   btn[72].BackColor = Color.LimeGreen;
               }
           }
Posted
Updated 19-Jun-13 0:55am
v2

Replace ur code with this:

C#
int[] errorIndexOne = new errorIndexOne[72];

      for(int i=0; i<72; i++){
           if (errorIndexOne[i] == 1)
           {
               if (btn[i].BackColor == Color.LimeGreen)
               {
                   btn[i].BackColor = Color.Red;
               }
               else
               {
                   btn[i].BackColor = Color.LimeGreen;
               }
           }
      }
 
Share this answer
 
Try this one:-

C#
int[] errorIndexOne = new errorIndexOne[72];

      for(int i=0; i<errorindexone.length;i++)
{
         if (errorIndexOne[i] == 1)
           {
               if (btn[i].BackColor == Color.LimeGreen)
               {
                   btn[i].BackColor = Color.Red;
                   break;
               }
               else
               {
                   btn[i].BackColor = Color.LimeGreen;
               }
           }
      }


Thanks
Prince Sharma
 
Share this answer
 
v3
try this

C#
int[] errorIndexOne = new errorIndexOne[72];

      for(int i=0; i<72; i++){
           if (errorIndexOne[i] == 1)
           {
               if (btn[i].BackColor == Color.LimeGreen)
               {
                   btn[i].BackColor = Color.Red;
               }
               else
               {
                   btn[i].BackColor = Color.LimeGreen;
               }
           }
      }
 
Share this answer
 
Comments
Thanks7872 19-Jun-13 8:20am    
Dont post duplicate answers. Comment to update the answer if needed.
samadhan_kshirsagar 19-Jun-13 8:40am    
I also guessed that answer
Thanks7872 19-Jun-13 8:42am    
Since it has already been posted beofre yours,you are not supposed to post it again.
samadhan_kshirsagar 19-Jun-13 9:21am    
okay.....thanks

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