Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

issue with collection vs generic list.

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
This is similar to my previous post where I am trying to resolve the issue of setting up a class then a collection of the class. But I am not calling it a List<class> but as a collection that inherits from that class.

It seems to work except when it actually assigns the collection.

I get this error:

{"Unable to cast object of type 'System.Collections.Generic.List`1[BusinessLayer.DotNetType]' to type 'BusinessLayer.DotNetTypeCollection'."}

The classes are:

Code:
    public class DotNetType
    {
        public string DatabaseDataType { get; set; }
        public string DotNetDataType { get; set; }

    }

    public class DotNetTypeCollection : List<DotNetType>
    {
        public DotNetTypeCollection()
        {
        }

        public DotNetTypeCollection GetTypes()
        {
            DbObject dbo = new DbObject();
            DataSet ds = null;
            ds = dbo.GetDataSet("dbGen.GetDotNetDataTypes");

            return (DotNetTypeCollection)ds.Tables[0].AsEnumerable().Select(row => new DotNetType
                            {
                                DotNetDataType = row.Field<string>("DotNetDataType"),
                                DatabaseDataType = row.Field<string>("DatabaseDataType")
                            }).ToList();
        }
    }

Then I call it like this from my code:

public DotNetTypeCollection dntcol { get; set; }
public DotNetTypeCollection dntcol2 { get; set; }

...
dntcol = new DotNetTypeCollection();
dntcol2 = dntcol.GetTypes();

the assignment to dntcol2 causes the error.

Thanks,

Tom
 
I also tried this where I assign the results to a variable that inherits from a List<> which I assume would act the same as if the type was either the collection or the list.

Code:
        public DotNetTypeCollection GetTypes()
        {
            DotNetTypeCollection dntc2;

            DbObject dbo = new DbObject();
            DataSet ds = null;
            ds = dbo.GetDataSet("dbGen.GetDotNetDataTypes");

            dntc2 = (DotNetTypeCollection)ds.Tables[0].AsEnumerable().Select(row => new DotNetType
                            {
                                DotNetDataType = row.Field<string>("DotNetDataType"),
                                DatabaseDataType = row.Field<string>("DatabaseDataType")
                            }).ToList();
            return dntc2;
        }

Thanks,

Tom
 
If you'd like to see it in action, compile and run this. You'll see that going from mammal to cat fails, but going from cat to mammal works just fine.

Code:
[COLOR=#0000FF]using[/color] System;
[COLOR=#0000FF]using[/color] System.Collections.Generic;
[COLOR=#0000FF]using[/color] System.Data;
[COLOR=#0000FF]using[/color] System.Linq;
[COLOR=#0000FF]using[/color] System.Text;
[COLOR=#0000FF]using[/color] System.Threading.Tasks;
 
[COLOR=#0000FF]namespace[/color] ConsoleApplication1
{
    [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Program[/color]
    {
        [COLOR=#0000FF]static[/color] [COLOR=#0000FF]void[/color] Main([COLOR=#0000FF]string[/color]&#91;&#93; args)
        {
 
            [COLOR=#0000FF]try[/color]
            {
                [COLOR=#2B91AF]Mammal[/color] m = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Mammal[/color]();
                [COLOR=#2B91AF]Cat[/color] c = ([COLOR=#2B91AF]Cat[/color])m;
                [COLOR=#2B91AF]Console[/color].WriteLine([COLOR=#A31515]&quot;Cast from mammal to cat good&quot;[/color]);
            }
            [COLOR=#0000FF]catch[/color] {
                [COLOR=#2B91AF]Console[/color].WriteLine([COLOR=#A31515]&quot;Cast from mammal to cat failed&quot;[/color]);
            }
 
            [COLOR=#0000FF]try[/color]
            {
                [COLOR=#2B91AF]Cat[/color] c = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Cat[/color]();
                [COLOR=#2B91AF]Mammal[/color] m = ([COLOR=#2B91AF]Mammal[/color])c; [COLOR=#008000]//cast is actually unnecessary here because it is already a mammal object[/color]
                [COLOR=#2B91AF]Console[/color].WriteLine([COLOR=#A31515]&quot;You can go from a sub-class to the base-class&quot;[/color]);
            }
            [COLOR=#0000FF]catch[/color] {
                [COLOR=#2B91AF]Console[/color].WriteLine([COLOR=#A31515]&quot;You can&#39;t go from a sub-class to the base-class&quot;[/color]);
            }
 
            [COLOR=#2B91AF]Console[/color].ReadKey();
        }
    }
 
 
    [COLOR=#0000FF]public[/color] [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Mammal[/color]
    {
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]string[/color] name { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
    }
    [COLOR=#0000FF]public[/color] [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Cat[/color] : [COLOR=#2B91AF]Mammal[/color]
    {
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]int[/color] age { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
    }
    [COLOR=#0000FF]public[/color] [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Dog[/color] : [COLOR=#2B91AF]Mammal[/color]
    {
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]int[/color] dogyears { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top