1

I am new with C# and i have a problem. Actually it's my first year in college and in programming and i have a problem with arrays. I've made a class with 3 constructors and 1 method in Windows Form Application. The problem is that i want to store data from three textBoxes - that the user is typing- into an array of 10 using a button. and i don't know how to do it.

public class Employee
{
    private int idnum;
    private string flname;
    private double annual;

    public Employee()
    {

        idnum = 0;
        flname = "";
        annual = 0.0;
    }
    public Employee(int id, string fname)
    {
        idnum = id;
        flname = fname;
        annual = 0.0;
    }
    public Employee(int id, string fname, double ann)
    {
        idnum = id;
        flname = fname;
        annual = ann;
    }

    public int idNumber
    {
        get { return idnum; }
        set { idnum = value; }
    }
    public string FLName
    {
        get { return flname; }
        set { flname = value; }
    }

    public double Annual
    {
        get { return annual; }
        set { annual = value; }
    }
    public string Message()
    {
        return (Convert.ToString(idnum) + "  " + flname + "  " + Convert.ToString(annual));
    }

}
4
  • 1
    what do you mean by "an array of 10" ? Commented Apr 10, 2014 at 0:27
  • Are you saying you want to store the user's input (textboxes) in an array of size 10? Commented Apr 10, 2014 at 0:31
  • Your code has no textboxes nor arrays. Did you paste the wrong code? Commented Apr 10, 2014 at 0:39
  • What do you mean by “annual”? Is it a dollar amount? If so you should use decimal, not double. Commented Apr 10, 2014 at 2:31

1 Answer 1

1

First of all you should add on this form 3 textboxe elements and name it in a next manner textBoxId, textBoxFLName, textBoxAnnual

Also you have to add a button. Let's call it btnSave Write an event OnClick for this button. In this method we must read all data which user fill in on the form.

List<Employee> allEmployees = new List<Employee>();
private void buttonSave_Click(object sender, EventArgs e)
    {
        //read user input
        int empId = Int32.Parse(textBoxId.Text);
        string empFlName = textBoxFLName.Text;
        double empAnnual = double.Parse(textBoxAnnual.Text);

        // create new Employee object
        Employee emp = new Employee(empId, empFlName, empAnnual);

        // add new employee to container (for example array, list, etc). 
        // In this case I will prefer to use list, becouse it can grow dynamically
        allEmployees.Add(emp);

    }

And you can also rewrite your code in a little bit shortest manner:

public class Employee
{
    public int IdNum { get; set; }
    public string FlName { get; set; }
    public double Annual { get; set; }

    public Employee(int id, string flname, double annual = 0.0)
    {
        IdNum = id;
        FlName = flname;
        Annual = annual;
    }

    public override string ToString()
    {
       return (Convert.ToString(IdNum) + "  " + FlName + "  " + Convert.ToString(Annual));
    }
}
Sign up to request clarification or add additional context in comments.

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.