简体   繁体   中英

How can I fill in an Array Using a Button_Click Event Handler in C# Visual 2010

I am trying to receive user input from a textbox and send it into an array that is in my class that is named Employee.

I am not sure if the code below is correct, but it seems like it is, because I have no compiling errors.
However, when I press the button the employee name and number are still in the textbox. What I would like for this application to do is the following:

  1. receive an employee's name and ID number;
  2. send them to my array that is in my " Employee" class, and as a name is sent to my array;
  3. I want the text box to clear in order for a new name to be entered.

Is this possible? Or am I asking for too much?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    namespace Company_Employees
    {
        class Employee
        {
            private const int NAME = 100;
            private static string[] employee = new string[NAME];
            private const int NUMBER = 100;
            private static int[] iD = new int[NUMBER];

            public Employee()  n// This is a null Constructor
            {
                employee[NAME] = null;
                iD[NUMBER] = 0;
            }



            public Employee(string name, int number) //  This is my overloaded constructor that receive these arguments from my main form.
            {
                for (int index = 0; index < employee.Length; index++)
                {
                    name = employee[index];

                }



                for (int index = 0; index < iD.Length; index++)
                {
                     number = iD[index];    
                }
           }


            public static int getemployeeNumber ()
            {

                return iD[NUMBER];
            }

            public static string getemployeeName()
            {
                return employee[NAME];
            }
        }
    }

This is my main Form that's has the button_click event handler. I want the user to input the employee name and ID number. Every time the user clicks the button to send the information to my "EmployeeClass", the textbox is cleared in order for new input to be entered.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            string employeeNames;
            int eNumbers;

            employeeNames = eName.Text;
            eNumbers = int.Parse( eNum.Text );

            Employee chart = new Employee(employeeNames, eNumbers);

            for ( int index = 0; index < 100; index++)
            {
                index = eNumbers;
            }
        }
    }
}            

Per @Palatiner 's comment, to let you know why I changed what you had to this, is that what you had way overcomplicated something that is very simple. Not to mention, the code you have will never work. Based on your code, you would always be updating the same array item since you explicitly assign to the array item at the constant position of 100.

All you need to do is create a object that keeps track of the 2 properties you want to save, Name and Id , along with a List that keeps a collection of those objects. An array does not dynamically resize, while a List does. With it, you do not need to manage the size of the collection on your own. It also provides access to a lot of LINQ extention methods.



I would create an Employee class like this:

public class Employee
{
    public string Name { get; set; }
    public int Id { get; set; }

    public Employee(string name, int id)
    {
        this.Name = name;
        this.Id = id;
    }
}


I would then create a CreateEmployee method like this:

private void CreateEmployee()
{
    // Get textbox values.

    string name = eName.Text;

    int id;
    int.TryParse(eNum.Text, out id);

    // Validate the values to make sure they are acceptable before storing.

    if (this.EmployeeValuesAreValid(name, id))
    {
        // Values are valid, create and store Employee.

        this.Employees.Add(new Employee(name, id));

        // Clear textboxes.

        eName.Text = string.Empty;
        eNum.Text = string.Empty;
    }
}


The EmployeeValuesAreValid method could be as simple as this:

private bool EmployeeValuesAreValid(string name, int id)
{
    return !String.IsNullOrEmpty(name) && id > 0;
}


Inside your Button Click you simply have to call CreateEmployee :

private void button1_Click(object sender, EventArgs e)
{
    this.CreateEmployee();
}



Putting everything together, you get:

public partial class Form1 : Form
{
    public class Employee
    {
        public string Name { get; set; }
        public int Id { get; set; }

        public Employee(string name, int id)
        {
            this.Name = name;
            this.Id = id;
        }
    }

    public List<Employee> Employees { get; set; }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.CreateEmployee();
    }

    private bool EmployeeValuesAreValid(string name, int id)
    {
        return !String.IsNullOrEmpty(name) && id > 0;
    }

    private void CreateEmployee()
    {
        // Get textbox values.

        string name = eName.Text;

        int id;
        int.TryParse(eNum.Text, out id);

        // Validate the values to make sure they are acceptable before storing.

        if (this.EmployeeValuesAreValid(name, id))
        {
            // Values are valid, create and store Employee.

            this.Employees.Add(new Employee(name, id));

            // Clear textboxes.

            eName.Text = string.Empty;
            eNum.Text = string.Empty;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM