I have a base class that has a property and a method that uses that property. I have a class that inherits that base class and has its own implementation of the base class's property that is explicitly hidden using the New modifier. In the base class' method, is there a good way to use the inherited class' property instead of the base's implementation?
class Program
{
public class MyBase
{
public string MyProperty { get { return "Base"; } }
public string MyBaseMethod()
{
return MyProperty;
}
}
public class MyInherited : MyBase
{
public new string MyProperty { get { return "Inherited"; } }
}
static void Main(string[] args)
{
List<MyBase> test = new List<MyBase>();
test.Add(new MyBase());
test.Add(new MyInherited());
foreach (MyBase item in test)
{
Console.WriteLine(item.MyBaseMethod());
}
}
}
In the example, the output is:
Base
Base
Current workaround:
...
public class MyBase
{
public string MyProperty { get { return "Base"; } }
public string MyBaseMethod()
{
if (this is MyInherited)
{
return baseMethod(((MyInherited)this).MyProperty);
}
else
{
return baseMethod(MyProperty);
}
}
private string baseMethod(string input)
{
return input;
}
}
...
Is there a better way to do this? I'd rather not have to do explicit type casts.
Hiding a member with the new keyword should generally be avoided. Instead make the base class' property virtual and override it in the descending class. The MyBaseMethod will automatically use this overridden property in inheriting classes.
public class MyBase
{
public virtual string MyProperty { get { return "Base"; } }
public string MyBaseMethod()
{
return MyProperty;
}
}
public class MyInherited : MyBase
{
public override string MyProperty { get { return "Inherited"; } }
}
var inherited = new MyInherited();
Console.WriteLine(inherited.MyBaseMethod()); // ==> "Inherited"
See this interesting post related to the new keyword: Why do we need the new keyword and why is the default behavior to hide and not override?
Make the property virtual, not sealed, and override it, rather than shadowing it. Then all uses of the property will use the most derived implementation of it.
There is no such way. If you do new (which is early binding), you have to do explicit casts. The only solution is to make the property virtual. Then you can override it (using the override modifier). This is late binding.
Related
Code tells more than words, so look at this:
public abstract class ViewObject: INotifyPropertyChanged {
public virtual string Id {
get {
return this.GetType().Name;
}
}
}
public class Object : ViewObject {
private string id = string.Empty;
public override string Id {
get {
return this.id;
}
set {
this.id = value;
}
}
}
What is the correct way to implement the desired behaviour of a base implementation in the abstract class (yes, it should have a base implementation for this, but not for other things)?
I can only think of using the new keywork instead of override to simply hide the base implementation, but is this right?
you are already using inheritance. Override method is useful when method name and parameter is same.
here you can use method overloading.
for method overload name is same but parameter is different. you can use in inheritance also.
i hope this is useful
If you use the new keyword and someone casts your derived object to the base class, the base implementation will be called and not the derived one. To avoid this, the override is needed.
But that is currently not possible, cause your base class doesn't support a setter. So stick to the override and implement a set method in the base class that simply throws a NotSupportedExecption.
public abstract class ViewObject
{
public virtual string Id
{
get { return this.GetType().Name; }
set { throw new NotSupportedException(); }
}
}
public class Object : ViewObject
{
private string id = string.Empty;
public override string Id
{
get { return this.id; }
set { this.id = value; }
}
}
I currently have something like this:
abstract class BaseClass {
public abstract string title();
}
class Derived1 : BaseClass {
public override string title() { return "D1"; }
}
class Derived2 : BaseClass {
public override string title() { return "D2"; }
}
class Receiver<T> where T : BaseClass {
private T obj;
public string objTitle() { return obj.title(); }
}
The problem I am running into is that, if obj is null, objTitle throws a null reference exception. I can guarentee in this case that title will always return the same string for a given derived type; is there any way to make Receiver able to access it on the generic parameter T? My instinct is to use a static, but I don't know of any way to make a static visible to the Reciever; there's no way to make a base class or constraint specifying it.
You could use reflection to call a static method on the type, or if the value is actually a constant, you could also instantiate a new instance if there isn't an instance yet.
class Receiver<T> where T : BaseClass, new() {
private T obj;
public string objTitle() { return (obj ?? new T()).title(); }
}
What I would do is construct the T immediately and drop the if
class Receiver<T> where T : BaseClass, new() {
private T obj = new T();
public string objTitle() { return obj.title(); }
}
In C# you can't override static methods. From your design I can see that "title" is independent with an instance of BaseClass/Derived1/Derived2. Adding an instance method title() doesn't make sense here. I recommend you design these classes like this: (I renamed the classes to make it easy to understand)
abstract class MessageBase { }
class TextMessage : MessageBase { }
class ImageMessage : MessageBase { }
class Receiver<T> where T : MessageBase
{
public string GetMessageTitle()
{
if (typeof(T) == typeof(TextMessage)) return "Text";
else if (typeof(T) == typeof(ImageMessage)) return "Image";
return "Default";
}
}
I have the following:
class Info
{
public string str;
};
class CarInfo : Info {}
class InfoContainer
{
public virtual List<Info> info_list {get; set;}
public bool is_known(Info inf)
{
if (-1 == info_list.FindIndex( i => i.str == inf.str) return false;
return true;
}
}
class CarFleetInfo : InfoContainer
{
new public List<CarInfo> info_list;
CarFleetInfo()
{
info_list = new List<CarInfo>();
}
}
Main()
{
CarInfo c = new CarInfo();
Info i = new Info();
c.is_known(i);
}
I have few other "specific info" class that inherited from Info (like CarInfo ), and few classes that inherited from InfoContainer, which are each overrides info_list with other a list of object of some "specific info".
now, the call to c.is_known(i) raise an exception saying info_list is null.
Your problem is the new keyword on new public List<CarInfo> info_list;. In this context, new means "Ignore the definition provided by my base class (if any), and use my own definition instead". So when you set a CarInfo's info_list, you're not setting the base Info's info_list. Then when you try to access the info_list that's part of an Info, it's null.
The best way to fix it is simply to take out the CarInfo's info_list and simply use the base class's version. This is the correct way to do what you're trying to do here, although there are others that could be used.
You aren't overriding the info_list property, you are shadowing it.
To override the property you should use the override keyword instead of the new keyword, and then the code will work as intended.
When you shadow the property the subclass gets it's own property with the same name, that the base class doesn't know anything about.
You're using new on a property which explicitly hides the member from its base class. It would be invalid to use override since a List<Info> is not a List<CarInfo>.
Instead consider using generics:
class Info
{
public string Foo { get; set; }
}
class CarInfo : Info {}
class InfoContainer<T>
where T: Info
{
public List<T> info_list { get; set; }
public bool is_known(T inf)
{
if (-1 == info_list.FindIndex(i => i.Foo == inf.Foo)) return false;
return true;
}
}
class CarFleetInfo : InfoContainer<CarInfo>
{
}
There are two problems:
You should be overriding instead of shadowing
You cannot override a method with a different signature
class Info
{
public string str;
};
class CarInfo : Info {}
class InfoContainer
{
public virtual List<Info> info_list {get; set;}
public bool is_known(Info inf)
{
if (!info_list.Exists(p => p.str == inf.str)) return false;
return true;
}
}
class CarFleetInfo : InfoContainer
{
public override List<Info> info_list { get; set; }
CarFleetInfo()
{
info_list = new List<Info>();
}
}
You cannot have public List<CarInfo> override public List<Info>
I think you can't assign a List<CarInfo> to a List<Info>, so you can't override the info_list that way. To work around, you have to use generic class instead defined as follow:
class InfoContainer<T> where T : Info
{
public virtual List<T> info_list {get; set;}
public bool is_known(T inf)
{
if (-1 == info_list.FindIndex( i => i.str == inf.str) return false;
return true;
}
}
class CarFleetInfo<T> : InfoContainer<T> where T : CarInfo
{
public override List<T> info_list {get;set;}
public CarFleetInfo(){
info_list = new List<T>();
}
}
The purpose of a base class is to provide functions and definitions that will be overridden. Overriding a base class is meaningless. May be a good exercise on breaking a language, but in general you're creating a paradox and an infinite loop within your compiler. The override attribute means to the preprocessor that your code should run priority rather than it's base class operation.
However, you should still call the base class operation in most cases to ensure all fields and objects of the inherited object are instantiated and correctly initialized.
I often use the class-factory pattern whereby a class has a private constructor and a static method to create the class. This allows for the situation where the class cannot be constructed for some reason, and a null is returned - very handy.
I would like to be able to extend this to a factory method which creates a particular class from a hierarchy of derived classes depending on conditions. However I can't see a way of then hiding the constructors of the derived classes to force the use of the factory method. If the factory method is in the base class it no longer has access to the private constructors of derived classes. Putting a factory method in every derived class doesn't work as the required type must then be known beforehand. Nested classes might be a way if a class had access to the private members of a nested class, but sadly it seems that the nested classes have access to the private members of the enclosing class, but not the other way round.
Does anyone know of a way of doing this?
There are several possibilities, two of which are:
Put all those classes in one project and make the constructors internal. Other projects won't be able to call those constructors but the code inside that project can.
Make the constructors of those classes protected (instead of private) and create a private derived class in the class containing the factory method. Create an instance of that private class and return it.
Example for the second option:
public static class AnimalFactory
{
public static Animal Create(int parameter)
{
switch(parameter)
{
case 0:
return new DogProxy();
case 1:
return new CatProxy();
default:
throw new ArgumentOutOfRangeException("parameter");
}
}
private class DogProxy : Dog { }
private class CatProxy : Cat { }
}
public abstract class Animal { }
public class Dog : Animal
{
protected Dog() { }
}
public class Cat : Animal
{
protected Cat() { }
}
Here's the sample code I was working on when Daniel posted his answer. It looks like it's doing what he suggested:
public static class BaseFactory
{
public static Base Create(bool condition)
{
if (condition)
{
return Derived1.Create(1, "TEST");
}
else
{
return Derived2.Create(1, DateTime.Now);
}
}
}
public class Base
{
protected Base(int value)
{
}
protected static Base Create(int value)
{
return new Base(value);
}
}
public sealed class Derived1: Base
{
private Derived1(int value, string text): base(value)
{
}
internal static Derived1 Create(int value, string text)
{
return new Derived1(value, text);
}
}
public sealed class Derived2: Base
{
private Derived2(int value, DateTime time): base(value)
{
}
internal static Derived2 Create(int value, DateTime time)
{
return new Derived2(value, time);
}
}
[EDIT] And for Daniel's second suggestion:
public static class BaseFactory
{
public static Base Create(bool condition)
{
if (condition)
{
return new Derived1Creator(1, "TEST");
}
else
{
return new Derived2Creator(1, DateTime.Now);
}
}
private sealed class Derived1Creator: Derived1
{
public Derived1Creator(int value, string text): base(value, text)
{
}
}
private sealed class Derived2Creator: Derived2
{
public Derived2Creator(int value, DateTime time): base(value, time)
{
}
}
}
public class Base
{
protected Base(int value)
{
}
protected static Base Create(int value)
{
return new Base(value);
}
}
public class Derived1: Base
{
protected Derived1(int value, string text): base(value)
{
}
protected static Derived1 Create(int value, string text)
{
return new Derived1(value, text);
}
}
public class Derived2: Base
{
protected Derived2(int value, DateTime time): base(value)
{
}
protected static Derived2 Create(int value, DateTime time)
{
return new Derived2(value, time);
}
}
Note that this second approach means that the classes can't be sealed, unfortunately.
Rather than using methods inside the class itself as a factory implement the Factory pattern by means of a static class ("the factory") that returns the correct instance based on the logic you write.
You can intercept the derived type creation in the base class contructor and check that the caller is your factory using StackFrames:
protected Class1() //base class ctor
{
StackFrame[] stackFrames = new StackTrace().GetFrames();
foreach (var frame in stackFrames)
{
//check caller and throw an exception if not satisfied
}
}
I have a nice interface, and I want to implement one member of it in a base class so the clients can derive from the base class and have less boiler-plate to write. However, even though declared abstract, the compiler complains that the class does not implement the interface?!? How is this supposed to be done?
Code in question:
public interface ITaskDefinition
{
ITask CreateTask(TaskId id);
string Name { get; }
bool HasName { get; }
}
public abstract class TaskDefinitionBase : ITaskDefinition
{
private string name_ = null;
public void SetName(string name)
{
name_ = name;
}
public string Name
{
get
{
return name_;
}
}
public bool HasName
{
get
{
return name_ != null;
}
}
}
and the error I get is:
ITaskDefinition.cs(15,27): error CS0535: 'NetWork.Task.TaskDefinitionBase' does not implement interface member 'NetWork.Task.ITaskDefinition.CreateTask(NetWork.Task.TaskId)'
You must add an abstract stub of the method:
public abstract ITask CreateTask(TaskId id);
Inheritors can then override it to implement the interface.
When an object implements an interface it must obey all the constraints applied by the interface. This means that you must implement all properties and methods declared in the interface.
In your case you're missing the implementation of CreateTask. Also, you should note that C# use properties as opposed to Java for example that uses getters/setters. For this reason you don't need a SetName method.
You code should look something like this:
public abstract class TaskDefinitionBase : ITaskDefinition
{
private string name_ = null;
public abstract ITask CreateTask(TaskId id);
public string Name
{
get
{
return name_;
}
set
{
name_ = value
}
}
public bool HasName
{
get
{
return name_ != null;
}
}
}
TaskDefinitionBase needs to include CreateTask - if you don't want to implement it, just mark the method as abstract to force any implementing classes to implement it.