Use custom text fields input (numbers) to specific size and array output?
This is "homework" (in my C # book)
What should happen in the text box, the user enters 10 numbers (one at a time I will do this) and every time he clicks the "addValBtn" button, he should add that number to the array until he fills 10 dots using 10 user-entered numbers. Then I try to display this array via displayValBtn (which I can figure out myself), but I just can't seem to get this damn array to work correctly.
My book explains how to set up an array and from what I read on Stackoverflow and Google people had similar questions. But none of them seemed to contribute every time you click the button. So I don't know what to do.
I created and defined my array as numArray (using double) - set the index of my array to 10. Then I made a for loop to parse the number from the textbox into an array. But when I run, nothing happens. (or as far as Im concerned about it working, I just didn't display it to see if the count keeps it)
1) Am I doing it the right way for this situation? 2) So how do I need to display the contents of the array after filling it through the button, should my variables 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)
{
}
}
}
source to share
Am I doing it right for this situation?
Sorting, but not quite :) The code you have to parse the input is good, but you just assign all 10 array values ββto the same number.
Also, your array is locally bound to the method addValBtn_Click
, which means it won't close at the end of that event handler.
So how do I need to display the contents of the array after filling it through the button, should my variables be global?
As I mentioned earlier, your array won't stick. You are on the right track, but it doesn't have to be a global variable, just a class levelForm
You need to declare your array once and then keep pushing values ββto it when the button is clicked. Be sure to keep track of which index you are also using.
I declared my "index" in the class as follows: int index = 0; and this is saying that the "array.array.index" field is never assigned. Doesn't make any sense to me seeing how I assigned a meaning to it!
Make sure you declare it in the right place. If you've 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 problem with that. If there is, then I probably can't help you without standing on my shoulder :)
source to share
You need to do a little research on event based programming.
for (int index = 0; index < numArray.Length; index++)
{
numArray[index] = int.Parse(intTxtBox.Text);
}
This will read the same integer at every position in the array.
You need to keep the "index" in your class and just do something like:
if (this.index < 10)
{
numArray[this.index] = int.Parse(intTxtBox.Text);
this.index++
}
source to share