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. Create an object of an inherited class

Create an object of an inherited class

Scheduled Pinned Locked Moved C#
csharphelpquestionworkspace
7 Posts 4 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.
  • P Offline
    P Offline
    popchecker
    wrote on last edited by
    #1

    Hi All, I am developing a C# library. In that library there is an abstract method in an abstract class which let the users to implement it. Now I want to call that method in some other parts of my library. My problem is that the library aware only about the abstract class not about the class which user implemented. This is something I want to implement.

    abstract class MyBase
    {
    abstract void DoSomething();
    }

    class AnotherPartOfLibrary
    {
    public void AnotherFunction()
    {
    //here I want to call MyBase.DoSomething()
    }
    }

    /* User Implementation of my library */
    class UserImplementation : MyBase
    {
    public void DoSomething(){}
    }

    All I need to let the user to extent my library and I want to call these extended functionalities in some parts of library. One method I found that to force the user for an entry of user class name in configuration file. So that I can create an instance of user class using reflection. But is there any other methods to do this? Thanks & Regards, Thomas

    T W D 3 Replies Last reply
    0
    • P popchecker

      Hi All, I am developing a C# library. In that library there is an abstract method in an abstract class which let the users to implement it. Now I want to call that method in some other parts of my library. My problem is that the library aware only about the abstract class not about the class which user implemented. This is something I want to implement.

      abstract class MyBase
      {
      abstract void DoSomething();
      }

      class AnotherPartOfLibrary
      {
      public void AnotherFunction()
      {
      //here I want to call MyBase.DoSomething()
      }
      }

      /* User Implementation of my library */
      class UserImplementation : MyBase
      {
      public void DoSomething(){}
      }

      All I need to let the user to extent my library and I want to call these extended functionalities in some parts of library. One method I found that to force the user for an entry of user class name in configuration file. So that I can create an instance of user class using reflection. But is there any other methods to do this? Thanks & Regards, Thomas

      T Offline
      T Offline
      T M Gray
      wrote on last edited by
      #2

      Does AnotherPartOfLibrary inherit from MyBase? If not, what is the relationship between the two classes? You can't call MyBase.DoSomething() because it is abstract. If everything in the base is abstract are you sure you want a base class and not an Interface?

      P 1 Reply Last reply
      0
      • P popchecker

        Hi All, I am developing a C# library. In that library there is an abstract method in an abstract class which let the users to implement it. Now I want to call that method in some other parts of my library. My problem is that the library aware only about the abstract class not about the class which user implemented. This is something I want to implement.

        abstract class MyBase
        {
        abstract void DoSomething();
        }

        class AnotherPartOfLibrary
        {
        public void AnotherFunction()
        {
        //here I want to call MyBase.DoSomething()
        }
        }

        /* User Implementation of my library */
        class UserImplementation : MyBase
        {
        public void DoSomething(){}
        }

        All I need to let the user to extent my library and I want to call these extended functionalities in some parts of library. One method I found that to force the user for an entry of user class name in configuration file. So that I can create an instance of user class using reflection. But is there any other methods to do this? Thanks & Regards, Thomas

        W Offline
        W Offline
        William Winner
        wrote on last edited by
        #3

        I agree with the other answer...are you sure you don't want an interface and not an abstract class? Also, how are you not getting errors with what's written anyway? First, you need to specify that abstract void DoSomething() is public. Secondly, in order to implement it, you have to override it in the class that inherits from it. In other words, it should be

        public override void DoSomething()

        And why would you ever want to call a method within an abstract class? You want to call a method within a class that inherits that abstract class. Unless, you just want an interface. If you just want an interface, you would want something like this:

        P 1 Reply Last reply
        0
        • P popchecker

          Hi All, I am developing a C# library. In that library there is an abstract method in an abstract class which let the users to implement it. Now I want to call that method in some other parts of my library. My problem is that the library aware only about the abstract class not about the class which user implemented. This is something I want to implement.

          abstract class MyBase
          {
          abstract void DoSomething();
          }

          class AnotherPartOfLibrary
          {
          public void AnotherFunction()
          {
          //here I want to call MyBase.DoSomething()
          }
          }

          /* User Implementation of my library */
          class UserImplementation : MyBase
          {
          public void DoSomething(){}
          }

          All I need to let the user to extent my library and I want to call these extended functionalities in some parts of library. One method I found that to force the user for an entry of user class name in configuration file. So that I can create an instance of user class using reflection. But is there any other methods to do this? Thanks & Regards, Thomas

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          To do this you need to either have a parameter of MyBase or ineriting class in the method

          public abstract class MyBase
          {
          public abstract void DoSomething();
          }
          public class AnotherPartOfLibrary
          {
          public void AnotherFunction(MyBase myBase)
          {
          myBase.DoSomething();
          }
          }
          public class UserImplementation : MyBase
          {
          public override void DoSomething()
          {
          throw new NotImplementedException();
          }
          }

          or a field of MyBase or inheriting class that you can use in AnotherPartOfLibrary.

          Dave

          If this helped, please vote & accept answer!

          Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          1 Reply Last reply
          0
          • T T M Gray

            Does AnotherPartOfLibrary inherit from MyBase? If not, what is the relationship between the two classes? You can't call MyBase.DoSomething() because it is abstract. If everything in the base is abstract are you sure you want a base class and not an Interface?

            P Offline
            P Offline
            popchecker
            wrote on last edited by
            #5

            No AnotherPartOfLibrary is not inherited from MyBase. They don't have any relationship. This a library which can be extent. Its a component for the programmers. My problem is related to design of the component. Ok I will tell you the scenario. In this library, I am not aware of the user implemented classes. The users will implement the classes by extending the classes in my library. So those classes are defined only at the time of user implementation, not at the time of designing the library. Here comes my problem. I want to call the user extended functionalities in my library. Hope I am clear now.

            1 Reply Last reply
            0
            • W William Winner

              I agree with the other answer...are you sure you don't want an interface and not an abstract class? Also, how are you not getting errors with what's written anyway? First, you need to specify that abstract void DoSomething() is public. Secondly, in order to implement it, you have to override it in the class that inherits from it. In other words, it should be

              public override void DoSomething()

              And why would you ever want to call a method within an abstract class? You want to call a method within a class that inherits that abstract class. Unless, you just want an interface. If you just want an interface, you would want something like this:

              P Offline
              P Offline
              popchecker
              wrote on last edited by
              #6

              William Winner wrote:

              how are you not getting errors with what's written anyway?

              yes I know I can't call a method in an abstract class, it will make a runtime error. I just demonstrated how I want to implement it. It was to make clear my idea.

              William Winner wrote:

              Secondly, in order to implement it, you have to override it in the class that inherits from it. In other words, it should be public override void DoSomething()

              Sorry that was a typo.

              William Winner wrote:

              And why would you ever want to call a method within an abstract class? You want to call a method within a class that inherits that abstract class. Unless, you just want an interface.

              May be I couldn't explained my problem properly. This is a design issue. myBase and AnotherPartOfLibrary are in my library. The

              UserImplementation

              is not part of my library. It declared only at the time of user implementation. That is my problem. This a set of components for the use of programmers. They can use the classes in library. Also they can inherit some class for extending its functionality. So my problem is, while designing this set of classes I am not aware of the classes the users going to implement. How can I initialize those classes in my classes?

              D 1 Reply Last reply
              0
              • P popchecker

                William Winner wrote:

                how are you not getting errors with what's written anyway?

                yes I know I can't call a method in an abstract class, it will make a runtime error. I just demonstrated how I want to implement it. It was to make clear my idea.

                William Winner wrote:

                Secondly, in order to implement it, you have to override it in the class that inherits from it. In other words, it should be public override void DoSomething()

                Sorry that was a typo.

                William Winner wrote:

                And why would you ever want to call a method within an abstract class? You want to call a method within a class that inherits that abstract class. Unless, you just want an interface.

                May be I couldn't explained my problem properly. This is a design issue. myBase and AnotherPartOfLibrary are in my library. The

                UserImplementation

                is not part of my library. It declared only at the time of user implementation. That is my problem. This a set of components for the use of programmers. They can use the classes in library. Also they can inherit some class for extending its functionality. So my problem is, while designing this set of classes I am not aware of the classes the users going to implement. How can I initialize those classes in my classes?

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                Your users can pass any instance of thier class that derives from your abstract base class to any method that has a parameter of your base class's type as per my earlier example. You will only have access to properties/methods declared (not necessarily implemented) in the base class of course - but that's all you need.

                Dave

                If this helped, please vote & accept answer!

                Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                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