Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Getting at items in arraylist

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike P - 14 Oct 2007 21:13 GMT
I have an arraylist that is made up of Sandwich objects, which have
properties for each ingredient.  What is the correct syntax to loop
through the arraylist and get at each of these properties for each item
in the array?
Mike P - 14 Oct 2007 22:34 GMT
I'm trying the following syntax, but getting errors.  Can anybody help?

for (int i = 0; i <= orders.Count ; i++)
       {
           sb.Append("<br/><table>");
           sb.Append("<tr><td>" + orders[i]["bacon"].ToString());
       }

FULL CODE :

protected void Page_Load(object sender, EventArgs e)
   {
       ArrayList orders = new ArrayList();
       StringBuilder sb = new StringBuilder();

       orders = (ArrayList)Session["Order"];

       sb.Append("<form id='form1' runat='server'>");
       sb.Append("<div
style='text-align:center;page-break-after:always;'>");
       sb.Append("<table style='width:6.5in;border:solid 1px
black;height:9.5in'>");
       sb.Append("<tr>");
       sb.Append("<td valign=top>");
       sb.Append("<table width=90%>");
       sb.Append("<tr>");
       sb.Append("<td align=left valign=middle>ORDER</td>");

       for (int i = 0; i <= orders.Count ; i++)
       {
           sb.Append("<br/><table>");
           sb.Append("<tr><td>" + orders[i]["bacon"].ToString());
       }

       sb.Append("</td></tr></table></td></tr></table></div></form>");

       DisplayOrder.InnerHtml += sb.ToString();
   }
Arne Vajhøj - 14 Oct 2007 23:13 GMT
> I'm trying the following syntax, but getting errors.  Can anybody help?
>
[quoted text clipped - 34 lines]
>         DisplayOrder.InnerHtml += sb.ToString();
>     }

1) It will be a bit easier for us if you tell us which errors you get.

2) I am not sure appending HTML is the best way to do this in ASP.NET.

Arne
Family Tree Mike - 15 Oct 2007 01:02 GMT
your order loops are going one step too far, and should go as follows:

for (int i = 0; i < orders.count; ++i)

However, I think that the following is more understandable, in my opinion:

foreach (Sandwitch s in orders)
{
}

> > I'm trying the following syntax, but getting errors.  Can anybody help?
> >
[quoted text clipped - 40 lines]
>
> Arne
John B - 15 Oct 2007 07:24 GMT
> I'm trying the following syntax, but getting errors.  Can anybody help?
>
> for (int i = 0; i <= orders.Count ; i++)
>         {

You will need to cast the items as the ArrayList stores objects.
        Sandwich item = (Sandwich)orders[i];
        ...item["bacon"].ToString();

<...>

HTH

JB
Mike P - 15 Oct 2007 09:14 GMT
John,

>>Sandwich item = (Sandwich)orders[i];
>>...item["bacon"].ToString();

So what is the correct full syntax to get at the item["bacon]?
Christof Nordiek - 15 Oct 2007 12:09 GMT
> John,
>
>>>Sandwich item = (Sandwich)orders[i];
>>>...item["bacon"].ToString();
>
> So what is the correct full syntax to get at the item["bacon]?
given that bacon is a property of the Sandwich-class:

item.bacon

the codeexample of JB presumes, that Sandwich has an indexer with parameter
of string and you want to return the item called bacon.
In that case item["bacon"] is allready the right syntax.

Christof
Christof Nordiek - 15 Oct 2007 12:12 GMT
>I have an arraylist that is made up of Sandwich objects, which have
> properties for each ingredient.  What is the correct syntax to loop
> through the arraylist and get at each of these properties for each item
> in the array?

Unless you are bound to Framework 1.1 for some reason you should use
List<Sandwich> instead of ArrayList.

Christof

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.