OT: generics and contains

OT: generics and contains

Old forum URL: forums.lhotka.net/forums/t/2226.aspx


mtagliaf posted on Monday, January 29, 2007

Just getting into generics - trying to use List(Of T), in a non-CSLA environment.

I need to implement Exists or Contains to see if an element is already in the List before I add it again. However, I want to do it by "primary key" (an integer), and the Contains method requires a type <T> as the parameter. I don't want to have to create an instance of type <T> if I'm not going to put it in this list, so what do I do to see if the object with a certain integer key is already in the List?

thank you,
matt tag
 

SonOfPirate replied on Monday, January 29, 2007

You'll want to create an overloaded Contains method accepting the integer value as a parameter.  If your collection is not keyed by default (such as a dictionary), then iterate through the items in the collection and check the appropriate property for a match.  You will have to implement a contraint on T that defines the property you are checking or you will have compilation properties (unless you are working in a concrete class that specifies T).

HTH

 

Copyright (c) Marimer LLC