How to add object in an ArrayList in a foreach loop

  • Thread starter Thread starter Fia
  • Start date Start date
F

Fia

Hi

I'm using the code below. And I get the error that quest is local, which I
understand, but how can I do instead to fill an ArrayList with the objects.

public ArrayList createQuestArr()

{

ArrayList quest = new ArrayList();

foreach (DataRow row in ds.Tables["Frgor"].Rows)

{


ArrayList answ = new ArrayList();

int nr = (int)row.ItemArray[0];

string quest = (string)row.ItemArray[1];

string ra = (string)row.ItemArray[6];

for (int i = 2; i <= 5; i++)

answ.Add(row.ItemArray);


Question q =new Question (nr,quest ,answ, ra );

quest.add(q);

}

}

Please help

Fia
 
Hi

I'm using the code below. And I get the error that quest is local, which I
understand, but how can I do instead to fill an ArrayList with the objects.

public ArrayList createQuestArr()

{

ArrayList quest = new ArrayList();

foreach (DataRow row in ds.Tables["Frgor"].Rows)

{

ArrayList answ = new ArrayList();

int nr = (int)row.ItemArray[0];

string quest = (string)row.ItemArray[1];

string ra = (string)row.ItemArray[6];

for (int i = 2; i <= 5; i++)

answ.Add(row.ItemArray);

Question q =new Question (nr,quest ,answ, ra );

quest.add(q);

}
}

Please help

Fia


And to add to Peter's response, even though you have created the quest
ArrayList, you have not returned it from the method using the return
keyword.

Chris
 
Back
Top