Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 Array element display, newbie question

View previous topic View next topic Go down 
AuthorMessage
AnrgyGang
Member
Member
AnrgyGang

Posts : 5
Join date : 2011-06-24

Array element display, newbie question Empty
PostSubject: Array element display, newbie question   Array element display, newbie question EmptyWed 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?
Back to top Go down
Jervyn
Member
Member
Jervyn

Posts : 6
Join date : 2011-06-21

Array element display, newbie question Empty
PostSubject: Re: Array element display, newbie question   Array element display, newbie question EmptyWed 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?
Back to top Go down
AnrgyGang
Member
Member
AnrgyGang

Posts : 5
Join date : 2011-06-24

Array element display, newbie question Empty
PostSubject: Re: Array element display, newbie question   Array element display, newbie question EmptyWed 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?
Back to top Go down
AnrgyGang
Member
Member
AnrgyGang

Posts : 5
Join date : 2011-06-24

Array element display, newbie question Empty
PostSubject: Re: Array element display, newbie question   Array element display, newbie question EmptyWed 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>
Back to top Go down
Jervyn
Member
Member
Jervyn

Posts : 6
Join date : 2011-06-21

Array element display, newbie question Empty
PostSubject: Re: Array element display, newbie question   Array element display, newbie question EmptyWed 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)...
Back to top Go down
AnrgyGang
Member
Member
AnrgyGang

Posts : 5
Join date : 2011-06-24

Array element display, newbie question Empty
PostSubject: Re: Array element display, newbie question   Array element display, newbie question EmptyWed 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.
Back to top Go down
Sponsored content




Array element display, newbie question Empty
PostSubject: Re: Array element display, newbie question   Array element display, newbie question Empty

Back to top Go down
 

Array element display, newbie question

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» Display image in an array format
» Ruby Newbie
» List display sibling as block/none when is clicked
» Working with DOM Element
» get array value

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: Array element display, newbie question Edit-trash Useless :: Trash-