0

I am currently working on a homework assignment that I am struggling with. The goal of the program is to:

Create a program that allows users to record and view names of up to 1000 events. The names of the events are to be stored in an array. The names can be stored and viewed sequentially only: I,e, the first time the user presses “Set” to store a name, it would be placed in index 0 of the array; the second time the user stores a name it would be stored in index 1, etc. Similarly, the first time the user presses “View”, the item found in index 0 of the array is shown to the user; the second time the user presses “View”, the item found in index 1 of the array is shown, etc.

  • When the user presses the “Set” button (btnSet), an event name is inserted into the array in the next free index, sequentially as described above. The name to insert to the array is taken from txtEventName.
  • When the user presses the button “View” (btnView), the name of the next event to view sequentially (as described above) is shown to the user. The event name is shown to the user in txtName.

I currently have the code:

namespace ArrayHW
{
    public partial class Form1 : Form
    {

        int[] eventsArray;
        const int SIZE = 1000;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            eventsArray = new int[SIZE];

            for (int index = 0; index < eventsArray.Length - 1; index++)
            {
                eventsArray[index] = Convert.ToInt32(txtEventName.Text);
            }

        }

        private void btnView_Click(object sender, EventArgs e)
        {
            for (int index = 0; index < eventsArray.Length- 1; index++)
            {
                Debug.WriteLine(eventsArray[index]);
                txtName.Text = Convert.ToString(eventsArray[index]);
            }

        }
    }
}

When I run the form, I only get the result for index = 0,1, 2, 3, etc or whatever I had just input into the array in

private void btnSet_Click(object sender, EventArgs e)
{
    eventsArray = new int[SIZE];

    for (int index = 0; index < eventsArray.Length - 1; index++)
    {
        eventsArray[index] = Convert.ToInt32(txtEventName.Text);
    }

}

rather than it showing up in sequential order like it is supposed to. Could anyone show me a better way to approach this problem, or help me find out what I am doing wrong? Thank you very much.

0

2 Answers 2

3

Please read the comments in the code block. Hopefully that will help you resolve your problem.

public partial class Form1 : Form
{
    const int SIZE = 1000;
    int[] eventsArray = new int[SIZE];

    //as per your requirement, you would need these 
    //to display and set items at proper index in the array.
    int _setIndex = 0;
    int _viewIndex = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void btnSet_Click(object sender, EventArgs e)
    {
        //this is where your problem is. Every time the Set btn is clicked,
        //you are creating a new array. Therefore you are only seeing what you added in the 
        //last click. Anything that was added to the array on the prior clicks are lost because
        //you just created a new array with the line below.
        //eventsArray = new int[SIZE];
        //You would need to comment the line above because this is code block is being 
        //executed on every btnSet click. New up this array only once by moving this to global scope.

        //for (int index = 0; index < eventsArray.Length - 1; index++)
        //{
        //    eventsArray[index] = Convert.ToInt32(txtEventName.Text);
        //}

        //Since we have created fields to keep track of _setIndex, all we need to do is:
        if (_setIndex < SIZE)
        {
            eventsArray[_setIndex] = Convert.ToInt32(txtEventName.Text);
            _setIndex++;
        }
    }

    private void btnView_Click(object sender, EventArgs e)
    {
        //this wont be necessary.
        //for (int index = 0; index < eventsArray.Length - 1; index++)
        //{
        //    Debug.WriteLine(eventsArray[index]);
        //    txtName.Text = Convert.ToString(eventsArray[index]);
        //}
        if(eventsArray.Length > _viewIndex)
        {
            txtName.Text = Convert.ToString(eventsArray[_viewIndex]);
            //this is to fulfill the requirement below:
            // Similarly, the first time the user presses “View”, the item found in index 0 
            // of the array is shown to the user; the second time the user presses “View”,
            // the item found in index 1 of the array is shown, etc.
            _viewIndex++;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

int[] eventsArray = new int[SIZE];
int index = 0;
const int SIZE = 1000;

private void btnSet_Click(object sender, EventArgs e)
{
    eventsArray[index++] = Convert.ToInt32(txtEventName.Text);
}

7 Comments

Hello, thank you for your response. I tried your solution with the new code but my output is always 0 now. Did I declare something incorrectly? ` int[] eventsArray = new int[SIZE]; int index = 0; const int SIZE = 1000; public Form1() { InitializeComponent(); } private void btnSet_Click(object sender, EventArgs e) { eventsArray = new int[SIZE]; eventsArray[index++] = Convert.ToInt32(txtEventName.Text); Debug.WriteLine(eventsArray[index++]); }`
@Zack - My code did not have eventsArray = new int[SIZE]; in the btnSet_Click method. Why did you put it in? What happens when you take it out?
@Zack - And why did you put in Debug.WriteLine(eventsArray[index++])? That won't work.
my bad, I added the debug when I noticed only 0 was appearing in the textbox "txtName" for the btnView method.I also took out the eventsArray = new int [SIZE] declaration, but nothing changed.
@Zack - You need to post the code that you're using in your question as my code should work perfectly fine. You've got something else causing your problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.