tags:

views:

2413

answers:

5
public class Item
{
        private int  _rowID;
        private Guid _itemGUID;

        public Item() { }

        public int Rid
        {
            get
            {
                return _rowID;
            }
            set {  }

        }

        public Guid IetmGuid
        {
            get
            {
                return _itemGuid;
            }
            set
            {
                _itemGuid= value;
            }

        }

}

The above is my custom object.

I have a list:

List<V> myList = someMethod;

where V is of type Item, my object.

I want to iterate and get the properties as such

foreach(V element in mylist)
{
   Guid test = element.IetmGuid; 
}

When I debug and look at the 'element' object I can see all the properties in the 'Quickwatch' but I cannot do element.IetmGuid.

+1  A: 
foreach( object element in myList ) {
    Item itm = element as Item;
    if ( null == itm ) { continue; }
    Guid test = itm.ItemGuid;
}
TcKs
A: 

Your list should be declared like this:

List<V> myList = someMethod;

Where V is the type item.

and then your iteration was correct:

foreach(V element in myList)
{
    Guid test = element.IetmGuid;
}
Sorry about the formatting, I do have it declared as List<V> myList = someMethodThe error I get is V does not contain a definition for IetmGuidI guess I want to determine the property at run time and V is not declared until runtime
+3  A: 

Hi John,

Try declaring your list like this:

List<Item> myList = someMethod;
Daniel M
+1, I don't see why he's using V when the class is called Item.
Kon
fallen888:I want to be able to build a list of Items or User objects. Both are populated by stored procs and the base table for each have IetmGuid,so based on run tim I can either do List<Item> or List<User> and hence the choice of List<V> where V is determined at run time for Item/User
But is a user an item or an item a user? If they share a common interface you can break it out.
Will
@John, then you'll have to cast and do a null check as suggested in another answer here. :)
Kon
Yeah, that kind of defeats the purpose of typed lists. You need to either express some relationship between an "Item" and a "User" in your code (through either inheritance or an interface) or use two separate lists.
Joel Coehoorn
+3  A: 

Are you putting a constraint on the generic type V? You'll need to tell the runtime that V can be any type that is a subtype of your Item type.

public class MyGenericClass<V>
  where V : Item  //This is a constraint that requires type V to be an Item (or subtype)
{
  public void DoSomething()
  {
    List<V> myList = someMethod();

    foreach (V element in myList)
    {
      //This will now work because you've constrained the generic type V
      Guid test = element.IetmGuid;
    }
  }
}

Note, it only makes sense to use a generic class in this manner if you need to support multiple kinds of Items (represented by subtypes of Item).

akmad
I did thispublic class MygenericClass<V>where V:new()
That's only requiring that type V have a parameterless constructor. If you always want to use the generic type V as an Item (or subtype), you should constrain the type as I showed above.
akmad
akmad: Say if I always required that type V have a parameterless constructor. would my approach work?
+1  A: 

Why do you keep spelling it IetmGuid here?

FlySwat