new to C#, trying to make an array from user input. please help
I'm new to C# and I'm trying to make an array out of user input. I really don't care what type it is (double, int, etc.), or how long it is ([10], [20], etc). How can I make an array that takes input from a textbox when a button is pressed and then stores it in [0], then stores the next user input from the textbox and stores it in [1], etc. until the array length is reached? thanks very much in advance, I've been searching for days with no luck finding examples.
thomas strussPosted Sep 14, 2010, 10:55 AM
List
private void Badd_Click(object sender, EventArgs e)
{
int score = Convert.ToInt32(TBi.Text);
ScoreList.Add(score);
if (score <0 | score >100)
{
MessageBox.Show ("enter 0-100 only...troublemaker.", "ERROR");
TBi.Text = "";
}
else if (score >=0 && score <=100)
{
int total = 0;
int count = ScoreList.Count;
for (int s = 0; s < count; s++)
{
total += ScoreList[s];
}
int average = total / count;
TBo.Text = (count).ToString();
TBo2.Text = (average).ToString();
TBo3.Text = (total).ToString();
}
}
private void Bdis_Click(object sender, EventArgs e)
{
string ScoreListString = "";
foreach (int score in ScoreList)
ScoreListString += score.ToString() + "\n";
MessageBox.Show(ScoreListString, "Sorted Scores");
}
private void Bclr_Click(object sender, EventArgs e)
{
ScoreList.Clear();
TBi.Text = "";
TBo.Text = "";
TBo2.Text = "";
TBo3.Text = "";
}
}
}
My question now is how do I get the program to disregard the input, meaning leave it out of the list, if it's out of the specified range? It's (score <0 | score >100) in my code.
Mamta MPosted Sep 13, 2010, 11:26 PM
Here's the code for it assuming that your form name is Form1
public partial class Form1 : Form
{
int size=3; // this can be changed as per your need
object[] arrInput;
int i=0;
public Form1()
{
InitializeComponent();
arrInput= new object[size];
}
private void btnOK_Click(object sender, EventArgs e)
{
arrInput[i++] = textBox1.Text;
}
private void btnDisplay_Click(object sender, EventArgs e)
{
for (i = 0; i < arrInput.Length; i++)
MessageBox.Show(arrInput[i].ToString());
}
}
Hope that helps.
-Mamta