Question

HEllo I have an OOP Homewrok using class composition , I've Created Two Class 1.date and I can call it in the main 2.employee but I can't call it to create an object here is the code

namespace ConsoleApplication1
{
    class Program
    {
        public class date
        {
            private int day;
            private int mounth;
            private int year;

            public date(int day, int mounth, int year)
            {
                this.day = day;
                this.mounth = mounth;
                this.year = year;

            }
            public void Display()
            {

                Console.WriteLine("{0}/{1}/{2}", day, mounth, year);

            }

            public class employee
            {
                private string name;
                private date hire_date;
                private date Emplye_birth;
                private int number;

                public employee(int number, string name, date bd, date hd)
                {
                    this.name = name;
                    this.number = number;
                    hire_date = hd;
                    Emplye_birth = bd;

                }
                public void printEmplyy()
                {
                    Console.WriteLine("Employee number {0} \n name:{1}", number, name);
                    Console.WriteLine("hire date of {0}", name);
                    hire_date.Display();
                    Console.WriteLine("birthday of {0}", name);
                    Emplye_birth.Display();

                }
            }

        }
        static void Main(string[] args)
        {

            date Khaled_birth = new date(10, 10, 1995);
            date khaled_hire = new date(10, 10, 2017);
            string user;
            int number;
            Console.WriteLine("Please Enter the user and password ");
            Console.Write("USERNAME:"); user = Console.ReadLine();
            Console.Write("Password"); number = Convert.ToInt32(Console.ReadLine());
            /*
            employee khaled_info = new employee(number, user, khaled_hire, Khaled_birth); 
           that code wont to run 
             */
            if (user == "khaled" && number == 001995)
            {
                /*    khaled_info.printEmplyy(); 
               that code wont to run
                 */
            }

        }
    }
}
Was it helpful?

Solution

Your employee class is nested inside of your date class. If you pull it out, then you can access it. Like so:

public class date
{
    private int day;
    private int month;
    private int year;

    public date(int day, int month, int year)
    {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public void Display()
    {
        Console.WriteLine("{0}/{1}/{2}", day, month, year);
    }
}

public class employee
{
    private string name;
    private date hire_date;
    private date Employee_birth;
    private int number;

    public employee(int number, string name, date bd, date hd)
    {
        this.name = name;
        this.number = number;
        hire_date = hd;
        Employee_birth = bd;
    }

    public void printEmployee()
    {
        Console.WriteLine("Employee number {0} \n name:{1}", number, name);
        Console.WriteLine("hire date of {0}", name);
        hire_date.Display();
        Console.WriteLine("birthday of {0}", name);
        Employee_birth.Display();
    }
}

OTHER TIPS

Or use full path to employee class like:

Program.date.employee khaled_info = new Program.date.employee(...);

BTW you have 1 string formatting error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top