Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2011
    Location
    Australia
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array element display, newbie question

    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?

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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?

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New Coder
    Join Date
    May 2011
    Location
    Australia
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    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?

  • #4
    New Coder
    Join Date
    May 2011
    Location
    Australia
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #5
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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)...

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #6
    New Coder
    Join Date
    May 2011
    Location
    Australia
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •