How to add object in an ArrayList in a foreach loop

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
 
D

dunawayc

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top