473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Storing generic list in variable???

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
3 2237
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: prasanthag | last post by:
Hi, I am a newbie to this group. I have a problem in handling the variable arguments passed to a function. My requirement is like this. I have 2 functions say, void funcX(int i, int j);...
4
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172:...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
1
by: Miesha.James | last post by:
Hello, I'm trying to rewrite visual c++ code into visual c++ .NET code and I've run across a problem with storing objects into a list. Here;s an example of the code I have: ref struct...
0
by: shapper | last post by:
Hello, I have an enum as follows: Public Enum Feature Title Content Date Search End
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
8
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange...
5
by: Cirene | last post by:
I am writing a shopping cart app in asp.net. So far I created 2 classes, 1 for customer_info (name, address, email), 1 for product_info (prodname, price, description). As the shopper adds...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.