473,705 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2259
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.usenet NOS...@xs4all.n lwrote:
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.usenet NOS...@xs4all.n lwrote:
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
2515
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); void funcY(int i, int j,char *name);
4
2954
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: returning address of local variable or temporary". In good old 'C', that would indicate that a 'static' was missing from the declaration of the returned value.
6
5851
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 ID="FldType_add" Runat="server" DataSource='< %#GetFieldType()%>' DataValueField="Type" DataTextField="Type" /> Oce the page is loaded all the values are added to the dropdown list. But when I thought of getting the selected value from the...
1
2174
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 Cookies { String^ Name;
0
1464
by: shapper | last post by:
Hello, I have an enum as follows: Public Enum Feature Title Content Date Search End
10
2943
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 generic list of business objects. I'm able to see that the type is the correct one in the debugger. However when I try to traverse the list using the type I can't compile. The same type variable I've verified as being passed
8
3153
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 formatting) to show what my issue is. I have a generic abstract class here: public abstract class MyGenericAbstractClass<Twhere T : MyInterface
5
1239
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 things to the cart and navigates throught the site to shop more, should I store this info in a session var? What is the typical way? Should I create another class for cart and store an array of product_info to keep track of what's in the cart,...
11
2543
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 generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
9275
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9140
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8981
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5934
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4705
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3140
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.