1

In the Insert Into ArrayList button’s click event handler, check to see if the user has: Entered a position in the Position textbox. Entered an actor’s name in the Actor Name textbox.

Check these textboxes to see if the user has entered a value. Do not worry if the user has entered an incorrect value, just that they have entered a value. If no text has been entered display relevant error messages in message box format and exit the method so no further processing takes place.

Follow this with a call to InsertIntoArrayList, passing these values and causing the insertion of the new value into the ArrayList.

Finally, call PopulateActors to update its items to reflect the change.

My code so far is not inserting into the array:

 private void btnInsert_Click(object sender, EventArgs e)
        {            

            if (string.IsNullOrEmpty(txtPosition.Text))
            {
                MessageBox.Show("Please enter a Position");
                return;
            }
            if (string.IsNullOrEmpty(txtActorName.Text))
            {
                MessageBox.Show("Please enter an Actor Name");
                return;
            }

            InsertIntoArrayList(txtActorName.Text, Position);
            PopulateActors();
        }        

And the Methods used are:

void InsertIntoArrayList(string txtActorName, int txtPosition)
            {

            ArrayList ActorArrayList = new ArrayList();

            ActorArrayList.Insert(txtActorName, txtPosition);

            }

And:

void PopulateActors()
            {
                cboActor.Items.Clear();

                foreach (string Actor in ActorArrayList)
                {
                    cboActor.Items.Add(Actor);
                }
            }

Newbie Coder! Cheers for your help :)

4
  • 1
    Where do you get the variable Position passed to the insertion code? Note that ActorArrayList is declared inside the insertion code and thus cannot be see outside of that method. If your code compiles then you have another ArrayList named ActorArrayList declared at a more global scope Commented Mar 15, 2016 at 22:41
  • 1
    Although this won't solve your current problem (which Luka has done for you nicely) you really should change ArrayList to List<string>. ArrayList is an untyped list and really shouldn't be used when you know the type that the list should contain. Commented Mar 15, 2016 at 22:55
  • @Enigmativity is definitely right, if you have access to newer versions of C#. Commented Mar 15, 2016 at 23:05
  • Cheers will keep that in mind. Commented Mar 15, 2016 at 23:16

1 Answer 1

1

Your problem is here:

void InsertIntoArrayList(string txtActorName, int txtPosition)
{

      ArrayList ActorArrayList = new ArrayList();

      ActorArrayList.Insert(txtActorName, txtPosition);

}

Each time you're calling this function, you're creating a new ArrayList and insert something into it. However, this ArrayList then immidietly gets released from memory because the function block is done.

You need to add an instance Member to your class and insert your data there. You could add something like this to the top of your class:

private ArrayList  ActorArrayList = new ArrayList();

Also Insert expects an Int as its first parameter, so you might want to switch the two parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers! Made those changes and all sorted now :) Thanks!

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.