Big T War
Big T War

Reputation: 47

variable values remain static in new objects creation

Class 01

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

namespace lab06_ex02
{
    public class Employee01
    {
        public string EmployeeName01 { get; set; }
        public Employee01(string employeeName01)
        {
            EmployeeName01 = employeeName01;
        }
        public void InputEmployeeNames01()
        {
            Console.WriteLine("Please enter a name of an employee:");
            string employeeName01 = Console.ReadLine();
        }
        public void DisplayEmployeeNames01()
        {
            Console.WriteLine("The name of the first employee entered was: \n{0}\n", EmployeeName01);
        }
    }
}

Class 02

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

namespace lab06_ex02
{
    public class Employee02
    {

        public string EmployeeName02 { get; set; }
        public Employee02(string employeeName02)
        {
            EmployeeName02 = employeeName02;
        }
        public void InputEmployeeNames02()
        {
            Console.WriteLine("Please enter a name of another employee:");
            string employeeName02 = Console.ReadLine();
        }
        public void DisplayEmployeeNames02()
        {
            Console.WriteLine("The name of the second employee entered was: \n{0}\n", EmployeeName02);
        }
    }
}

and Class 03

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

namespace lab06_ex02
{
    public class EmployeeTest
    {
        public static void Main(string[] args)
        {
            //create Employee object myEmployee and pass string to constructor
            Employee01 myEmployee01 = new Employee01("");
            Employee02 myEmployee02 = new Employee02("");
            myEmployee01.InputEmployeeNames01();
            myEmployee02.InputEmployeeNames02();
            myEmployee01.DisplayEmployeeNames01();
            myEmployee02.DisplayEmployeeNames02();

            //Console.WriteLine("The name of the first employee was: ", myEmployee01.EmployeeName01);
            //Console.WriteLine();
            //Console.WriteLine("The name of the second employee was: ", myEmployee02.EmployeeName02);

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

Class 03 displays the input parameter values for object01 and object02 creation. How do I get class03 to display the user input for each name from class01 and class02? Thanks!

Upvotes: 0

Views: 30

Answers (1)

Chief Wiggum
Chief Wiggum

Reputation: 2934

Instead of assigning the value from the input into a new variable, assign it to the property. So instead of

public void InputEmployeeNames01()
{
    Console.WriteLine("Please enter a name of an employee:");
    string employeeName01 = Console.ReadLine();
}

Something like this might help:

public void InputEmployeeNames01()
{
    Console.WriteLine("Please enter a name of an employee:");
    EmployeeName01 = Console.ReadLine();
}

And obviously the same goes for Employee02.

But why create two classes for the employees? Why not a single employee class and then create two instances of that class?

public class Employee
{
    public string Name { get; set; }
    public Employee(string name)
    {
        Name = name;
    }
    public void SetName()
    {
        Console.WriteLine("Please enter a name of an employee:");
        Name = Console.ReadLine();
    }
    public void PrintName()
    {
        Console.WriteLine("The name of the employee entered was:");
        Console.WriteLine(Name);
    }
}

And then something like that:

public class EmployeeTest
{
    public static void Main(string[] args)
    {
        //create Employee object myEmployee and pass string to constructor
        var myEmployee01 = new Employee("Bob");
        var myEmployee02 = new Employee("Alice");
        myEmployee01.SetName();
        myEmployee02.SetName();
        myEmployee01.PrintName();
        myEmployee02.PrintName();

        //Console.WriteLine("The name of the first employee was: ", myEmployee01.Name);
        //Console.WriteLine();
        //Console.WriteLine("The name of the second employee was: ", myEmployee02.Name);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Upvotes: 1

Related Questions