tags:

views:

116

answers:

2

I would like to create a list of objects in which there is a generic list.

So what I have is this object:

public class agieDBColumn where dataTp:IComparable{ private string _header; private string _longHeaderName; private List _data;

public string header { get { return _header; } set { _header = value; } }

public string longHeaderName { get { return _longHeaderName; } set { _longHeaderName = value; } }

public List data { get { return _data; } set { _data = value; } }

}

And this is how I would like to use it:

List columns;

Then I would like to add new instances to the list "columns" specifying the type.

There does not seem to be a syntaxs that allows this though as I get "Error 1 Using the generic type 'Picker.DBColumn' requires '1' type arguments Frm.cs 56 9 FCAgieStratPicker " when trying to define the type of List I would like.

Is this even possible? Or would I need to use the base class object in this case?

Maybe there is a better way to handle the data I would like to represent in memory.

A: 

Your type is generic. If you want to create a list of that type you'll need to specify the type for your class as well.

List<agieDBColumn<int>> columns = ...
tvanfosson
Yes but I don't want to at that point. I want to define a different type for the object when adding it to the list. I think I may have actually found my solution now here: http://stackoverflow.com/questions/754341/adding-generic-object-to-generic-list-in-cI will likely have to make an Interface.
crusy_
@user - but you still have to supply the type, in this case, the interface type. Note that you already are constraining it to be IComparable so you could use that unless you need access to other methods when you retrieve it from the list.
tvanfosson
Yes I constrained it to IComparable and so I guess I would be able to use that. I think 'chiccodoro' actually gave me the idea to solve the original problem though and you gave the correct answer to the question!(So who should I give the accepted answer to? I am new here...)
crusy_
@user -- I suggest that you edit your question to indicate that an alternate solution worked better than your proposed solution and then accept the answer that you finally used. The key point is editing your question so that the accepted answer makes sense in context.
tvanfosson
A: 

It seems what you eventually want to achieve is already provided by the System.Data.DataTable class.

Adding columns:

DataTable dataTable = new DataTable();
DataColumn column = dataTable.Columns.Add(header, type);
column.Caption = longHeaderName;

Adding rows:

dataTable.Rows.Add(someIntValue, someStringValue, ...)
chiccodoro
OK I will need to read up on how to use that :-)
crusy_
That look very promising for my needs indeed. Thanks!
crusy_