Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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. ;)

[Added]
Thanks a lot! Very fast responses. I want to add some error control, but I'm not sure how best to do it. Right now I'm trying to use an else-if statement but the program continues too long and puts the int in the list even though it's out of range! Fun but frustrating. Here's the code:

[Added]
Moved it here: else if (score >=0 && score <=100)
{
ScoreList.Add(score);
int total = 0;
int count = ScoreList.Count;

Works perfect now! Thanks Marc, that's a point for you!

[Added]
Gotcha Will. Thanks for the help! :)

List<int> ScoreList = new List<int>();

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 = "";
}</int></int>
Posted
Updated 14-Sep-10 6:14am
v4
Comments
William Winner 14-Sep-10 11:45am    
Thomas: In the future, don't post an answer with another question or comments about responses. Either update your original post (as I have done for you) or add a comment...as I am doing now.

Hi,

Try using ArrayList. For more details about ArrayList go through this link,
http://en.csharp-online.net/ArrayList[^]

Regards,
Suresh
 
Share this answer
 
create a global arraylist.

for every button click add the textbox.text to arraylist.
 
Share this answer
 
In answer to your second question...
That would be because you're adding the item to your List outside of your if/else if blocks. You need to move the line of code that adds the item to the list into either the "if" block or the "else if" block, depending on which condition should trigger storing the value.
 
Share this answer
 
v3
Just a quick note:

I hope this is a typing error, but
C#
if (score <0 | score >100)

is not correct. You need to have two pipes as in:
C#
if (score < 0 || score > 100)


Your code will still work with one pipe, but two pipes tells it to stop checking the further conditions if the first condition is true.

This can be very useful if you're doing something like:
C#
if (someClass != null && someClass.Value == 5)


In this case, the expression will execute without error if someClass is null. What happens is the first part comes out as false and because we're checking for and, it doesn't need to continue.

However, if you write
C#
if (someClass != null & someClass.Value == 5)

and someClass is null, you will get an error.
 
Share this answer
 
Comments
Dalek Dave 14-Sep-10 12:45pm    
Good Call
hello thomas
for your purpose, the best way is to use ArrayList, because its dosent matter that which kind of data is you are going to add into list(int, double,string)
so ArrayList is the best option, because it is the array of object, not any perticular kind of datatype array, and its length is also depend on user.....


ArrayList _instance= new ArrayList();
_instance.Add(5);
_instance.Add(5.5);
_instance.Add("five");


please give me your rwealy about solution
 
Share this answer
 
v4
Comments
Peter_in_2780 14-Sep-10 1:23am    
Nilesh. We use English in this forum, not txtspk. If you want to be taken seriously, clean up your answer.
Dalek Dave 14-Sep-10 3:28am    
It is hard enough for some people to read concise, clear English, which is why Childish Txtspk is frowned upon.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900