0

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;
            }
        }
    }
}            
4
  • It seems that the application already receives the information you want and it stores the data in the arrays. If you want to clear the textboxes, just add eName.Clear(); and eNum.Clear() after the arrays filling process. Commented Nov 19, 2013 at 17:37
  • Mind if I ask: what is your previous programming experience? Your code looks very "low level". Scott's answer implicitly changes a lot of your style (arrays, CONSTANTS, etc.) without explaining it a lot. These are some wonderful features in C#, you should definitely look into why he changed it like that. Commented Nov 19, 2013 at 17:47
  • @Palatiner - I've added more detail to my answer. Commented Nov 19, 2013 at 18:18
  • I am an First Semester C# student, I am aware that my code is very simple and at time mediocre. However, Thank you for pointing that out. Commented Nov 19, 2013 at 18:18

1 Answer 1

1

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I see what you, I am will go home and run this code to see if it does the job. I will post more comments later today.
At first, your answer just solved the problem.The way it is now, you explain what you do and why you're doing it. I like it. @ErickBRCC: Don't get me wrong on this; your question was answered and it will probably help you with what you're trying to do. But you should really look at some C# tutorials and find a proper way to do your coding; even though your question was "solved", this isn't what people would call a good question because you seem to have missed some major errors in your code. What I wanna say: learn to understand what happens why! use the debugger! question everything!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.