Reputation: 546
C# VS2010
I have 2 methods in my base class. One is virtual with an empty parameter list and the other is an overload which isn't virtual but allows for several parameters to be passed in.
The empty virtual method is overridden in some derived classes to call the overloaded base function instead.
The overloaded method foobars the values passed then needs to call the base version of the virtual method.
How do I do this please? (To get around it I moved the code out of the virtual method into a separate private method, which is called by both methods, but I wondered if I needed to do this)
using KVP = KeyValuePair<string, object>;
abstract class BaseRecordClass
{
private IEnumerable<KVP> BasePrep()
{
// Serialization can't handle DbNull.Value so change it to null
var result = Data.Select(kvp => new KVP(kvp.Key, kvp.Value == DBNull.Value ? null : kvp.Value)).ToList();
// Add the table name
result.Add(new KVP(TableNameGuid, TableName));
return result;
}
/// <summary>
/// Prepares class for sending/serializing over the web.
/// </summary>
/// <returns></returns>
public virtual IEnumerable<KVP> PrepareForWebInterface()
{
return BasePrep();
}
/// <summary>
/// Override to the above that adds extra items to
/// result eg lists of subrecords
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
protected IEnumerable<KVP> PrepareForWebInterface(params KVP[] items)
{
var result = BasePrep().ToList();
result.AddRange(items);
return result;
}
}// class
class SubRecordClass
{
public override IEnumerable<KVP> PrepareForWebInterface()
{
var parms = new List<KVP>
{
new KVP(CustomGroupsFieldListStr, _customGroupsFieldList.Select(item => item.PrepareForWebInterface()).ToList()),
new KVP(UserGroupsListStr, _userGroupsList.Select(item => item.PrepareForWebInterface()).ToList()),
new KVP(StaffPermissionsStr, _staffPermissions.Select(item => item.PrepareForWebInterface()).ToList())
};
return PrepareForWebInterface(parms.ToArray());
}
}
Upvotes: 0
Views: 245
Reputation: 19151
It's not quite clear from your question what it is you want.
Sounds like you want to call a base method that is overridden in a subclass, from an inherited method in that same class, which is not overridden - does that make sense?
If so, I believe you just need to call the method in your base class, using base.YourMethod()
.
For the sake of simplicity and clarity though, you may well be better off just keeping the relevant logic in a separate method, as you are currently doing. I really don`t see anything wrong with that, based on you sparse description..
Upvotes: 1