AnrgyGang Member
Posts : 5 Join date : 2011-06-24
| Subject: Array element display, newbie question Wed Jun 29, 2011 6:29 am | |
| - Code:
-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> void Page_Load() { string[] drinkList = new string[4]; drinkList[0] = "Water"; drinkList[1] = "Juice"; drinkList[2] = "Soda"; drinkList[3] = "Milk"; drinkLabel.Text = drinkList[1]; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Array Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="drinkLabel" runat="server" /> </div> </form> </body> </html>
The above code was sent to me. When run it doesn't display the array element "Juice" as I would have expected. There's nothing I can see that isn't right with the code, though not a C# expert or beginner for that matter. Can anyone see something wrong with the code? |
|
Jervyn Member
Posts : 6 Join date : 2011-06-21
| Subject: Re: Array element display, newbie question Wed Jun 29, 2011 6:30 am | |
| just threw that together to check what is wrong w/ it... its almost fine.. the problem lies in your referenceing- you are gettin compilation errors... you set up 4 strings (string[] drinkList=new string[4]) thats fine... but you index them wrong.. always remember the "0" so your string align as follows string[0] string[1] string[2] string[3] for a total of 4 strings... so when u reference drinkList[4] you are attempting to reference the fifth string in the drinkList[string] array... make sense? |
|
AnrgyGang Member
Posts : 5 Join date : 2011-06-24
| Subject: Re: Array element display, newbie question Wed Jun 29, 2011 6:32 am | |
| - Jervyn wrote:
- just threw that together to check what is wrong w/ it... its almost fine.. the problem lies in your referenceing- you are gettin compilation errors... you set up 4 strings (string[] drinkList=new string[4])
thats fine... but you index them wrong.. always remember the "0" so your string align as follows string[0] string[1] string[2] string[3] for a total of 4 strings... so when u reference drinkList[4] you are attempting to reference the fifth string in the drinkList[string] array... make sense? Sorry confused now The array is being set to 4 elements, as you say 0 to 3 when referenced, and the label is then set to element 1 (the second item in the arry), not quite seeing where it's addressing a fifth element? |
|
AnrgyGang Member
Posts : 5 Join date : 2011-06-24
| Subject: Re: Array element display, newbie question Wed Jun 29, 2011 6:32 am | |
| Strangely this works - convert to vb and then back to C# and must have changed something. - Code:
-
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> void Page_Load() { string[] drinkList = new string[4]; drinkList[0] = "Water"; drinkList[1] = "Juice"; drinkList[2] = "Soda"; drinkList[3] = "Milk"; drinkLabel.Text = drinkList[1]; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Arrays</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="drinkLabel" runat="server" /> </div> </form> </body> </html>
|
|
Jervyn Member
Posts : 6 Join date : 2011-06-21
| Subject: Re: Array element display, newbie question Wed Jun 29, 2011 6:33 am | |
| nope look at ur first post and then above... u did exactly what you needed to... so you set up an array of strings. this is called drinkList... so in your first code you set the text to be label.Text=drinklist[4]; this is attempthing to access the fifth string in the array... ie drinkList[0] drinkList[1] drinkList[2] drinkList[3] and then it ends... yet you try and reference and -- scratch all that... i looked down at ur original to see where u were calling drinkList[4] and... oops! i simply left the above because it's informative on the way the array builds... so in short i dont see error of ur original code... i thought you rtried to ref drinkList[4] (which doesnt excist)... |
|
AnrgyGang Member
Posts : 5 Join date : 2011-06-24
| Subject: Re: Array element display, newbie question Wed Jun 29, 2011 6:33 am | |
| Maybe there's a control character or something in it????
Strange, converted to vb as more used to that syntax, worked fine, put it back into C# and fine ... dunno. |
|
Sponsored content
| Subject: Re: Array element display, newbie question | |
| |
|