0

here is my code. I want to increase speed value with a.Quest() method. This method is in the first class. And the second class inherits from first class.

Increasing method is in second class but I use it in first class. That's what I'm trying to do. I don't want to write so much code in main.

However when I run the program, both a.Speed and b.Speed is equal to zero. I expect one of them to be 10. How can I fix that?

    interface IElektroBeyin
    {
        int Speed{ get; set; } // Hız kontrolü
    }
    class AA : IElektroBeyin
    {
        public int Speed { get; set; }
        public virtual void GetFaster() { }
        public virtual void GetSlower() { }
        public  void Quest()
        {
            int x;                
            Console.WriteLine("Want to get faster ? 1- Yes");
            x = Console.Read();
            if (x == 1)
            {
                GetFaster();
            }
        }
    }
    class BB : AA
    {
        public override void GetFaster()
        {
               Speed += 10;
        }
        public override void GetSlower()
        {
                Speed -= 10;
        }
    }

    static void Main(string[] args)
    {
        BB b = new BB();
        AA a = new AA();
        a.Quest();
        Console.WriteLine(b.Speed);
        Console.WriteLine(a.Speed);
        Console.ReadLine();
    }
  }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
PreS
  • 43
  • 1
  • 5
  • `Console.Read` does not read an integer, it gives you the integer of the character read. You should be using `Console.ReadKey` and using the `KeyChar` property – Camilo Terevinto Apr 29 '18 at 18:02
  • 1
    Possible duplicate of [Console.Read() and Console.ReadLine() problems](https://stackoverflow.com/questions/12308098/console-read-and-console-readline-problems) – Camilo Terevinto Apr 29 '18 at 18:05
  • You are calling a.Quest, and a is of class AA, and the virtual methods in AA doesn't increment Speed, so nothing will happen. You need to call b.Quest to make the speed in b change. – Bent Tranberg Apr 29 '18 at 18:06

1 Answers1

0
AA a = new AA();
a.Quest();

AA.Quest() calls AA.GetFaster(), but this method is empty. That's why nothing happens when it is called. BB.GetFaster() is not called since it is in a derived class of AA. You probably want it to be the other way around.

Further, to get a number that a user enters, you can't use the return value of Console.Read(). Use int.Parse(Console.ReadLine()) or something similar.

adjan
  • 13,371
  • 2
  • 31
  • 48