none
Property age not changed through construction Why age always = 0 :( ? RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApp2
    {
        public abstract class Welcoming
        {
            public abstract void SayHi();
            public abstract  string SayGoodbye();
        }
        public class Person : Welcoming
        {
            private string name;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
            public override void SayHi()
            {
                Console.WriteLine("Hello {0}", Name);
            }
            public override string SayGoodbye()
            {
                return "Goodbye " + Name;
            }
            public Person(string name)
            {
                Name = name;
                SayHi();
                Console.WriteLine(SayGoodbye());
            }
        }
        public class Human : Person
        {
            private int age;
            public int Age
            {
                get
                {
                    return age;
                }
                set
                {
                    age = value;
                }
            }
            public override string SayGoodbye()
            {
                return "Goodbye " + Name + " Age = " + Age;
            }
            public override void SayHi()
            {
                base.SayHi();
                Console.WriteLine("Age {0}", Age);
            }
            public Human(string name, int agE) :base(name)
            {
                Age = agE;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Human male = new Human("Raiden", 32);
                Console.ReadLine();
            }
        }
    }

    Sunday, January 14, 2018 3:24 AM

All replies

  •         public Human(string name, int agE) :base(name)
            {
                Age = agE;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Human male = new Human("Raiden", 32);
                Console.ReadLine();
            }
        }
    }

    Use the debugger to step through the program one line at a time.
    You should see that the displayed lines all occur before the program
    reaches this line:

    Age = agE;

    - Wayne

    Sunday, January 14, 2018 4:30 AM
  •     class Program
        {
            static void Main(string[] args)
            {
                Human male = new Human("Raiden", 32);
                Console.ReadLine();
            }
        }



    To illustrate that the action - displaying Age - occurs
    too early, try adding this line to your code in Main:

    Human male = new Human("Raiden", 32);
    Console.WriteLine(male.SayGoodbye()); // <<<
    Console.ReadLine();
    

    The added line should show the age as 32.

    - Wayne


    Sunday, January 14, 2018 4:49 AM
  • I know already it will showed it, but isn't there any way to fix it in the human class to run as construction?!
    Monday, January 15, 2018 1:18 AM
  • Hello Yousif,

    I would recommend that you set the abstract class named "Welcoming" as interface, in my opinion an Interface defines what something can do and an abstract class defines what something is.

     >>but isn't there any way to fix it in the human class to run as construction?!

    Because the parent class's ctor executes before the constructor of the subclass, and also you need to wait the sub class ctor finishing executing. you should extract the ctor code of parent class into a method. The result should be like as below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp2
    {
    	public abstract class Welcoming
    	{
    		public abstract void SayHi();
    		public abstract string SayGoodbye();
    	}
    	public class Person : Welcoming
    	{
    		private string name;
    		public string Name
    		{
    			get
    			{
    				return name;
    			}
    			set
    			{
    				name = value;
    			}
    		}
    		public override void SayHi()
    		{
    			Console.WriteLine("Hello {0}", Name);
    		}
    		public override string SayGoodbye()
    		{
    			return "Goodbye " + Name;
    		}
    		public Person(string name)
    		{	
    		}
    		public void Test() {
    			Name = name;
    			SayHi();
    			Console.WriteLine(SayGoodbye());
    		}
    	}
    	public class Human : Person
    	{
    		private int age;
    		public int Age
    		{
    			get
    			{
    				return age;
    			}
    			set
    			{
    				age = value;
    			}
    		}
    		public override string SayGoodbye()
    		{
    			return "Goodbye " + Name + " Age = " + Age;
    		}
    		public override void SayHi()
    		{
    			base.SayHi();
    			Console.WriteLine("Age {0}", Age);
    		}
    		public Human(string name,int agE):base(name)
    		{
    			Age = agE;
    			base.Test();
    		}
    	}
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			Human male = new Human("Raiden", 32);
    			Console.ReadLine();
    		}
    	}
    }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 15, 2018 7:17 AM
    Moderator
  • Hello Yousif,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 1:10 PM
    Moderator