Home All Groups Group Topic Archive Search About

Collection findall error

Author
17 Dec 2008 7:16 PM
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

Author
17 Dec 2008 10:35 PM
Anthony Jones
Show quote Hide quote
"tshad" <t**@dslextreme.com> wrote in message
news:eWFBKwHYJHA.2068@TK2MSFTNGP03.phx.gbl...
>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.


--
Anthony Jones - MVP ASP/ASP.NET



Post Thread options