user1687151
user1687151

Reputation: 1

Put the contents of an array in a messagebox

I need to take the contents of an array and put them out to a message box when a button is clicked. The numbers are loaded into the array when the user hits the add button, and it does its other functions just fine. However, when the display button is clicked, it pops up a message box but it just reads 0. Here is the code:

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 Testscores.UI
{
    public partial class frmTestscores : Form
    {
        //creates the variables needed for the calculations
        int scores;
        double total = 0;
        int count = 0;
        int counts = 0;
        double average = 0;
        int[] sArray;


        public frmTestscores()
        {
            InitializeComponent();
        }
        //creates the exit button click event
        private void btnExit_Click(object sender, EventArgs e)
        {
            //exits the application
            Application.Exit();
        }
        //creates the clear button click event
        private void btnClear_Click(object sender, EventArgs e)
        {
            //clears all text fields and variables
            txtAverage.Text = "";
            txtCount.Text = "";
            txtScore.Text = "";
            txtTotal.Text = "";
            scores = 0;
            total = 0;
            counts = 0;
            average = 0;

        }
        //creates the add button click event
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //creates the try and catch statements
            try
            {
                //does the calculations and outputs it into the text fields
                scores = int.Parse(txtScore.Text);

                counts += 1;
                total = total + scores;

                average = total / counts;
                    txtAverage.Text = average.ToString();
                    txtTotal.Text = total.ToString();
                txtCount.Text = counts.ToString();
                //initializes the array(this is where i get confused)
                int SIZE = counts;
                Array.Resize(ref sArray, SIZE);
                for (int count = 1; count > SIZE; count++)
                {

                   sArray[count] = scores;
                    //outputs the count to the text box
                    txtCount.Text = count.ToString();




                }
            }
                //catch statement
            catch (Exception ex)
            {
                //outsputs a message to the user
                MessageBox.Show("Please enter a valid number,");

            }
        }
        //creates the display button click event
        private void btnDisplay_Click(object sender, EventArgs e)
        {
            //supposed to output the array to a message box
            MessageBox.Show(sArray[].ToString());

        }
    }
}

Upvotes: 0

Views: 9009

Answers (3)

Jonathan Henson
Jonathan Henson

Reputation: 8206

Yes, this will not output the entire array. Read the documentation on ToString(). It usually outputs the object's typename unless it is overridden in a subclass.

The brute force of doing this is quite simple:

string output = new string();

for(int i = 0; i < sArray.Length; i++)
{
   output += sArray[i] // plus any delimiters or formating.
}

MessageBox.Show(output);

Upvotes: 0

Gromer
Gromer

Reputation: 9931

Change your display click event handler to:

private void btnDisplay_Click(object sender, EventArgs e)
{
    string output = string.Empty;
    foreach (var item in sArray)
    {
        output += item + " ";
    }

    //supposed to output the array to a message box
    MessageBox.Show(output);
}

Upvotes: 0

hamid reza mansouri
hamid reza mansouri

Reputation: 11333

You can combine the individual strings from the array into a single string (such as with the string.Join method) and then display the concatenated string:

string toDisplay = string.Join(Environment.NewLine, sArray); 
MessageBox.Show(toDisplay);

Upvotes: 5

Related Questions