How can I properly override a class method/property in a derived class?

怎甘沉沦 提交于 2019-12-12 03:38:51

问题


Follow up from This Question -

In the Question class I have the following properties (I'm simplifying here)

public class Question : IDisposable, IEquatable<Question>{
    protected virtual DataRow Row{ get { return DataTable.Rows[0]; } }
    public string Value { get { return this.Row.Field<string>("Value"); } }
}

now, for the SessionQuestion class I want to keep this in play, but I want to read the Value field from a different data table :

public class SessionQuestion : Question, IEquatable<SessionQuestion>{
    protected override DataRow Row { get { return OtherDataTable.Rows[0]; } }
}

Now, if I were to say something like :

new SessionQuestion( ).Value

would that return the value from DataTable.Rows[0], or OtherDataTable.Rows[0]? I want it to return the value from OtherDataTable, so if this doesn't work, what would I need to do so it would work properly?

来源:https://stackoverflow.com/questions/32029791/how-can-i-properly-override-a-class-method-property-in-a-derived-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!