2D_Pray
2D_Pray

Reputation: 15

C# I have to Apply 10 Variables to a Label

With this assignment I have to apply 10 variables into a label from a Textbox, but when I enter the variables it enters 10 of the same value from the textbox i'm really confused on this topic. Here is the code for it:

private void btnNumbers_Click(object sender, EventArgs e)
    {
        int[] numbers = new int[10];
        for (int i = 0; i < numbers.Length; i++)
        {
            numbers[i] = Convert.ToInt32(txtNumbers.Text);
        }
        SmallestAndLargest(numbers);
    }
    private void SmallestAndLargest(int[] numbers)
    {
        Array.Sort(numbers);
        int x;
        for (x = 0; x < numbers.Length; ++x)
            lblSorted.Text += String.Format("  {0} ", numbers[x]);
        lblSmallest.Text = String.Format("Smallest Number Entered: {0} ", numbers[0]);
        lblLargest.Text = String.Format("Largest Number Entered: {0} ", numbers[numbers.Length - 1]);
    }

Upvotes: 1

Views: 90

Answers (5)

Dion V.
Dion V.

Reputation: 2120

int[] numbers = new int[10];
int counter = 0;

private void btnNumbers_Click(object sender, EventArgs e)
{        
    numbers[counter] = Convert.ToInt32(txtNumbers.Text);
    counter++;
    SmallestAndLargest(numbers);
}
private void SmallestAndLargest(int[] numbers)
{
    Array.Sort(numbers);
    int x;
    for (x = 0; x < numbers.Length; ++x)
        lblSorted.Text += String.Format("  {0} ", numbers[x]);
    lblSmallest.Text = String.Format("Smallest Number Entered: {0} ", numbers[0]);
    lblLargest.Text = String.Format("Largest Number Entered: {0} ", numbers[numbers.Length - 1]);
}

Not the best way to do it, but it should work. Key is to declare your array outside your clickmethod since it is gone as soon as it leaves the scope of the method. Be aware though - if you exceed the 10 entries, this will throw an exception. I'll leave that as an excercise.

EDIT: The way I figured the OP has implemented this is slightly different than the answers provided so far. My interpretation was that the OP would type in an integer in a textbox and click a button, after which it will be added to the array, repeating this 10 times.

EDIT 2: Since I figured this is the way you want it, you should also change

for (x = 0; x < numbers.Length; ++x)
    lblSorted.Text += String.Format("  {0} ", numbers[x]);

to

lblSorted.Text = String.Empty;
for (x = 0; x < numbers.Length; ++x)           
    lblSorted.Text += String.Format("  {0} ", numbers[x]);

This will make sure the label text is updated, not appended.

EDIT 3: Because I am in a good mood today, here is the Form I made to test and it works as desired.

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

        private void btnNumbers_Click(object sender, EventArgs e)
        {
            int x;
            if (!Int32.TryParse(txtNumbers.Text, out x)) return;
            numbers.Add(x);
            SmallestAndLargest(numbers.ToArray());
        }
        private void SmallestAndLargest(int[] numbers)
        {
            Array.Sort(numbers);
            int x;
            lblSorted.Text = String.Empty;
            for (x = 0; x < numbers.Length; ++x)                        
                lblSorted.Text += String.Format("  {0} ", numbers[x]);
            lblSmallest.Text = String.Format("Smallest Number Entered: {0} ", numbers[0]);
            lblLargest.Text = String.Format("Largest Number Entered: {0} ", numbers[numbers.Length - 1]);
        }
    }
}

Upvotes: 0

Sid
Sid

Reputation: 14896

Have a look at this! I changed the array into an List<Int> and made some other little changes, but it should do what you are trying to archive!

    public List<int> Numbers { get; set; }

    private void btnNumbers_Click(object sender, EventArgs e)
    {
        var currentVal = 0;
        if (Int32.TryParse(txtNumbers.Text, out currentVal))
            Numbers.Add(currentVal);
        SmallestAndLargest();
    }
    private void SmallestAndLargest()
    {

        Numbers.Sort(); //Sorts
        var max = 0;
        foreach(var x in Numbers)
        {
            lblSorted.Text += String.Format("  {0} ", x);
            max = x; //As List is sorted the current x is always the maximum value
        }            

        lblSmallest.Text = String.Format("Smallest Number Entered: {0} ", Numbers.ElementAt(0));
        lblLargest.Text = String.Format("Largest Number Entered: {0} ", max);
    }

Upvotes: 0

SamY
SamY

Reputation: 88

If you want to enter lots of values inside the texbox try:

string[] words = txtNumbers.Text.Split(' ');
for (int i = 0; i < numbers.Length; i++)
        {
            numbers[i] = Convert.ToInt32(words[i]);
        }

Upvotes: 0

PiousVenom
PiousVenom

Reputation: 6908

You need to split your enter value. So something like this:

private void btnNumbers_Click(object sender, EventArgs e)
{
    var numbers = txtNumbers.Text.Split(' ');

    int[] convertedItems = Array.ConvertAll<string, int>(numbers, int.Parse);

    SmallestAndLargest(convertedItems );
}

So, if you're entering values like this:

1 2 3 5 7 9 10

You're numbers array will be populated with one number each. The Split(' ') will split the string based where ever it finds an space. If they enter it a different way, you'll need to use a different delimiter for the Split(). You can read more about it on MSDN.

Then in your other method, you can iterate through your array and find the smallest and largest values.

Upvotes: 1

Pawel Maga
Pawel Maga

Reputation: 5787

Every time you assigning the same value from txtNumbers.Text property

 for (int i = 0; i < numbers.Length; i++)
 {
    numbers[i] = Convert.ToInt32(txtNumbers.Text);
 }

Upvotes: 1

Related Questions