Reputation: 93
This is "homework" (in my c# book)
What should happen is on a textbox a user inputs 10 numbers (one at a time I would do it) and each time he clicks the button "addValBtn" it should add that number to the array until it fills up 10 spots with 10 user inputted numbers. Then I'm trying to display that array via the displayValBtn (which I can figure out myself) but I just cant get this damn array to work correctly.
My book explained how to set up an array fine, and from what I read on Stackoverflow and off Google people had similar questions. But none of them seemed to take input each time you click the button. So I'm at a loss of what to do exactly.
I created and defined my array as numArray (using double) - set my array index to 10. Then I did a for loop so that it should parse the number from the textbox into the array. But when I run nothing happens. (or as far as Im concerned its working I just havent displayed it back to see if its storing the numbers)
1) Am I doing this correctly for this situation? 2) Since I need to display the contents of the array once its populated via a button, would my variables need to be global?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace array
{
public partial class array : Form
{
public array()
{
InitializeComponent();
}
private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
public void addValBtn_Click(object sender, EventArgs e)
{
double[] numArray = new double[10];
for (int index = 0; index < numArray.Length; index++)
{
numArray[index] = int.Parse(intTxtBox.Text);
}
}
private void displayValBtn_Click(object sender, EventArgs e)
{
}
}
}
Upvotes: 3
Views: 7647
Reputation: 44906
Am I doing this correctly for this situation?
Sort of, but not really :) The code you have for parsing the input is good, but you are simply assigning all 10 values of the array to the same number.
Also, your array is scoped locally to the addValBtn_Click
method, which means it won't stick around past the end of that event handler.
Since I need to display the contents of the array once its populated via a button, would my variables need to be global?
As I mentioned earlier, your array isn't going to stick around. You are on the right track, but it doesn't need to be a global variable, just class level Form
You need to declare your array once, and then keep pushing values into it when the button is clicked. Be sure to keep track of which index you are on as well.
I declared my "index" in the class like so: int index = 0; and it's telling me that the field 'array.array.index' is never assigned to. Doesn't really make sense to me, seeing as how I assigned it a value!
Make sure you are declaring this in the right spot. If you have done everything you say your class declaration should look something like this:
public partial class array : Form
{
private const int MAX_ITEMS = 10;
private int _currentIndex = 0;
private double[] _numArray = new double[MAX_ITEMS];
//SNIP...
}
There shouldn't be any problems with that. If there are, then I probably can't help you without standing over your shoulder :)
Upvotes: 1
Reputation: 29266
You need to do a bit of research on event driven programming.
for (int index = 0; index < numArray.Length; index++)
{
numArray[index] = int.Parse(intTxtBox.Text);
}
This is going to read the same integer into every position of the array.
You need to store "index" in your class and just do something like:
if (this.index < 10)
{
numArray[this.index] = int.Parse(intTxtBox.Text);
this.index++
}
Upvotes: 0