Reputation: 455
Until now I used multidimensional arrays int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6}};
but now I need something dynamic like list. Is there such a thing as multidimensional lists? I want to create something like a table with 4 columns. The 4th column should be a 10sec timer and when the 10seconds pass and column 2 and 3 on the specific line are not filled the whole line should be deleted from the list.
Upvotes: 1
Views: 3868
Reputation: 23462
You can just use a list that contains list like:
var multiList = new List<List<int>>();
After reading through your question again I don't think this is what you want. My guess is that you want something like this:
public class Item
{
public int? Col1 { get; set; }
public int? Col2 { get; set; }
public int? Col3 { get; set; }
private Timer Timer { get; set; }
public List<Item> ParentList { get; set; }
public Item()
{
Timer = new Timer();
Timer.Callback += TimerCallBack();
// Set timer timeout
// start timer
}
public void AddToList(List<Item> parentList)
{
parentList.Add(this);
ParentList = parentList;
}
public void TimerCallBack()
{
if(!Col3.HasValue || !Col2.HasValue)
{
ParentList.Remove(this);
}
}
}
....
var list = new List<Item>();
var item = new Item { /*Set your properties */ };
item.AddToList(list);
That should get you started. You can read about the Timer
here.
Upvotes: 1
Reputation: 96702
It really depends on your application. You could use List<List<int>>
, for instance, and add new items thusly:
myList.Add(new List<int> { 0, 0, 0, 0 });
It sounds from your last comment that you're going to be applying specific logic to the items in this list, which suggests the possibility that you should create a class for your items, e.g.
public class Item
{
public int Col1 { get; set; }
public int Col2 { get; set; }
public int Col3 { get; set; }
public int Col4 { get; set; }
public bool CanRemove { get { return Col2==0 && Col3 == 0 && Col4 == 0; } }
and then create a List<Item>
. Then you can remove entries from it via:
var toRemove = myList.Where(x => x.CanRemove).ToList();
foreach (Item it in toRemove)
{
myList.Remove(it);
}
Upvotes: 1
Reputation: 22829
I suggest you create a class that encapsulates what you said a single "line" in your list should do. It will give your code that much more expressiveness and be far more readable, which is the core trait of any good code.
So in the end you will have List<YourSpecificClass>
Upvotes: 2
Reputation: 108937
Try
class Foo{
public int Col1 { get;set; }
public int Col2 { get;set; }
public int Col3 { get;set; }
public int Col4 { get;set; }
}
and List<Foo>
Upvotes: 5
Reputation: 29196
what you want is List< List< yourtype > >
this will give you what you want:
List< List<int> > lst = new List< List< int > >;
lst.Add( new List<int>() );
lst[0].Add( 23 );
lst[0][0] == 23;
Upvotes: 3