Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. method inheretance

method inheretance

Scheduled Pinned Locked Moved C#
questionhelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Deresen
    wrote on last edited by
    #1

    Hello everyone, I'm building my own library. But I'm kind of stuck and it is really tough to explain, but I will try: I have a class, let's call this X. X has an interface and a base class. X also inherets another class (called Y). Now I call some base functions and give X as a parameter. But I would like to know if it is possible in another baseclass? To sketch it up in code, it looks like this now:

    class X : Y, InterfaceX
    {
    public X()
    {
    }

    bool function1(String txt)
    {
    return base.function1(this, txt);
    }
    }

    class Y
    {
    bool function1(InterfaceX x, String txt)
    {
    //do something
    return true/false;
    }
    }

    And what I would like to have is something like this:

    class X : Y (& Z), InterfaceX
    {
    public X()
    {
    }
    }

    class Y
    {
    bool function1(InterfaceX x, String txt)
    {
    //do something
    return true/false;
    }
    }

    class Z
    {

    bool function1(String txt)
    {
    return base.function1(this.getParent(), txt);
    }
    }

    Is this possible, and if it is, how can I implement this? Thank you very much.

    A 1 Reply Last reply
    0
    • D Deresen

      Hello everyone, I'm building my own library. But I'm kind of stuck and it is really tough to explain, but I will try: I have a class, let's call this X. X has an interface and a base class. X also inherets another class (called Y). Now I call some base functions and give X as a parameter. But I would like to know if it is possible in another baseclass? To sketch it up in code, it looks like this now:

      class X : Y, InterfaceX
      {
      public X()
      {
      }

      bool function1(String txt)
      {
      return base.function1(this, txt);
      }
      }

      class Y
      {
      bool function1(InterfaceX x, String txt)
      {
      //do something
      return true/false;
      }
      }

      And what I would like to have is something like this:

      class X : Y (& Z), InterfaceX
      {
      public X()
      {
      }
      }

      class Y
      {
      bool function1(InterfaceX x, String txt)
      {
      //do something
      return true/false;
      }
      }

      class Z
      {

      bool function1(String txt)
      {
      return base.function1(this.getParent(), txt);
      }
      }

      Is this possible, and if it is, how can I implement this? Thank you very much.

      A Offline
      A Offline
      Alan Balkany
      wrote on last edited by
      #2

      Class X can't inherit from Y and Z because C# only supports single inheritance (unfortunately). You could have X inherit from Y, and Y inherit from Z. Or you could have X implement multiple interfaces. Also you need the keyword "virtual" on the virtual methods in your base classes, and the keyword "override" on your overridden methods in the derived classes. Another possibility is the Decorator design pattern, which lets you define inheritance (single or multiple) at runtime.

      D 1 Reply Last reply
      0
      • A Alan Balkany

        Class X can't inherit from Y and Z because C# only supports single inheritance (unfortunately). You could have X inherit from Y, and Y inherit from Z. Or you could have X implement multiple interfaces. Also you need the keyword "virtual" on the virtual methods in your base classes, and the keyword "override" on your overridden methods in the derived classes. Another possibility is the Decorator design pattern, which lets you define inheritance (single or multiple) at runtime.

        D Offline
        D Offline
        Deresen
        wrote on last edited by
        #3

        Thank you. But the problem I have is that if I want to add a function to my Interface class and it is as simple as calling the base function with as parameter itself, shouldn't it be possible to do that in some kind of global class. Something like this:

        class Main
        {
        public Main()
        {
        X x = new X();
        x.function1("myText"); -> false
        X2 x2 = new X2();
        x2.function1("myText"); -> true
        }
        }

        interface InterfaceX
        {
        int number { get; };
        bool function1(String txt);
        }

        class X : Z, InterfaceX
        {
        public int number
        {
        get { return 5;}
        }
        public X(){}
        }
        class X2 : Z, InterfaceX
        {
        public int number
        {
        get { return 2;}
        }
        public X2() {}
        }

        class Z : Y,
        {
        public bool function1(String txt)
        {
        return base.function2(this.getParent(), txt);
        }
        }

        class Y
        {
        public bool function2(InterfaceX x, String txt)
        {
        if(x.number < 3)
        return true;
        else
        return false;
        }
        }

        It's all about the this.getParent(), this doesn't work, but how can I get this working?

        A B 2 Replies Last reply
        0
        • D Deresen

          Thank you. But the problem I have is that if I want to add a function to my Interface class and it is as simple as calling the base function with as parameter itself, shouldn't it be possible to do that in some kind of global class. Something like this:

          class Main
          {
          public Main()
          {
          X x = new X();
          x.function1("myText"); -> false
          X2 x2 = new X2();
          x2.function1("myText"); -> true
          }
          }

          interface InterfaceX
          {
          int number { get; };
          bool function1(String txt);
          }

          class X : Z, InterfaceX
          {
          public int number
          {
          get { return 5;}
          }
          public X(){}
          }
          class X2 : Z, InterfaceX
          {
          public int number
          {
          get { return 2;}
          }
          public X2() {}
          }

          class Z : Y,
          {
          public bool function1(String txt)
          {
          return base.function2(this.getParent(), txt);
          }
          }

          class Y
          {
          public bool function2(InterfaceX x, String txt)
          {
          if(x.number < 3)
          return true;
          else
          return false;
          }
          }

          It's all about the this.getParent(), this doesn't work, but how can I get this working?

          A Offline
          A Offline
          Alan Balkany
          wrote on last edited by
          #4

          I'm not sure what you're trying to do, but this.getParent () will only work if: 1. Class Z (or an ancestor of Z) defines a getParent () method, and 2. This getParent () method returns InterfaceX (or a descendant of InterfaceX).

          D 1 Reply Last reply
          0
          • A Alan Balkany

            I'm not sure what you're trying to do, but this.getParent () will only work if: 1. Class Z (or an ancestor of Z) defines a getParent () method, and 2. This getParent () method returns InterfaceX (or a descendant of InterfaceX).

            D Offline
            D Offline
            Deresen
            wrote on last edited by
            #5

            With the this.getParent() I would like to get the 'parent' of Z. So if I make an X : Z I would like to get X. And if I make an BlahBlah : Z I would like to get BlahBlah. I'm trying to get some work of my back. In stead of implementing all the functions (of the interface) inside of every class, I would like to make a base class which implements these functions.

            A 1 Reply Last reply
            0
            • D Deresen

              With the this.getParent() I would like to get the 'parent' of Z. So if I make an X : Z I would like to get X. And if I make an BlahBlah : Z I would like to get BlahBlah. I'm trying to get some work of my back. In stead of implementing all the functions (of the interface) inside of every class, I would like to make a base class which implements these functions.

              A Offline
              A Offline
              Alan Balkany
              wrote on last edited by
              #6

              If you put all the interface functions in a base class, they will automatically be available to all the derived classes. If this doesn't work for you, you can define a class that implements all the interface functions, and define an instance of this in any class that that implements the interface. Then for each interface function, forward the call to this instance. This avoids having to re-implement the whole functions in each class that implements the interface. For example: interface InterfaceX { // Lots of interface function definitions... } class ImplementationOfInterfaceX : InterfaceX { // Implementation of each function... } class X : InterfaceX { ImplementationOfInterfaceX implX; public int func1 (int par) { return implX.func1 (par); // I.e., a forwarded call instead of a 100-line function. } public float func2 (float par) { return implX.func2 (par); } ... } Of course this would be much easier if C# allowed multiple inheritance. I'm hoping they will in a future version. It makes some things very easy. It seems to me they left it out because it allows complexity and confusion for some extreme contrived cases that no one in their right mind would do. Not a good justification for leaving it out (in my biased opinion).

              1 Reply Last reply
              0
              • D Deresen

                Thank you. But the problem I have is that if I want to add a function to my Interface class and it is as simple as calling the base function with as parameter itself, shouldn't it be possible to do that in some kind of global class. Something like this:

                class Main
                {
                public Main()
                {
                X x = new X();
                x.function1("myText"); -> false
                X2 x2 = new X2();
                x2.function1("myText"); -> true
                }
                }

                interface InterfaceX
                {
                int number { get; };
                bool function1(String txt);
                }

                class X : Z, InterfaceX
                {
                public int number
                {
                get { return 5;}
                }
                public X(){}
                }
                class X2 : Z, InterfaceX
                {
                public int number
                {
                get { return 2;}
                }
                public X2() {}
                }

                class Z : Y,
                {
                public bool function1(String txt)
                {
                return base.function2(this.getParent(), txt);
                }
                }

                class Y
                {
                public bool function2(InterfaceX x, String txt)
                {
                if(x.number < 3)
                return true;
                else
                return false;
                }
                }

                It's all about the this.getParent(), this doesn't work, but how can I get this working?

                B Offline
                B Offline
                Ben Fair
                wrote on last edited by
                #7

                The crux of the problem is that you want class Z to pass an item into a function that accepts an InterfaceX item, but class Z is not necessarily an InterfaceX. So, the only way to do it as-is is to do this messy kind of check. Ideally, you'd have the call to function2 be in a class that is an InterfaceX. Passing a Z that is not an InterfaceX won't work, so you'll have figure out how you want to handle that situation. Here's a class Z that should work:

                class Z : Y
                {
                public bool function1(String txt)
                {
                if(this is InterfaceX)
                return base.function2((InterfaceX)this, txt);
                else
                return false; // maybe throw exception?
                }
                }

                Keep It Simple Stupid! (KISS)

                D 1 Reply Last reply
                0
                • B Ben Fair

                  The crux of the problem is that you want class Z to pass an item into a function that accepts an InterfaceX item, but class Z is not necessarily an InterfaceX. So, the only way to do it as-is is to do this messy kind of check. Ideally, you'd have the call to function2 be in a class that is an InterfaceX. Passing a Z that is not an InterfaceX won't work, so you'll have figure out how you want to handle that situation. Here's a class Z that should work:

                  class Z : Y
                  {
                  public bool function1(String txt)
                  {
                  if(this is InterfaceX)
                  return base.function2((InterfaceX)this, txt);
                  else
                  return false; // maybe throw exception?
                  }
                  }

                  Keep It Simple Stupid! (KISS)

                  D Offline
                  D Offline
                  Deresen
                  wrote on last edited by
                  #8

                  Thank you very much, I'll try to do it this way.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups