Skip to content

Newbie overriding problem

Jack

Asked Dec 24, 2008, 10:25 PM EST1 answer
Share
LinkedInX
I can't work out how to override a function, I spent 3 hours reading and searching to no luck.

What I am trying to do is to have a function lets say called blah in a class lets say class1 that will return "One" to me. Then in another class, say class2 another function also called blah would return "Two".
Up to here it is ok

class TestClass
{
   public class Class1
   {
      public virtual blah()
      {
         return "One";
      }
   }
   class Class2: Class1
   {
      public override blah()
      {
         return "Two";
      }
   }

Then in my main I would like that depending on a variable or button status the first or second blah() is called but the command to call it must be the same, I cant one time use
Code
Class1 s = new blah();

and the second time use

Class2 s = new blah(x);
That part I can't work out how to do. It is probably ultra simple or maybe I took the wrong way.

In any case, your help is much appreciated.




 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    you can alternate the method calls by doing this but for one time

    private void button1_Click(object sender, RoutedEventArgs e)
            {
              
                CallTheWriteMethod();
            }
            bool alternative;
            private void CallTheWriteMethod()
            {
                if (alternative == true)
                {
                    MessageBox.Show("alternative 1");  
                }
                if (alternative == false)
                {
                    MessageBox.Show("alternative 2");   
                }
                if (alternative == true) alternative = false;  
            }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                alternative = true;
            }
    0