Collection findall error

T

tshad

I am using FindAll to get a set of my TableColumnNames and get an error if I
do it this way:

TableColumnNameCollection tcn =
AppSettings.tableColumnNames.FindAll(delegate(TableColumnName
tcn1)
{ return tcn1.TableName == strTemp; });

But I can do it this way:

List<TableColumnName> tcn =
AppSettings.tableColumnNames.FindAll(delegate(TableColumnName
tcn1)
{
return tcn1.TableName == strTemp; });

My class is:
**********************************************
using System;
using System.Collections.Generic;
using System.Text;

namespace RepositoryServiceApp.BusinessLayer
{
public class TableColumnName
{
private string mstrTableName;
private string mstrColumnName;
private string mstrColumnType;

public string ColumnType
{
get { return mstrColumnType; }
set { mstrColumnType = value; }
}

public string ColumnName
{
get { return mstrColumnName; }
set { mstrColumnName = value; }
}

public string TableName
{
get { return mstrTableName; }
set { mstrTableName = value; }
}

public new string ToString()
{
return TableName + " / " + ColumnName + " / " + ColumnType;
}
}
public class TableColumnNameCollection : List<TableColumnName> { }
}
*******************************************************

Why can't I use TableColumnNameCollection in my Findall statement?

Isn't it the same as List<TableColumnName>?

Thanks,

Tom
 
A

Anthony Jones

tshad said:
I am using FindAll to get a set of my TableColumnNames and get an error if
I do it this way:

TableColumnNameCollection tcn =

AppSettings.tableColumnNames.FindAll(delegate(TableColumnName tcn1)
{ return tcn1.TableName == strTemp; });

But I can do it this way:

List<TableColumnName> tcn =

AppSettings.tableColumnNames.FindAll(delegate(TableColumnName tcn1)
{
return tcn1.TableName == strTemp; });

My class is:
**********************************************
using System;
using System.Collections.Generic;
using System.Text;

namespace RepositoryServiceApp.BusinessLayer
{
public class TableColumnName
{
private string mstrTableName;
private string mstrColumnName;
private string mstrColumnType;

public string ColumnType
{
get { return mstrColumnType; }
set { mstrColumnType = value; }
}

public string ColumnName
{
get { return mstrColumnName; }
set { mstrColumnName = value; }
}

public string TableName
{
get { return mstrTableName; }
set { mstrTableName = value; }
}

public new string ToString()
{
return TableName + " / " + ColumnName + " / " + ColumnType;
}
}
public class TableColumnNameCollection : List<TableColumnName> { }
}
*******************************************************

Why can't I use TableColumnNameCollection in my Findall statement?

Isn't it the same as List<TableColumnName>?

TableColumnNameCollection is not "the same as" a List<TableColumnName>.

TableColumnNameCollection is a List<TableColumnName> in the sense that its a
subclass of List<TableColumnName>.

Hence its possible to assign a TableColumnNameCollection to a
List<TableColumnName> since we know all TableColumnNameCollection are
List<TableColumnName>.

OTH its not possible to assign List<TableColumnName> to a
TableColumnNameCollection because not all List<TableColumnName> are
TableColumnNameCollection.

The FindAll in this case is creating an instance of List<TableColumnName>
not an instance of TableColumnNameCollection hence you will not be able to
assign it such, even with an explicit cast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top