...

View Full Version : Array element display, newbie question



Jethro
05-31-2011, 02:29 AM
<%@ 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?

alykins
05-31-2011, 03:12 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?

Jethro
05-31-2011, 04:01 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?

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?

Jethro
05-31-2011, 04:07 AM
Strangely this works - convert to vb and then back to C# and must have changed something.



<%@ 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>

alykins
05-31-2011, 04:40 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! :eek: 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)...

Jethro
05-31-2011, 07:14 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum