I have an abstract class which defines a number of classes which can be voted on and then sorted. Since these classes all share the properties by which they are sorted, I would like to include a method at the abstract level which will let me sort them by these properties, but I’m running in to trouble with ‘not assignable to parameter’ errors.
How should I handle the following:
internal abstract class ESCO
{
public double HotScore { get; set; }
public double VoteTotal { get; set; }
public DateTime Created { get; set; }
protected static List<ESCO> SortedItems(List<ESCO> escoList, ListSortType sortType)
{
switch (sortType)
{
case ListSortType.Hot:
escoList.Sort(delegate(ESCO p1, ESCO p2) { return p2.HotScore.CompareTo(p1.HotScore); });
return escoList;
case ListSortType.Top:
escoList.Sort(delegate(ESCO p1, ESCO p2) { return p2.VoteTotal.CompareTo(p1.VoteTotal); });
return escoList;
case ListSortType.Recent:
escoList.Sort(delegate(ESCO p1, ESCO p2) { return p2.Created.CompareTo(p1.Created); });
return escoList;
default:
throw new ArgumentOutOfRangeException("sortType");
}
}
private SPUser GetCreatorFromListValue(SPListItem item)
{
var user = new SPFieldUserValue(SPContext.Current.Web, (string)item["Author"]);
return user.User;
}
private static VoteMeta InformationForThisVote(List<Vote> votes, int itemId)
{} // There are more methods not being shown with code to show why I used
// abstract instead of something else
}
Trying to implement as such:
class Post : ESCO
{
public string Summary { get; set; } // Properties in addition to abstract
public Uri Link { get; set; } // Properties in addition to abstract
public static List<Post> Posts(SPListItemCollection items, ListSortType sortType, List<Vote> votes)
{
var returnlist = new List<Post>();
for (int i = 0; i < items.Count; i++) { returnlist.Add(new Post(items[i], votes)); }
return SortedItems(returnlist, sortType);
}
I am totally open to “You’re doing it all wrong”.
I can’t reproduce the same error message, but I think the error you are getting is because
is attempting to return a list of the abstract base class. Try changing that to
You’ll need to include the System.Linq namespace if you did not already.
FYI, the error I get (in a simplified test case) is
Cannot implicitly convert type System.Collections.Generic.List<MyNamespace.ESCO>' to 'System.Collections.Generic.List<MyNamespace.Post>'