For Each issue w/ NameObjectCollectionBase

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

I have a class that inherits NameObjectCollectionBase called Products. In
the class' constructor, I add items using BaseAdd(key,value), where key is a
string representation of an id and value an instance of another class called
Product. For example:

Me.BaseAdd(Id,New Product(Id, Name, Price))

In another class, I would like to Enumerate the contents of the Products
class like so:
Dim product As Product

For Each product In Products

Response.Write(product .Id & ":" & product .Name & ":" & product .Price
& "<BR>")

Next

Unfortunately, when the code hits the line For Each product In Products, I
get an error that reads:

Specified cast is not valid.

It appears that the product is actually a string, not the object. What am I
doing incorrectly?

Thanks,

Craig Buchanan
 
Mike-

Thanks for the reply.

I get an error proclaiming that 'Product' isn't a member of 'Products'.
Obviously, as I haven't added a 'Product' property.

What am I missing? Perhaps an Item property?

Thanks,

Craig
 
Back
Top