ASP.NET: Issues with Generics
The introduction of Generics into .NET 2.0 was a huge improvement over the previous methods of aggregating objects. There are many, many new things you can do with Generics that could not be done previously, in addition to making harder tasks easier and more efficient. For example, I can take a Generics list of my own objects and bind them to a GridView and be able to use the objects’ properties as bound datafields! Something not possible before. However, they do, of course, come with problems of their own. For instance, for the novice to intermediate user, the concept of predicates and actions is enough to make your (my) head spin. And it seems like this is the most efficient way to find things, even though to me, it makes no sense. I suppose until I decide to really hunker down and attempt to learn how to use these features, I’ll stick with my
For Each [whatever] in MyGenericsList ... Next code.
Here is where I come across my problem, though. One of my business objects has a Generics list of another object (
AllPrices as List(Of Price)) consisting of prices and options. This is bound to a GridView, which displays all the properties. On update, I have all the properties of the selected Price object update, some numbers are automatically determined by various variables and business calculations, and then the object is matched up back to its appropriate item in my
AllPrices Generics collection, and the GridView is rebound. This works wonderfully for all but one situation, which has me vexed and confused. Here’s how I match the item and recalibrate my
AllPrices Generics list (in my RowUpdating event):
' "newPrice" contains all my price changes. it is of type PriceInfo.Price Dim updatedprice As New PriceInfo.Price For Each updatedprice In Prod.Prices.AllPrices If updatedprice.PriceID = newPrice.PriceID Then updatedprice = newPrice updatedprice.UpdatePrice() 'updates the database Prod.Prices.AllPrices(Prod.Prices.AllPrices.IndexOf(updatedprice)) = updatedprice 'update AllPrices **** Exit For End If Next GridView1.EditIndex = -1 BindGridView() 'rebinds the AllPrices Generics list to the GridView
What this code does is finds the matching object in the list by PriceID, updates it with the changes (
updatedprice = newPrice), updates the database, and then re-inserts the object into the AllPrices Generics list. However, in one situation, I get an “out of index” error at the line with four asterisks (****), even though the database updates correctly. Logically, this doesn’t make sense to me, since 1) it is finding the object in the collection (it can’t get to that line unless it does), 2) it is updating the database correctly (I’ve verified this in my database), it just isn’t updating the AllPrices list!
What the crap?
Any help is much appreciated. Also, if you’ve experienced similar issues with Generics, please post them! Or if you have some normal, realistic way to use actions and predicates! 🙂
Status: Solved! (12/11/07)
Solution: I was making things way too complex, apparently. I was running two loops (the For loop and the IndexOf() loop) and trying to match objects to one another. Rather than include all this ridiculous complexity, I created a simple counter variable (
Dim i As Integer) outside the For loop and just incremented it after the conditional statement runs (
i += 1), and replaced
Prod.Prices.AllPrices(Prod.Prices.AllPrices.IndexOf(updatedprice)) = updatedprice with
Prod.Prices.AllPrices(i) = updatedprice. Now everything is gravy. Thanks to Nick Berardi for the tips!
No comments yet.