Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code is as follow:

C#
private void IzvadaJautajumuAtbildes()
        {
            var something =
                from x in channel2.GetJautajumuSaraksts(SelectedTestID)
                select new
                {
                    id = x.QuestionID, ; //1,2,4,5,7
                    name = x.Name
                };
            foreach (var i in something)
            {
                lblQuestion.Text = i.nosauk;
                currentQuestionID = i.id
                answer = channel2.GetAnswer(currenQuestionID);
                foreach (var i2 in answer)
                {
                    btnAtbilde1.Text = i2.Answer1;
                    btnAtbilde2.Text = i2.Answer2;
                    btnAtbilde3.Text = i2.Answer3;
                    btnAtbilde4.Text = i2.Answer4;
                }
                break;
            }
         }


In this code, on button2_Click event, when I click on button2 once, everything is ok, I get question & answers.
How can I iterate IzvadaJautajumuAtbildes() again, but with the next id.
2 not 1.
Posted
Updated 8-Dec-10 14:49pm
v2

You can't change the iterator used in a foreach statement because it's read-only.

What you could do is create a property that indicates "used", use Linq to build a collection of items that aren't "used", and then use foreach on that new collection.
 
Share this answer
 
Comments
valza 9-Dec-10 1:07am    
I did something like this.
private ArrayList jautId = new ArrayList();
private ArrayList jautNosakums = new ArrayList();
.....
.....
var kautKas =
from x in channel2.GetJautajumuSaraksts(SelectedTestID)
select new
{
id = x.JautajumuID,
nosauk = x.Jautajums
};
foreach (var i in kautKas)
{
jautId.Add(i.id);
jautNosakums.Add(i.nosauk);
}
....
....
if(atbilzuSkaits < jautajumi.Count())
{
lblJautajums.Text = jautNosakums[atbilzuSkaits].ToString();
IzdzesTextNoAtbPogam();

atbildes = channel2.GetAtbildes((int)(jautId[atbilzuSkaits]));
// MessageBox.Show(jautId[atbilzuSkaits].ToString());

foreach (var i2 in atbildes)
{
btnAtbilde1.Text = i2.Atbildes1;
btnAtbilde2.Text = i2.Atbilde2;
btnAtbilde3.Text = i2.Atbilde3;
......
......

......

Is it good or bad, to do like this?!?!?!?! everything working after, about 2 hours of coding.
this is excelent forum. smart people and quick answers
Thanks! :)
Surround the whole thing by with a for loop

private void IzvadaJautajumuAtbildes(int[] testValues)
{
  for (int i = 0; i < testValues.Length(0); i++)
  {
    var something =
     from x in channel2.GetJautajumuSaraksts(testValues[i])
     ..................
     // rest of your code here
     ..................
       break;
     }
  }
}


Check syntax, I'm doing this off the top of my head.
 
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