1

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<dataTp> where dataTp:IComparable{ private string _header; private string _longHeaderName; private List<dataTp> _data; public string header { get { return _header; } set { _header = value; } } public string longHeaderName { get { return _longHeaderName; } set { _longHeaderName = value; } } public List<dataTp> data { get { return _data; } set { _data = value; } } } 

And this is how I would like to use it:

List<DBColumn> 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<dataTp> 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.

2 Answers 2

1

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, ...) 
Sign up to request clarification or add additional context in comments.

1 Comment

That look very promising for my needs indeed. Thanks!
0

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 = ... 

4 Comments

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: stackoverflow.com/questions/754341/… I will likely have to make an Interface.
@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.
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...)
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.