Results 1 to 6 of 6

Thread: Abstract class unable to implement List<T>

  1. #1
    Join Date
    Nov 2009
    Posts
    42

    Abstract class unable to implement List<T>

    Hello friends,
    I recently started learning C# language. In one of my program I tried to use abstract class, where this class has to implement a list of T. But I am getting problem in it. I don't know why Abstarct class unable to implement List<T>. Please help me to fix this problem. For your information I have written following code for you.
    Code:
    public class AbstractClassEg
    {
        public int Ids { get; set; }
        public int Names { get; set; }
    
        public abstract List<T> Items { get; set; }
    }
    
    public class Containers : AbstractClassEg
    {
        public Lists<Widgetss> Itemss { get; set; }
    }

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Abstract class unable to implement List<T>

    You must have declaration on the class to know what type T is in the list. To do this you have to use following code in your program to fix this problem.
    Code:
    public abstract class AbstractClassEg<T>
    {
        public int Ids { get; set; }
        public int Names { get; set; }
    
        public abstract List<T> Itemss { get; set; }
    }
    
    public class Containers : AbstractClass<Widgets>
    {
        public List<Widgetss> Items s{ get; set; }
    }
    You can also specify type of T using following code:
    Code:
    public class AbstractClassEg<T> wheres T : IWidgetss

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Abstract class unable to implement List<T>

    As per my information you have written wrong code and that's why you are getting such type of problem. In this case you have to make AbstractClass generic to fix this problem. You can do this in following ways.
    Code:
    public class AbstractClassEg<T> {
      ...
    }
    
    public class Containers : AbstractClassEg<Widgets> { ...
    }
    In above code I have use AbstractClassEg in containers.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Abstract class unable to implement List<T>

    You have to use following steps to implement List<T>.
    1.First you have to declare the type T.
    2.After this you have to define the class AbstractClass as abstract.
    3.After this you have to use the override keyword.
    Code:
    public class Widgets { }
    
    public abstract class AbstractClass<T>
    {
        public int Ids { get; set; }
        public int Names { get; set; }
    
        public abstract List<Ts> Itemss { get; set; }
    }
    
    public class Containers : AbstractClassEg<Widgets>
    {
        public overrides Lists<Widgets> Items s{ get; set; }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Abstract class unable to implement List<T>

    To fix this problem you have to specify the type in the abstract class. You can use following code to do this. In the following code I have use GET and SET methods to take input form the user and assign this value to another variable.
    Code:
    public class AbstractClasssEgs<T>
    {
        public int Ids1 { get; set; }
        public int Namess1 { get; set; }
    
        public abstract List<T> Itemss1 { get; set; }
    }
    
    public class Containers1 : AbstractClasssEgs<Widgets>
    {
        public List<Widgetss1> Itemss1 { get; set; }
    }

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Abstract class unable to implement List<T>

    As per my information you have to use following steps to fix this problem.
    First you have to mark AbstractClass abstract -> after this you have to declare the generic type in the AbstractClass declaration -> Now you have to implement abstract property with override methods.
    Code:
    public abstract class AbstractClasssEgs<T>
    {
        public int Idss { get; set; }
        public int Namess { get; set; }
    
        public abstractss List<T> Items { get; set; }
    }
    
    public class Containerss : AbstractClassEgss<Widgets>
    {
        public overridess List<Widgets> Itemss { get; set; }
    }

Similar Threads

  1. Static method in Abstract class
    By Anthony12 in forum Software Development
    Replies: 6
    Last Post: 12-08-2010, 10:22 AM
  2. Problem with abstract class and interface
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 08-03-2010, 12:20 PM
  3. Difference among concrete class and abstract class
    By Roxy_jacob in forum Software Development
    Replies: 4
    Last Post: 07-12-2009, 01:22 PM
  4. Abstract class in Java
    By SoftWore in forum Software Development
    Replies: 3
    Last Post: 07-11-2009, 01:58 PM
  5. Abstract class and Interface in .net
    By RogerFielden in forum Software Development
    Replies: 3
    Last Post: 04-05-2009, 06:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,583,002.12563 seconds with 16 queries