Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and using a custom List<T> in C#

I am trying to use a customized List were I have added a few additional tools. I want to apply this list to a long list of customized classes that I have created. All of the classes have an ID number and some of the tools in the List use the ID.

Here is a portion of my code that I am trying to use. I hope this help you understand my problem.

namespace Simple_Point_of _Sales_System
{
    public class MyList<T> : List<T>
    {
        internal int SetID()
        {
            return this.Max(n => n.ID) + 1;
        }
        internal T Find(int ID)
        {
            return this.Find(n => n.ID == ID);
        }
        internal T Add(T n)
        {
            Read();
            Add(n);
            Write();
            return n;
        }
        internal void Remove(int ID)
        {
            Read();
            if (this.Exists(t => t.ID == ID)) RemoveAll(t => t.ID == ID);
            else MessageBox.Show(GetType().Name + " " + ID + " does not exist.", "Missing Item", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Write();
        }
        internal void Edit(int ID, T n)
        {
            Read();
            if (this.Exists(t => t.ID == ID)) this[FindIndex(t => t.ID == ID)] = n;
            else MessageBox.Show(GetType().Name + " " + ID + " does not exist.", "Missing Item", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Write();
        }
        internal MyList<T> Read()
        {
            Clear();
            StreamReader sr = new StreamReader(@"../../Files/" + GetType().Name + ".txt");
            while (!sr.EndOfStream)
                Add(new T().Set(sr.ReadLine()));
            sr.Close();
            return this;
        }
        internal void Write()
        {
            StreamWriter sw = new StreamWriter(@"../../Files/" + GetType().Name + ".txt");
            foreach (T n in this)
                sw.WriteLine(n.ToString());
            sw.Close();
        }
    }

    public class Customer
    {
        public int ID;
        public string FirstName;
        public string LastName;
    }

    public class Item
    {
        public int ID { get; set; }
        public string Category { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
    }

    public class MyClass
    {
        MyList<Customer> Customers = new MyList<Customer>();
        MyList<Item> Items = new MyList<Item>();
    }
}
like image 610
Makai Avatar asked May 10 '13 04:05

Makai


People also ask

What is list T?

List<T> class is the generic equivalent of ArrayList class by implementing the IList<T> generic interface. This class can use both equality and ordering comparer. List<T> class is not sorted by default and elements are accessed by zero-based index.

How to create list in C#?

The following example shows how to create list and add elements. In the above example, List<int> primeNumbers = new List<int>(); creates a list of int type. In the same way, cities and bigCities are string type list. You can then add elements in a list using the Add() method or the collection-initializer syntax.

What is list int in C#?

Lists in C# are very similar to lists in Java. A list is an object which holds variables in a specific order. The type of variable that the list can store is defined using the generic syntax. Here is an example of defining a list called numbers which holds integers.

How to access list of objects in C#?

The code is: struct m1 { public int a; public int b; } int main() { List <m1> mList; m1. initialize; //new.


1 Answers

I think your custom list needs to put on some constraints on the generic type to allow that. I would update your signature to something like

public class MyList<T> : List<T> where T : IIdentity { .... }

Here I used IIdentity as the interface defining ID, but it could also be a class.

To update your code I would do something like this:

public interface IIdentity
{
    int ID { get; }
}

public class Customer : IIdentity
{
    public int ID { get; set;}
    public string FirstName;
    public string LastName;
}

public class Item : IIdentity
{
    public int ID { get; set; }
    public string Category { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}

I changed the ID in Customer to be a property instead of instance variable.

like image 80
Tomas Jansson Avatar answered Oct 04 '22 20:10

Tomas Jansson