INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

issue with collection vs generic list.

issue with collection vs generic list.

(OP)
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

RE: issue with collection vs generic list.

(OP)
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

RE: issue with collection vs generic list.

You can't cast a base type into a subtype - that doesn't work. To give a good example from stack overflow, you can't cast a mammal (your base type) into a dog, because it might be a cat.

I suggest you read up on inheritance: https://msdn.microsoft.com/en-us/library/ms173149....

RE: issue with collection vs generic list.

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

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
 
            try
            {
                Mammal m = new Mammal();
                Cat c = (Cat)m;
                Console.WriteLine("Cast from mammal to cat good");
            }
            catch {
                Console.WriteLine("Cast from mammal to cat failed");
            }
 
            try
            {
                Cat c = new Cat();
                Mammal m = (Mammal)c; //cast is actually unnecessary here because it is already a mammal object
                Console.WriteLine("You can go from a sub-class to the base-class");
            }
            catch {
                Console.WriteLine("You can't go from a sub-class to the base-class");
            }
 
            Console.ReadKey();
        }
    }
 
 
    public class Mammal
    {
        public string name { get; set; }
    }
    public class Cat : Mammal
    {
        public int age { get; set; }
    }
    public class Dog : Mammal
    {
        public int dogyears { get; set; }
    }
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close