Where to declare array so it isn't recreated every time Save is pushed

Duchesssix

New member
Joined
Oct 19, 2014
Messages
1
Programming Experience
Beginner
I am using Net 4.5. This is all new to me, especially programming with WPF instead of console apps.

I need to create a data entry form that keeps all user data in an arraylist for easy display and sort. However, I haven't figured out where to declare the generic collection so every time SAVE is pressed thearray is recreated andI never have more than 1 record at a time. If I move array instantiation outside the button click I get out of context errors. Can anyone help me?

C#:
namespace Project4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public class InputRecord
        {
            public string firstName { get; set; }
            public string lastName { get; set; }
            public string address { get; set; }
            public string city { get; set; }
            public string state { get; set; }
            public int zip { get; set; }
            public string phone { get; set; }

            public InputRecord()
            { }

            public InputRecord(string firstName, string lastName, string address,
                string city, string state, int zip, string phone)
            {
                this.firstName = firstName;
                this.lastName = lastName;
                this.address = address;
                this.city = city;
                this.state = state;
                this.zip = zip;
                this.phone = phone;
            }

        }
        public MainWindow()
        {
            InitializeComponent();

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            ArrayList myList = new ArrayList();
            //dataGrid1.ItemsSource = "";
            InputRecord myaddressFiles = new InputRecord();

            myaddressFiles.firstName = txtFName.Text;
            myaddressFiles.lastName = txtLName.Text;
            myaddressFiles.address = txtAddress.Text;
            myaddressFiles.city = txtCity.Text;
            myaddressFiles.state = txtState.Text;
            //myaddressFiles.zip = txtZip.Text;
            myaddressFiles.phone = txtPhone.Text;

            
            myList.Add(myaddressFiles);
            myList.Sort();
            dataGrid1.ItemsSource = myList;
            clearData();

        }

       

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {

            clearData();

        }

        private void clearData()
        {
            txtFName.Text = "";
            txtLName.Text = "";
            txtAddress.Text = "";
            txtCity.Text = "";
            txtState.Text = "";
            txtZip.Text = "";
            txtPhone.Text = "";
            txtFName.Focus();
        }
    }
}
 
Firstly, an array and an ArrayList are two different things. They are similar, hence the similar names, but they are not the same. An ArrayList is not an array. It behaves like an array in many ways but the ArrayList class was created specifically to overcome the most glaring limitation of arrays: they are fixed-size. An ArrayList grows and shrinks automatically as you add and remove items, which an array can't do.

Secondly, if you're using .NET 2.0 or later, you shouldn't be using an ArrayList at all. If you're doing an old exercise or a new exercise set by a poor instructor then you may have to use it to comply but .NET 2.0 introduced generics and basically superseded the ArrayList with the List(Of T), where T is the type of the items it contains. If you have the choice, you should be using that rather than ArrayList.

As for the issue, if you don't want a new ArrayList created each time btnSave is clicked then don't create an ArrayList inside the Click event handler of btnSave. The obvious alternative to inside the method is outside the method.
 
Back
Top Bottom