Click here to Skip to main content
15,890,579 members
Home / Discussions / C#
   

C#

 
GeneralRe: C#.net Date Time checker Pin
Member 1238698215-Mar-16 20:08
Member 1238698215-Mar-16 20:08 
GeneralRe: C#.net Date Time checker Pin
Mycroft Holmes15-Mar-16 21:35
professionalMycroft Holmes15-Mar-16 21:35 
GeneralRe: C#.net Date Time checker Pin
Member 1238698215-Mar-16 22:43
Member 1238698215-Mar-16 22:43 
GeneralRe: C#.net Date Time checker Pin
Richard MacCutchan15-Mar-16 23:40
mveRichard MacCutchan15-Mar-16 23:40 
AnswerRe: C#.net Date Time checker Pin
Gerry Schmitz16-Mar-16 6:35
mveGerry Schmitz16-Mar-16 6:35 
GeneralRe: C#.net Date Time checker Pin
Member 1238698218-Mar-16 0:26
Member 1238698218-Mar-16 0:26 
GeneralRe: C#.net Date Time checker Pin
Gerry Schmitz18-Mar-16 6:25
mveGerry Schmitz18-Mar-16 6:25 
QuestionDynamically Create Objects Then Add Them To An Array Pin
MadDashCoder14-Mar-16 21:27
MadDashCoder14-Mar-16 21:27 
I have a simple console application that allows users to keep adding an employee to an array until they answer no to a prompt asking them if they want to add more employees.

Below is my code but it is only showing the first employee and not the subsequent employees that were added.

C#
class Program
    {
        static void Main(string[] args)
        {
            Employee[] employees = new Employee[5];
            int employeeCount = 0;
            bool addMoreEmployees = true;
            String answer = string.Empty;
            while(addMoreEmployees)
            { 
                Employee emp = new Employee();
                emp.AddEmployee();

                //Add employee to employees array at index employeeCount if it is empty
                if (employees[employeeCount] == null)
                {
                    employees[employeeCount] = emp;
                }
                employeeCount++;

                Console.WriteLine("Do you want to add more employees?");
                answer = Console.ReadLine().ToUpper();

                if (answer == "NO" || employeeCount > 5)
                {
                    addMoreEmployees = false;                    
                }
            }

            foreach (Employee employee in employees)
            {
                Console.WriteLine("firstname: {0} \n" +
                                "lastname: {1} \n" +

                                "department: {2} \n" +

                                "salary: {3}",
                                employee.FirstName,
                                employee.LastName,
                                employee.Department,
                                employee.Salary
                                );
                Console.ReadLine();
            }
            

        }
    }

    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Department { get; set; }
        public double Salary { get; set; }

        public void AddEmployee()
        {
            Console.WriteLine("Enter employee's firstname.");
            this.FirstName = Console.ReadLine();

            Console.WriteLine("Enter employee's lastname.");
            this.LastName = Console.ReadLine();

            Console.WriteLine("Enter employee's department.");
            this.Department = Console.ReadLine();

            Console.WriteLine("Enter employee's salary.");
            this.Salary = Convert.ToDouble(Console.ReadLine());
        }
    }

AnswerRe: Dynamically Create Objects Then Add Them To An Array Pin
dan!sh 14-Mar-16 22:20
professional dan!sh 14-Mar-16 22:20 
GeneralRe: Dynamically Create Objects Then Add Them To An Array Pin
MadDashCoder15-Mar-16 4:10
MadDashCoder15-Mar-16 4:10 
QuestionUse XDocument to load xm, show on gridview and textbox, then modify and save xml Pin
Christian Torres M14-Mar-16 8:13
Christian Torres M14-Mar-16 8:13 
AnswerRe: Use XDocument to load xm, show on gridview and textbox, then modify and save xml Pin
Dave Kreskowiak14-Mar-16 8:42
mveDave Kreskowiak14-Mar-16 8:42 
SuggestionRe: Use XDocument to load xm, show on gridview and textbox, then modify and save xml Pin
Sascha Lefèvre14-Mar-16 9:01
professionalSascha Lefèvre14-Mar-16 9:01 
QuestionObtaining USB HID device information Pin
Leif Simon Goodwin14-Mar-16 5:19
Leif Simon Goodwin14-Mar-16 5:19 
AnswerRe: Obtaining USB HID device information Pin
Garth J Lancaster14-Mar-16 14:51
professionalGarth J Lancaster14-Mar-16 14:51 
GeneralRe: Obtaining USB HID device information Pin
Leif Simon Goodwin29-Mar-16 1:13
Leif Simon Goodwin29-Mar-16 1:13 
AnswerRe: Obtaining USB HID device information Pin
GrooverFromHolland15-Mar-16 9:39
GrooverFromHolland15-Mar-16 9:39 
Questionconditional compilation Pin
joost.versteegen14-Mar-16 3:48
joost.versteegen14-Mar-16 3:48 
AnswerRe: conditional compilation Pin
OriginalGriff14-Mar-16 5:09
mveOriginalGriff14-Mar-16 5:09 
GeneralRe: conditional compilation Pin
joost.versteegen14-Mar-16 5:48
joost.versteegen14-Mar-16 5:48 
GeneralRe: conditional compilation Pin
Eddy Vluggen14-Mar-16 6:50
professionalEddy Vluggen14-Mar-16 6:50 
AnswerRe: conditional compilation Pin
Sascha Lefèvre14-Mar-16 6:18
professionalSascha Lefèvre14-Mar-16 6:18 
QuestionRe: conditional compilation Pin
John Torjo14-Mar-16 13:12
professionalJohn Torjo14-Mar-16 13:12 
AnswerRe: conditional compilation Pin
joost.versteegen14-Mar-16 21:32
joost.versteegen14-Mar-16 21:32 
GeneralRe: conditional compilation Pin
John Torjo14-Mar-16 21:41
professionalJohn Torjo14-Mar-16 21:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.