• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Another C# Generic List Question

Bohica69

Gawd
Joined
Jul 12, 2005
Messages
676
I've got a class that I inherited from the Generic List class -
public class InfoList:List<Info>

It all works the way I expect it to except when I try and do this - I delcare a InfoList, and do a findAll from it :

Code:
InfoList tempList = new InfoList()

Predicate<Info> tempFilter;
HistoryFilter filter = new HistoryFilter(HistoryFilterDescription);
tempFilter = new Predicate<Info>(filter.FilterByHistory);

InfoList returnList = (InfoList)tempList.FindAll(tempFilter);

The FindAll method returns a List<Info> object which makes sense, but since InfoList inherits from List<Info> I thought I could just do the cast - But when I do that I get an Unable to cast object of type generic list to type of InfoList. If I remove the cast then it won't compile - So, do I have to iterate thru the List<Info> and store those in the InfoList, or is there a way to make this work in one shot? I've been searching google and haven't found any relevant links (yet).

Thanks in advance.
 
Can you show your class definition? It's kind of hard to answer a question about a class if we don't know what it looks like. My guess, without seeing the class, is that you have additional structure in the more derived class making a direct conversion impossible. Does your derived class have a constructor which takes an List<Info> as a parameter?
 
No, I did't have a constructor that takes List<Info> as a parameter. However, your asking the question lead me to do this:

Code:
public InfoList(List<Info> incomingList)
 {
   this.AddRange(incomingList);
 }

That solved my problem. Now I just pass the results of the find all to the constructor. Would there be any other solutions? Here's an outline of the InfoList Class
Code:
public InfoList():this((true))
{
}
public InfoList(bool LoadDefaultData)
{

    if (LoadDefaultData)
    {
	// Do some SQL stuff here
	this.Add(LeadInfo);
    }
}
public InfoList(List<Info> incomingList)
{
    this.AddRange(incomingList);
}

There are some other methods that return sorted List<Info> in the class as well, but I don't see that those are important to this discussion.

Thanks.
 
Are there any other data members in your derived class?
 
There are these private variables - used for access to the database

Code:
private IDbConnection _conn = null;
private IDbDataAdapter _adapter = null;
private IDbCommand _command = null;
private IDataParameter _parameter = null;
private DataAccess _dataAccess = new DataAccess();

These private methods - used for sorting the list - there are a bunch of these, but the signitures are all the same...
Code:
private int CompareByCID(Info info1, Info info2)
private int CompareByCreateDate(Info info1, Info info2)

These public methods
Code:
public int GetCount() - reuturns this.Count
public List<Info> SortList()
public List<Info> SortList(int maximumRows, int startRowIndex)
public List<Info> SortList(string SortExpression)
public List<Info> SortList(int maximumRows, int startRowIndex, string SortExpression)

The SortList methods are using the Comparision delgate to return a sorted list. The CompareBy methods are used for the sorting.

I think the constructors are causing the problem:
public LeadInfoList():this((true))
public LeadInfoList(bool LoadDefaultData)
public LeadInfoList(List<LeadInfo> incomingList)

The empty constructor calls the overloaded constructor which then loads the list with the default set of data from the db - could that be the problem?

If you'd like I can email you the class itself - but your looking at pretty much everything (minus the details of the methods)

Thanks for the help!
 
List<string> foo
and
MyList foo
are not the same type even if mylist inherits List<string>

if your return type was InfoList (the child type) and you're returning a List<info> (the base type) you will need to explicitly cast it. if your method return type was a List<Info> (the base type) and you returned an InfoList (the child type) then because the return type is the base class for the object you're returning, then no cast is needed.

List<Info> does not inherit from InfoList, and therefore does not know how to convert itself to ListInfo. Since ListInfo is a List<info> it does know how to convert itself, so therefore the cast is necessary.
 
Back
Top