Getting at items in arraylist

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

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?
 
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["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["bacon"].ToString());
}

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

DisplayOrder.InnerHtml += sb.ToString();
}
 
Mike said:
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["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["bacon"].ToString());
}

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

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
 
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)
{
}

Arne Vajhøj said:
Mike said:
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["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["bacon"].ToString());
}

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

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
 
Mike said:
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;
...item["bacon"].ToString();

<...>

HTH

JB
 
Mike P said:
John,
Sandwich item = (Sandwich)orders;
...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
 
Mike P said:
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
 
Back
Top