1

I'm fairly new to c# and having difficulty with lists.

I'm creating an application that takes in a user's name, age, and address, then stores it in a list when the user clicks the 'add' button. I'm using a GUI with text boxes for user input.

I have made a Customer class and unsure what to do next. I've followed tutorials and other questions but can't seem to find an answer.

public class Customer
{
    private string name;
    private Int32 age;
    private string address1;
    private string address2;
    private string address3;


    public string Name
    {
        get
        {
            return name;
        }

        // if name is blank throw argument asking user for input

        set
        {
            if (name == "")
            {
                throw new ArgumentException("Please enter your name");
            }
            else 
            {
                name = value;
            }
        }
    }

    public Int32 Age
    {
        get
        {
            return age;
        }

        set
        {
                age = value;
        }
    }


    // get/set address

    public string Address1
    {
        get
        {
            return address1;
        }

        set
        {
            if (address1 == "")
            {
                throw new ArgumentException("Please enter your address");
            }
            else
            {
                address1 = value;
            }
        }
    }

    public string Address2
    {
        get
        {
            return address2;
        }

        set
        {
            if (address2 == "")
            {
                throw new ArgumentException("Please enter your adress");
            }
            else
            {
                address2 = value;
            }
        }
    }

    public string Address3
    {
        get
        {
            return address3;
        }


        set
        {
            if (address3 == "")
            {
                throw new ArgumentException("Please enter your adress");
            }
            else
            {
                address3 = value;
            }
        }
    }
6
  • You aren't showing any code about using a list. That's what we need to help you. Commented Dec 14, 2016 at 18:59
  • Research generic lists: msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx. Look at the examples. In your case, the generic parameter would be your Customer class. Commented Dec 14, 2016 at 19:01
  • 1
    In the Name property setter you are throwing exception if name is empty. I presume you wanted to throw exception if value is null or empty: if (string.IsNullOrEmpty(value)) throw new ArgumentException()... Commented Dec 14, 2016 at 19:02
  • Following up on @NathanA 's comment, you would declare you list as such List<Customer> customerList = new List<Customer>();, and then you could add to your list like this customerList.Add(new Customer{ ... }). But really, I guess we should ask... the list you speak of, are you intending to store the information in a file(which you call a list) or in a List<Customer>? Commented Dec 14, 2016 at 19:04
  • @NathanA Thank you will give it a look! Commented Dec 14, 2016 at 19:09

2 Answers 2

2

This is an example of simple Windows Forms form that will give you an idea. Basically you would like to store the list of customers in private generic list variable. More about how to use generic and non-generic lists in C# here.

public partial class Form1 : Form
{
    // Initialize private generic list where all customers will be stored at runtime
    private List<Customer> _customers = new List<Customer>();

    private void buttonAddCustomer_Click(object sender, EventArgs e)
    {
        // It might be a good idea to add some validation logic before assigning the input values
        var newCustomer = new Customer();
        newCustomer.Name = this.textBoxName.Text;
        newCustomer.Age = Convert.ToInt32(this.textBoxAge.Text);
        newCustomer.Address1 = this.textBoxAddress1.Text;
        newCustomer.Address2 = this.textBoxAddress2.Text;
        newCustomer.Address3 = this.textBoxAddress3.Text;

        _customers.Add(newCustomer);
    }
}

enter image description here

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

Comments

0

I think what you are looking for is in the MakeItHappen() method

using System;
using System.Collections.Generic;
using System.Windows.Forms;

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

        private void btn_Go_Click(object sender, EventArgs e)
        {
            MakeItHappen();
        }

        private void MakeItHappen()
        {
            List<Customer> customerList = new List<Customer>();//initialize your List<Customer>
            customerList.Add(new Customer { Name = txtbx_Name.Text, Address1 = txtbx_Address1.Text, Age = int.Parse(txtbx_Age.Text) });//add a record to it
        }
    }



    public class Customer
    {
        private string name;
        private Int32 age;
        private string address1;
        private string address2;
        private string address3;


        public string Name
        {
            get
            {
                return name;
            }

            // if name is blank throw argument asking user for input

            set
            {
                if (name == "")
                {
                    throw new ArgumentException("Please enter your name");
                }
                else
                {
                    name = value;
                }
            }
        }

        public Int32 Age
        {
            get
            {
                return age;
            }

            set
            {
                age = value;
            }
        }


        // get/set address

        public string Address1
        {
            get
            {
                return address1;
            }

            set
            {
                if (address1 == "")
                {
                    throw new ArgumentException("Please enter your address");
                }
                else
                {
                    address1 = value;
                }
            }
        }

        public string Address2
        {
            get
            {
                return address2;
            }

            set
            {
                if (address2 == "")
                {
                    throw new ArgumentException("Please enter your adress");
                }
                else
                {
                    address2 = value;
                }
            }
        }

        public string Address3
        {
            get
            {
                return address3;
            }


            set
            {
                if (address3 == "")
                {
                    throw new ArgumentException("Please enter your adress");
                }
                else
                {
                    address3 = value;
                }
            }
        }


    }
}

Comments

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.