Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
I entered employee details in console while compiling, now i need to display those details in alist.

What I have tried:

namespace Program1
{
    class Employee
    {
        public string Empname = "";
        public int EMPID = 0;
        //public void print()
        //{
        //    Console.WriteLine("\n Employee Id is " + EMPID);
        //    Console.WriteLine("\n Employee Name is " + Empname);
        //}
        //Declare Employee ID
        public int EmployeeID
        {
            get;
            set;
        }
        //Declare Employee Name
        public string EmployeeName
        {
            get;
            set;
        }
        //public override string ToString()
        //{

        //    for (int i = 0; i < 24; i++) //just user the static array length
        //    {
        //        EMPID[i].Number = Console.ReadLine();
        //        Empname[i].Name = Console.ReadLine();
        //    }
        //    return "EmployeeID =" + EMPID + " , EmployeeName = " + Empname;
        //}
        // foreach (var item in newlist.OrderBy(Employee => Employee.empid)) Console.WriteLine(Employee);
    }
    class Program
    {
        public static void Main()
        {
           // Employee e = new Employee();// create object with employee 
           // //e.EmployeeID = 501;
           // //e.EmployeeName = "Anusha";
           // //Console.WriteLine(" Employee Data:{0}", e.EmployeeID);
           // //Console.WriteLine(" Employee Data:{0}", e.EmployeeName);
           // //Console.ReadLine();
           //for (int i = 0; i<2; i++)
           // {
            //    Console.WriteLine("Enter Employee ID", e.EmployeeID);
            //    Console.ReadLine();
            //    Console.WriteLine("Enter Employee name", e.EmployeeName);
            //    Console.ReadLine();
            //    string line = Console.ReadLine();
            //    if (line == "exit")
            //    {
            //        break;
            //    }
            //}
           //Console.WriteLine("Employee ID :" + e.EMPID + '\t' + "Employee Name :" + e.Empname);
           //Console.ReadLine();         
            Console.WriteLine(" Enter Employee ID");
            var EmpID = int.Parse(Console.ReadLine());
            var employee1 = new Employee
            {
                EmployeeID= EmpID;
            }
        }
    }

        //public class Book
        //{
        //    public string BookName { get; set; } = string.Empty;
        //    public string Author { get; set; } = string.Empty;
        //    public string ISBN { get; set; } = string.Empty;
        //}
}
Posted
Updated 4-Sep-18 20:32pm
Comments
dan!sh 5-Sep-18 1:58am    
Can you update the question with explanation as to what you are trying to do? It is not quite clear.
Member 13818142 5-Sep-18 2:08am    
.i have written below code using get and set methods to enter employee Details, now if i enter more than two employee details i want to display them in a list ... how can i display values which was enterred in console in a list
class Employee
{
public string Empname = "";
public int EMPID = 0;
//public void print()
//{
// Console.WriteLine("\n Employee Id is " + EMPID);
// Console.WriteLine("\n Employee Name is " + Empname);
//}
//Declare Employee ID
public int EmployeeID
{
get;
set;
}
//Declare Employee Name
public string EmployeeName
{
get;
set;
}
Console.WriteLine(Employee);
}
class Program
{
public static void Main()
{
Employee e = new Employee();// create object with employee
for (int i = 0; i<2; i++)
{
Console.WriteLine("Enter Employee ID", e.EmployeeID);
Console.ReadLine();
Console.WriteLine("Enter Employee name", e.EmployeeName);
Console.ReadLine();
string line = Console.ReadLine();
if (line == "exit")
{
break;
}
}
Console.WriteLine("Employee ID :" + e.EMPID + '\t' + "Employee Name :" + e.Empname);
Console.ReadLine();
}
}

//public class Book
//{
// public string BookName { get; set; } = string.Empty;
// public string Author { get; set; } = string.Empty;
// public string ISBN { get; set; } = string.Empty;
//}
}
TommoDotCommo 5-Sep-18 2:10am    
It doesn't look like you intend to use a database, so all of the information you enter will only be available until you close the application.

Your program also only has the ability to ask for one employee ID, so there is no use displaying a list of one employee.

What exactly do you want your program to do?
Member 13818142 5-Sep-18 2:16am    
I am entering employee details in console.. so i want to read data what i have entered in console and want to prepare a list and dipslay all the details again

Example: my o/p in console
Enter employee id :501
enter employee name : XXXXXX
Enter employee id :502
enter employee name : XXXXXX
Enter employee id :503
enter employee name : XXXXXX
This way i have entered 3 employee details so i want to read those details and display in a list likethis
Employee ids:501,502,503..
empoyee names:xxxx
Sinisa Hajnal 5-Sep-18 2:11am    
What kind of list? You're in console app, you just write out strings formatted as you need them to be.

Also, it may be useful if you describe what you're trying to do with your app in general, not just this particular task.

Your problem is here:
C#
public static void Main()
    {
    Employee e = new Employee();// create object with employee 
    for (int i = 0; i<2; i++)
        {
        Console.WriteLine("Enter Employee ID", e.EmployeeID);
        Console.ReadLine();
        Console.WriteLine("Enter Employee name", e.EmployeeName);
        Console.ReadLine();
        string line = Console.ReadLine();
        if (line == "exit")
            {
            break;
            }
        }
    Console.WriteLine("Employee ID :" + e.EMPID + '\t' + "Employee Name :" + e.Empname);
    Console.ReadLine(); 
    }
You only ever create one instance of an Employee - so inside your loop all you could do (if your code did anything, and it won't even compile at the moment, much less do anything useful) - is overwrite the content you already have.

It won't compile because your EMployee class includes code that isn't inside a method:
C#
class Employee
    {
    public string Empname = "";
    public int EMPID = 0;
    public int EmployeeID
        {
        get;
        set;
        }
    public string EmployeeName
        {
        get;
        set;
        }
    Console.WriteLine(Employee);  <<<---- This line isn't in a method
    }

But you really need to go back to basics and start again...
C#
class Employee
    {
    public int EmployeeID { get; set; }
    public string EmployeeName { get; set; }
    }
Will do for your Employee class.
Now, to use it:
C#
public static void Main()
    {
    List<Employee> employees = new List<Employee>();
    for (int i = 0; i<2; i++)
        {
        Employee employee = new Employee();
        Console.WriteLine("Enter Employee name", e.EmployeeName);
        string line = Console.ReadLine();
        if (line == "exit")
            {
            break;
            }
        ... Fill in Employee details here
        employees.Add(employee);
        }
    foreach (Employee employee in employees)
        {
        ... print Employee details here
        }
    Console.ReadLine(); 
    }
 
Share this answer
 
Comments
Member 13818142 5-Sep-18 2:41am    
@orginal griff thanks for solution but i am not able to print employee details i am getting value as zero and null .please suggest
OriginalGriff 5-Sep-18 2:48am    
And how do you expect me to have any idea why when you don't show me the actual code you used, or tell me what the debugger has shown you? You did try the debugger first, didn't you?
Member 13818142 5-Sep-18 2:53am    
@Griff I am getting the out put with values its a silly mistake i made , but now if i enter 2 values whileprinting the data i am getting only the last value printed
suppose i enter 2 employee ids 501 and 502 while i am printing i am getting only 502
OriginalGriff 5-Sep-18 2:58am    
At the risk of repeating myself: "And how do you expect me to have any idea why when you don't show me the actual code you used, or tell me what the debugger has shown you?"

I am pretty sure I know exactly what you have done wrong - you didn't read what I wrote or look at the code I gave you carefully enough - but without your code we have no idea what you actually tried, and little differences have a big impact in this business. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
You need to create a list, and add the created Employee objects to that list during each loop. Then at the end you can work with the list you created.

C#
public static void Main()
{
	List<Employee> employees = new List<Employee>();
	for (int i = 0; i<2; i++)
	{
		Employee e = new Employee();// create object with employee
		Console.WriteLine("Enter Employee ID:");
		e.EmployeeID = int.Parse(Console.ReadLine());
		Console.WriteLine("Enter Employee name:");
		e.EmployeeName = Console.ReadLine();
		employees.Add(e);
		string line = Console.ReadLine();
		if (line == "exit")
		{
			break;
		}
	}
	foreach (Employee employee in employees)
	{
		Console.WriteLine("Employee ID :" + employee.EmployeeID + '\t' + "Employee Name :" + employee.EmployeeName);
		Console.ReadLine();
	}
}
 
Share this answer
 
v2
Comments
Member 13818142 5-Sep-18 2:43am    
Thank you sir for your solution but i need to do it using get and set methods,
TommoDotCommo 5-Sep-18 2:46am    
My answer requires the Employee class you created and uses get and set methods. It's clear at this point that you have a limited understanding of the larger concepts here, so I think it's best if you go back to the original code/question you've been given by your teacher and look over some examples they've provided in more detail.
Member 13818142 5-Sep-18 2:49am    
Thank you sir, i got it but getting the same value which i entered last. not getting all the employee id's enetred.
TommoDotCommo 5-Sep-18 2:53am    
You're probably mixing up your "e" and "employee" parts of the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900