How to determine which inheriting class is using an abstract class's methods?

If you know each of the types you want to compare against, then use the is operator: if (this is WorldListener) { // ... } else if (this is MasterListener) { // ... } Alternatively, you could use GetType if you want a little more flexibility: var type = GetType(); // Do some logic on the type to determine what to do next You should be careful with this approach, however; it's generally indicative of bad design that you need to explicitly check for types (as these lovely people insist). Instead, it's almost always more appropriate to use polymorphism to delegate the desired behaviour to the base class (using a virtual or abstract method in the base class) – this is, after all, what it's designed for! You might apply polymorphism something like this: static class Program { static void Main(string args) { Listener listener = new WorldListener(); listener.DoSomething(); } } abstract class Listener { public void DoSomething() { var output = Decorate("Did something!"); ConsoleWrapper.

WriteLine(output); } protected abstract string Decorate(string input); } class WorldListener : Listener { protected override string Decorate(string input) { return string. Format("World {0}", input); } } class MasterListener : Listener { protected override string Decorate(string input) { return string. Format("Master {0}", input); } } This will produce the output World Did something!

The advantage of this approach is that if you ever want to add another type of listener, it's simply a matter of defining a new class for it with the appropriate Decorate method; there's no need to modify Listener itself.

If you know each of the types you want to compare against, then use the is operator: if (this is WorldListener) { // ... } else if (this is MasterListener) { // ... } Alternatively, you could use GetType if you want a little more flexibility: var type = GetType(); // Do some logic on the type to determine what to do next. You should be careful with this approach, however; it's generally indicative of bad design that you need to explicitly check for types (as these lovely people insist). Instead, it's almost always more appropriate to use polymorphism to delegate the desired behaviour to the base class (using a virtual or abstract method in the base class) – this is, after all, what it's designed for!

You might apply polymorphism something like this: static class Program { static void Main(string args) { Listener listener = new WorldListener(); listener.DoSomething(); } } abstract class Listener { public void DoSomething() { var output = Decorate("Did something! "); ConsoleWrapper. WriteLine(output); } protected abstract string Decorate(string input); } class WorldListener : Listener { protected override string Decorate(string input) { return string.

Format("World {0}", input); } } class MasterListener : Listener { protected override string Decorate(string input) { return string. Format("Master {0}", input); } } This will produce the output World Did something!. The advantage of this approach is that if you ever want to add another type of listener, it's simply a matter of defining a new class for it with the appropriate Decorate method; there's no need to modify Listener itself.

Great answer, wish I could choose more then one! – Kin Apr 18 '10 at 9:23 Wouldn't this cause me to have to delegate more to the inheriting classes? Almost all of the functions are re-used.

– Kin Apr 18 '10 at 9:28 It's up to you how much you delegate; you could retain the bulk of the logic in Listener and just delegate the code for decorating the input (i.e. Appending the World and Master tags). – Will Vousden Apr 18 '10 at 9:34 It sounds like I must be flawed in my understanding of abstract methods, thanks for your help.

– Kin Apr 18 '10 at 9:38 @Kin: I've amended the example I gave to demonstrate. – Will Vousden Apr 18 '10 at 9:38.

You can use if (this is WorldListener) instead of your pseudocode if (inheriting class == WorldListener) However, doing this is a bad design smell. You should strongly consider an alternative solution, e.g. Performing the write to the console wrapper in a virtual method instead of adding this strong coupling between the base class and its subclasses.

I will look into that, thanks! – Kin Apr 18 '10 at 9:23.

Hmm.. Well, in your simplified example you don't call DoSomething() and DoSomethingSpecific(), and there's no implementation for MasterListener.. Also, if I understand it right, in your expected output your MasterListener. DoSomethingSpecific() runs a ConsoleWrapper.WorldWrite.. You probably meanr MasterWrite? In any case.. Here's a working example that does what you want (at least in the way I understood your request :P ) The printed result is: World Did something World I did sth specific!

Master Did something Master I did sth specific! The code: void Main() { var wl = new WorldListener(); wl.DoSomething(); wl.DoSpecific(); var ml = new MasterListener(); ml.DoSomething(); ml.DoSpecific(); } public abstract class Listener { public abstract string Category { get; } public void DoSomething() { ConsoleWrapper. Write(Category, "Did something"); } } public static class ConsoleWrapper { public static void Write(string category, string input) { Console.

WriteLine("{0} {1}", category, input); } } public class WorldListener : Listener { public override string Category { get { return "World"; } } public void DoSpecific() { ConsoleWrapper. Write(Category, "I did sth specific! "); } } public class MasterListener : Listener { public override string Category { get { return "Master"; } } public void DoSpecific() { ConsoleWrapper.

Write(Category, "I did sth specific! "); } }.

Great answer thanks! – Kin Apr 18 '10 at 9:23 I've updated this example with what you might need - the Listener class doesn't depend on child inherited classes. Instead the child classes have to implement a Category property that will define which class has sent a string to the ConsoleWrapper.

– Artiom Chilaru Apr 18 '10 at 9:46.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions