0

I have a array which I wan't to fill with a textbox, like the javascript .push() method.

Here is my code so far:

namespace Assigment9
{
    public partial class Default : System.Web.UI.Page
    {
        double[] numbers = new double[5];

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            double number = Convert.ToDouble(numberTB.Text);

        }
    }
4
  • You are just declaring a number, not actually adding it to the array. Add an index counter, int index = 0, and do numers[index] = number. Then increment the index index++. Commented Mar 22, 2015 at 16:57
  • You want a List<double>. You can't modify an array's size. Commented Mar 22, 2015 at 16:59
  • 'push' does not make sense when accessing an array. Which end are you pushing the number to? What happens to existing elements? Commented Mar 22, 2015 at 16:59
  • I know, but I want to be able to add something to numbers[0] and the second time i click the button it should add a number to numbers[1] etc. Commented Mar 22, 2015 at 16:59

4 Answers 4

1

You need to add the numbers to the array of numbers you declared

double[] numbers = new double[5];

Right now you are just declaring a new variable in addButton_Click called number and not doing anything with it.

It might be easier to use a list of numbers. I.E

List<double> numbers = new List<double>();

Then in addButton_Click just add the number

numbers.Add(mynumber);

You have declared an array of length 5 in the class this means you will need to declare an index variable and do bounds checking to make sure after the addButton is click that the array isnt full. I.E once the user clicks the button more than 5 times. Using the list avoids that effort.

Also you will have an exception in the addButton_Click handler if the user types in a non number value into the text box. You should also look to change that to something like double.TryParse.

public partial class Default : System.Web.UI.Page
{
    public List<double> numbers
    {
        get
        {
            var list = ViewState["mylist"];
            if (list == null) ViewState["mylist"] = new List<double>();
            return ViewState["mylist"] as List<double>;
        }             
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void addButton_Click(object sender, EventArgs e)
    {
        string value;
        double number;

        value = numberTB.Text;
        if (Double.TryParse(value, out number)) numbers.Add(number);
        // you should display some sort of message if the value isnt a number...
    }
}

Since this is asp.net web forms you also need to stick the member variable in ViewState or Session otherwise the list or array / whatever will always be empty as web forms doesn't automatically persist member variable values for you.

Sign up to request clarification or add additional context in comments.

Comments

0

I would go like this:

        public partial class Default : System.Web.UI.Page
        {
            Collection<double> numbers = new Collection<double>();

            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void addButton_Click(object sender, EventArgs e)
            {
                try
                {
                    double number = Convert.ToDouble(numberTB.Text);
                    numbers.Add(number);

                }
                catch (FormatException) {
                Console.WriteLine("Unable to convert '{0}' to a Double.", number);
                }               
                catch (OverflowException) {
                Console.WriteLine("'{0}' is outside the range of a Double.", number);
                }

            }
        }

Comments

0

I know there are lots of way to get the same output .it is also one of them.

According to Your Comment

I know, but I want to be able to add something to numbers[0] and the second time i click the button it should add a number to numbers[1] etc

Here is Solution

namespace Assigment9
{
    public partial class Default : System.Web.UI.Page
    {
        double[] numbers = new double[5];
        int counter=0; // declare a global variable which will count the number of button clicks

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            if(counter <5)                
            {
             numbers[counter] = Convert.ToDouble(numberTB.Text);
             counter ++;
            }

        }
    }

4 Comments

I prefer your code because it's a array school assigment, I had this in mind too, but everytime I repress the button it resets the public partial class, any Idea how to fix that?
I did, I also put the counter in a label and every time I click the button it shows 1.
@marceljager sory i forget that you are working on web application you have to store counter in the session variable or application variable
Do you have an exemple, I'm just a beginner with C#. Thanks anyway. :)
0

Why would you like to use C# to load List from server side TextBox component. I would recommend to use Javascript for that.

But, if you explicitilly want to load list from c# code with TextBox you should use Session for instance because when you click on the button the page is making post back so the initialized list gets reseted.

Here's the result code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            List<double> numbers = new List<double>();
            Session["numbers"] = numbers;
        }  
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["numbers"] != null)
        {
            List<double> numbersResult = Session["numbers"] as List<double>;
            double number = Convert.ToDouble(TextBox1.Text);
            numbersResult.Add(number);
            Session["numbers"] = numbersResult;
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.