PC Review


Reply
Thread Tools Rate Thread

How to add object in an ArrayList in a foreach loop

 
 
Fia
Guest
Posts: n/a
 
      12th Apr 2009
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[i]);


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

quest.add(q);

}

}

Please help

Fia


 
Reply With Quote
 
 
 
 
dunawayc@gmail.com
Guest
Posts: n/a
 
      13th Apr 2009
On Apr 12, 2:04 pm, "Fia" <fiao...@telia.com> wrote:
> 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[i]);
>
> 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
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
remove ArrayList[n] inside foreach() loop? =?Utf-8?B?YmJn?= Microsoft C# .NET 13 6th Sep 2007 02:14 AM
stepping through a collection object in a foreach loop David C Microsoft C# .NET 5 9th Sep 2005 08:01 PM
URG: How can you tranverse an Arraylist using foreach loop =?Utf-8?B?Tmljb2xlIC0gQVNQL0MjIEJlZ2lubmVy?= Microsoft Dot NET 3 27th Aug 2004 10:57 PM
foreach for ArrayList - unexpected behavior... Kail Microsoft C# .NET 3 14th Jun 2004 07:20 AM
foreach & arraylist Lloyd Dupont Microsoft C# .NET 9 15th Oct 2003 12:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:54 AM.