sazr
sazr

Reputation: 25938

Override Parent Constant Variables

I am attempting to override a classes constant variables but my approach isn't working.

For the below code:

// The following line of code should output "DATE: 9/9/2014"
// But it outputs: "DATE: 31/3/2015"
CustomDateChecker.checkUsedByDate(); 

In the below class; how can I override the variables USED_BY_YEAR, USED_BY_MONTH and USED_BY_DAY?

public class DateChecker
{
    public const uint                   USED_BY_YEAR            = 2015;
    public const uint                   USED_BY_MONTH           = 3;
    public const uint                   USED_BY_DAY             = 31;

    public static bool checkUsedByDate()
    {
        TaskDialog.MessageBox.Show(string.Format("DATE: {0}/{1}/{2}", USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR));
    }
}

public class CustomDateChecker : DateChecker
{
    // Override Date
    public const uint                   USED_BY_YEAR            = 2014;
    public const uint                   USED_BY_MONTH           = 9;
    public const uint                   USED_BY_DAY             = 9;
}

Edit: After reading the advice I've changed the variables to static readonly instead of const. But unfortunately its the same issue, the date doesn't change. Is using static readonly a possible solution?

public class DateChecker
{
    public static readonly uint                   USED_BY_YEAR            = 2015;
    public static readonly uint                   USED_BY_MONTH           = 3;
    public static readonly uint                   USED_BY_DAY             = 31;

    public static bool checkUsedByDate()
    {
        TaskDialog.MessageBox.Show(string.Format("DATE: {0}/{1}/{2}", USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR));
    }
}

public class CustomDateChecker : DateChecker
{
    // Override Date
    public static new readonly uint                   USED_BY_YEAR            = 2014;
    public static new readonly uint                   USED_BY_MONTH           = 9;
    public static new readonly uint                   USED_BY_DAY             = 9;
}

Upvotes: 0

Views: 329

Answers (2)

Shaggy
Shaggy

Reputation: 73

  1. You are trying to override a field or member. Overriding is a feature associated with Methods/properties not with fields or members of the class.
  2. Other problem over here is you are trying to use static method to get polymorphic behavior which will not succeed. Static methods are associated with class and not instance of a class. So static methods cannot be used with virtual/override qualifier against it. For that one should use instance methods/non-static methods or properties.
  3. See if below example helps.

            public class DateChecker
            {
                public virtual uint USED_BY_YEAR { get; set; }
                public virtual uint USED_BY_MONTH { get; set; }
                public virtual uint USED_BY_DAY { get; set; }
                public DateChecker()
                {
                    USED_BY_YEAR = 2015;
                    USED_BY_MONTH = 3;
                    USED_BY_DAY = 31;
    
                }
                public virtual void checkUsedByDate()
                {
                    Console.Write(string.Format("DATE: {0}/{1}/{2}", USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR));
                }
            }
    
         public class CustomDateChecker : DateChecker
        {
            public override uint USED_BY_DAY
            {
                get
                {
                    return 9;
                }
                set
                {
                    base.USED_BY_DAY = value;
                }
            }
    
            public override uint USED_BY_MONTH
            {
                get
                {
                    return 9;
                }
                set
                {
                    base.USED_BY_MONTH = value;
                }
            }
    
            public override uint USED_BY_YEAR
            {
                get
                {
                    return 2014;
                }
                set
                {
                    base.USED_BY_YEAR = value;
                }
            }        
        }
        class Program
        {        
            static void Main(string[] args)
            {
                new CustomDateChecker().checkUsedByDate();
            }
        }
    

Upvotes: 0

codebased
codebased

Reputation: 7073

public class DateChecker
    {
        public readonly uint USED_BY_YEAR = 2015;
        public readonly uint USED_BY_MONTH = 3;
        public readonly uint USED_BY_DAY = 31;

        public DateChecker()
        {

        }

        public DateChecker(uint d, uint m, uint y)
        {
            this.USED_BY_DAY = d;
            this.USED_BY_MONTH = m;
            this.USED_BY_YEAR = y;
        }

        public bool checkUsedByDate()
        {
            Console.WriteLine(string.Format("DATE: {0}/{1}/{2}", USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR));
            return false;
        }
    }

    public class CustomDateChecker : DateChecker
    {
        // Override Date
        public new const uint USED_BY_YEAR = 2014;
        public new const uint USED_BY_MONTH = 9;
        public new const uint USED_BY_DAY = 9;

        public CustomDateChecker()
            : base(USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR)
        {
        }


    }

    private static void Main(string[] args)
    {
        CustomDateChecker d = new CustomDateChecker();
        d.checkUsedByDate();
    }

Upvotes: 1

Related Questions