3

I am trying to write a basic abstract class where any class that extends it will have a List of some type.

The context is I call a web service, and I receive "pages" of orders, and each order has "pages" of order lines, etc.

abstract class Pagination
{
    public int _offset { get; set; }
    public int _total { get; set; }
    public string previous { get; set; }
    public string next { get; set; }

    // Can I add something here that represents a list of items
    // that is overridden in child classes somehow?
    // public abstract List<Something?> items { get; set; }
    // The purpose is for this generic "getItemCount" function or something similar
    /*
    public int getItemCount()
    {
        return items != null ? items.Count() : 0;
    }
    */
}

class OrderHeader : Pagination
{
    public int orderId { get; set; }
    public List<OrderLine> items { get; set; }
}

class OrderLine : Pagination
{
    public string sku { get; set; }
    public int qty { get; set; }
    public List<OrderLineDetails> items { get; set; }
}

class OrderLineDetails
{
    public string serialNum { get; set; }
}
Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
William YK
  • 1,025
  • 12
  • 26

4 Answers4

1

You can do that with generics

public abstract class Pagination<T>
{
    public abstract List<T> Items { get; set; }
}

public class OrderHeader : Pagination<OrderLine>
{
    public override List<OrderLine> Items { get; set; }
}

public class OrderLine : Pagination<OrderLineDetails>
{
    public override List<OrderLineDetails> Items { get; set; }
}
juharr
  • 31,741
  • 4
  • 58
  • 93
1

You can use generics, e.g.:

abstract class Pagination<T>
{
    // Other properties
    public List<T> items { get; set; }
}

class OrderHeader : Pagination<OrderLine>
{
    // Other properties
}

class OrderLine : Pagination<OrderLineDetails>
{
    // Other properties
}

class OrderLineDetails
{
    // Other properties
}
NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
1

As addition to answers containing overriding I'll try to show slightly different approach which may broaden horizons. If you'll change your abstract class implementation you don't even need to override your Collection unless you need explicit implementation for get; or set; because you're specifying generic by inheritance itself

abstract class Pagination<T>
{
    public virtual List<T> Items { get; set; }
}

class Tester : Pagination<string>
{
    public void Test()
    {
        foreach (string item in this.Items)
        {
            // you have declared List<string> from Pagination<T>
        }
    }
}

Also this one may be useful for you: Generic Type in constructor By that approach you would ended up with one base class which will provide you your generic List

abstract class Pagination2<T>
{
    public string Property1 { get; set; }
    public List<T> Items { get; set; }

    public static Pagination2<T> GetInstance<T>()
    {
        Pagination2<T> instance = new Pagination2<T>()
        {
            Items = new List<T>()
        };
        return instance;
    } 
}
Community
  • 1
  • 1
Jaroslav Kadlec
  • 2,505
  • 4
  • 32
  • 43
0
       abstract class Pagination<T>
    {
        public int _offset { get; set; }
        public int _total { get; set; }
        public string previous { get; set; }
        public string next { get; set; }
public List<T> items { get; set; }

        public int getItemCount()
        {
            return items != null ? items.Count() : 0;
        }

    }

    class OrderHeader : Pagination<OrderLine>
    {
        public int orderId { get; set; }

    }

    class OrderLine : Pagination<OrderLineDetails>
    {
        public string sku { get; set; }
        public int qty { get; set; }

    }
Dan Kuida
  • 1,017
  • 10
  • 18