By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

Storing generic list in variable???

P: n/a
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

Feb 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Seth Gecko wrote:
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#)
------------------------------------------------------------------------
Feb 14 '07 #2

P: n/a
On Feb 14, 10:03 am, "Frans Bouma [C# MVP]"
<perseus.usenetNOS...@xs4all.nlwrote:
Seth Gecko wrote:
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#)
------------------------------------------------------------------------- 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"?

Feb 14 '07 #3

P: n/a
Seth Gecko wrote:
On Feb 14, 10:03 am, "Frans Bouma [C# MVP]"
<perseus.usenetNOS...@xs4all.nlwrote:
Seth Gecko wrote:
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#)
--------------------------------------------------------------------
----- 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"?
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#)
------------------------------------------------------------------------
Feb 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.