I have a collection class that inherits List. I would like a method on that class that can iterate over the items in the base class list.
Currently I have this:
public class Invoices : List<Invoice>
{
public string ToString()
{
string cOP = "";
int i;
Invoice oInvoice;
cOP += string.Format("Invoices.Count = {0}\r\n", base.Count);
foreach (Invoice oInvoice in base)
{
cOP += oInvoice.ToString();
}
return cOP;
}
}
But I get the compile time error on base in the foreach statement, "Use of keyword 'base' is not valid in this context".
I've tried replacing base with:
Is there a reason why I need to convert the List to an Array to iterate over it? Surely I should just be able to iterate over the List?
You should use the this
keyword:
public class Invoices : List<Invoice>
{
public string ToString()
{
string cOP = "";
int i;
Invoice oInvoice;
cOP += string.Format("Invoices.Count = {0}\r\n", base.Count);
foreach (Invoice oInvoice in this)
{
cOP += oInvoice.ToString();
}
return cOP;
}
}