Storing generic list in variable???

S

Seth Gecko

Hi

I am working with generic lists of various objects and a control
dealing with these lists. For instance:

A parent form holds:
dim Walls as List(Of wall)
dim Segments as List(Of segment)

The parent form have a custom control, which have a public sub looking
this:
Public sub InitTree(Of T as New)(ByRef OwnerList as List(Of T))

This means I can pass both Walls and Segments as parameter to
InitTree, which works like a charm. My problem is that I want to store
the OwnerList in a private variable inside the control, but how
exactly do I define a variable that can hold a List(Of T)? I still
want to be able to do GetType(T) on the variable, so I don't want to
simply store it as an Object or ICollection or something like that.

Suggestions please.

Regards
....Seth
 
F

Frans Bouma [C# MVP]

Seth said:
Hi

I am working with generic lists of various objects and a control
dealing with these lists. For instance:

A parent form holds:
dim Walls as List(Of wall)
dim Segments as List(Of segment)

The parent form have a custom control, which have a public sub looking
this:
Public sub InitTree(Of T as New)(ByRef OwnerList as List(Of T))

This means I can pass both Walls and Segments as parameter to
InitTree, which works like a charm. My problem is that I want to store
the OwnerList in a private variable inside the control, but how
exactly do I define a variable that can hold a List(Of T)? I still
want to be able to do GetType(T) on the variable, so I don't want to
simply store it as an Object or ICollection or something like that.

Suggestions please.

You have to make the class generic in that case. So the custom control
has to be a generic control. In winforms that's not a good idea but if
you want to, you then do:

Private _theList As List(Of T)


If you don't want a generic control, use IList

FB


--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
S

Seth Gecko

You have to make the class generic in that case. So the custom control
has to be a generic control. In winforms that's not a good idea but if
you want to, you then do:

Private _theList As List(Of T)

If you don't want a generic control, use IList

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website:http://www.llblgen.com
My .NET blog:http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------- Hide quoted text -

- Show quoted text -

So that means that I can have function that take generic arguments,
but I cannot store them as the same type? I agree that making control
generic is not an option. I can live with storing my collection as an
IList (which is what I am doing right now), but I don't want to lose
the T. Is there a way to store the type, so that I can still do "dim x
as new T"?
 
F

Frans Bouma [C# MVP]

Seth said:
So that means that I can have function that take generic arguments,
but I cannot store them as the same type? I agree that making control
generic is not an option. I can live with storing my collection as an
IList (which is what I am doing right now), but I don't want to lose
the T. Is there a way to store the type, so that I can still do "dim x
as new T"?

Not if the class isn't generic. THe thing is that if the class is
generic of T, T is known at compile time and thus can be evaluated
throughout the code in the class, thus in the class' private member
declarations.

WITHIN the generic method, T is known of course, however the private
member declaration doesn't know T because the scope of T THERE is the
class, and the class isn't generic.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top