-1

In this code:

using System;

namespace ConsoleApp1
{
    public class TextInput
    {
        protected string _text = "";

        public void Add(char c)
        {
            _text += c;
        }

        public string GetValue()
        {
            return _text;
        }
    }


    public class NumericInput : TextInput
    {
        public new void Add(char c)
        {
            if (!char.IsDigit(c)) return;
            _text += c;
        }
    }


    public class Program
    {
        public static void Main(string[] args)
        {
            TextInput input = new NumericInput();
            input.Add('1');
            input.Add('a');
            input.Add('0');
            Console.WriteLine(input.GetValue());

            Console.WriteLine(char.IsDigit('1'));
            Console.WriteLine(char.IsDigit('a'));
            Console.WriteLine(char.IsDigit('0'));
        }
    }
}

... calling Console.WriteLine(char.IsDigit('a')); returns correctly False but in the overridden Add method it always return True.

Obviously it calls TextInput.Add() instead of NumericInput.Add(). Can this be corrected inside the overriden Add() method? Code in Main may not change!

BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
  • 1
    Make `Add` virtual and override it with `override` keyword – Aleks Andreev Jul 05 '18 at 17:33
  • @AleksAndreev Would that be the only possible option in this case? – BadmintonCat Jul 05 '18 at 17:37
  • Can you please explain us why you use new keyword? if you are trying to achieve method overriding. Second thing, here you are creating instance of base class `TextInput` and calling `Add()`, execution will not go into method present in derived class will not check `IsDigit()` condition. Please provide us some valid example – Prasad Telkikar Jul 05 '18 at 17:40
  • Here is output of your example: https://dotnetfiddle.net/J9Xjon – Prasad Telkikar Jul 05 '18 at 17:42
  • I think this answer will help a lot: https://stackoverflow.com/a/3838692/6300600 – Swift Jul 05 '18 at 17:46

3 Answers3

1

Check out the difference between new and override in c#. Plus, take a look at virtual key.

I'm not going to explain it now. There is a lot of information about this here and on the web.

MS docs: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords

More info here: Difference between new and override

To sum up, use override in this case. Try them both and you'll realize how do they work. There

Antonio Rodríguez
  • 976
  • 2
  • 11
  • 25
0

If you want to implement method overriding, use virtual keyword in base class and override keyword in derived class.

namespace ConsoleApp1
{
    public class TextInput
    {
        protected string _text = "";
        //Virtual allows derived class to enhance its functionality.
        public virtual void Add(char c)
        {
            _text += c;
        }
        ....
    }


    public class NumericInput : TextInput
    {
        //Override function will add more functionality to base class function
        public override void Add(char c)
        {
            if (!char.IsDigit(c)) return;
            _text += c;
        }
    }

Now new keyword does not override base class function, it will hide base class functionality.

If you want to override function in derived class, then use override instead of new keyword

For more reference, visit: Stackoverflow thread

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

why don't you use abstract class:

since we have multiple add implementation and also can not use operators on T, declare it as abstract and implement on derived classes

public abstract class Input<T>
    {
        protected T __input;
        public abstract void Add(T c);
        public T GetValue()
        {
            return __input;
        }
    }
    public class NumericInput : Input<int>
    {
        public override void Add(int c)
        {
            __input += c;
        }
    }
    public class TextInput : Input<string>
    {
        public override void Add(string c)
        {
            __input += c;
        }
    }

static void Main(string[] args)
        {
            var input = new NumericInput();
            input.Add(1);
            input.Add(2);
            Console.WriteLine(input.GetValue());

            var text = new TextInput();
            text.Add("a");
            text.Add("a");
            text.Add("a");
            Console.WriteLine(text.GetValue());

            Console.ReadLine();
        }
Arsalan
  • 709
  • 2
  • 14
  • 27