Click here to Skip to main content
15,920,102 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Can anybody tell me how to save 2 different datas in an array in a single iteration?
Like for eg: I have an array which gives me fieldindex & a timer which is set when any of the if conditions are satisfied. The problem is when the instruction enters the for loop & if more than 1 if conditions passes then the value in fieldindex will be over written, loosing my previous data. The below code may give a clear picture.
C#
   for(int i=0; i<24; i++)
    {
 if (SID >= 10 && SID <= 11)
       {
           fieldindex1[i] = 1;
           timer1.Start();
       }


if (SID >= 12 && SID <= 13)
       {
           fieldindex1[i] = 2;
           timer1.Start();
       }

if (SID >= 14 && SID <= 15)
       {
           fieldindex1[i] = 3;
           timer1.Start();
       }
   }

So their is an array by name fieldindex1[] & while scanning it might enter 1st if condition or any 1 of the if condition or all the if conditions. The above approach will not fetch me the required data.. I want it be done in some other way.. If it enters the 1st if condition then the fieldindex[i] value should be loaded to it n further incremented to accommodate new value. Also i dont want to use different arrays for different styles.
Posted
Updated 20-May-13 1:47am
v4
Comments
CHill60 20-May-13 7:10am    
I'm struggling to understand exactly what you want to happen ... if SID = 11 for example you are setting all 24 fieldindex1[] entries to 1 and starting timer1 24 times - is that really what you want?
Jagadisha_Ingenious 20-May-13 7:31am    
@CHill60: I want to add values to the fieldindex1[] with their respective data & start the timer only once if any of the if conditions are satisfied. If incase more than 1 if conditions are satisfied i should save for ex: fieldindex1[0] = 1; & fieldindex1[1] = 2, if 1st & 2nd if conditions are true. I don't want to use for loop here instead a better way to implement this would be good, since in the same iteration if 2 of the if conditions are satisfied i cannot save both values in the array as it will be over written, where i will loose the previous value.
R.B. 20-May-13 7:39am    
You say that if more than one condition passes, then field index will be overwritten. The code appears to be written so that only one of the conditions should be true per iteration. On the third condition, though, the variable used is SI, not SID - this could cause two conditions to be true.
Jagadisha_Ingenious 20-May-13 7:51am    
@R.B. Sorry it is also SID in the third if condition too. And i don't want that way if i initialize the counter instead of forloop & do it also the same problem persists so how to eliminate it..?

Hello!!!

I couldn't understand your requirement clearly... however you can preserve your data using List

C#
List<int> fieldindex1 = new List<int>();
            for (int i = 0; i < 24; i++)
            {
                if (SID >= 10 && SID <= 11)
                {
                    fieldindex1.Add(1);
                    //fieldindex1[i] = 1;
                    timer1.Start();
                }


                if (SID >= 12 && SID <= 13)
                {
                    fieldindex1.Add(2);
                    //fieldindex1[i] = 2;
                    timer1.Start();
                }

                if (SI >= 14 && SID <= 15)
                {
                    fieldindex1.Add(3);
                    //fieldindex1[i] = 3;
                    timer1.Start();
                }
            }
 
Share this answer
 
int count=0;
for(int i=0; i<24; i++)
{
if (SID >= 10 && SID <= 11)
{
fieldindex1[count++] = 1;
timer1.Start();
}


if (SID >= 12 && SID <= 13)
{
fieldindex1[count++] = 2;
timer1.Start();
}

if (SI >= 14 && SID <= 15)
{
fieldindex1[count++] = 3;
timer1.Start();
}
}
 
Share this answer
 
v2

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