Create an object of an inherited class
-
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
-
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
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?
-
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
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 bepublic 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:
-
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
To do this you need to either have a parameter of
MyBase
or ineriting class in the methodpublic 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 inAnotherPartOfLibrary
.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) -
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?
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.
-
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 bepublic 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:
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
andAnotherPartOfLibrary
are in my library. TheUserImplementation
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?
-
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
andAnotherPartOfLibrary
are in my library. TheUserImplementation
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?
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)