(VB.NET) Generics and how I might use them, eventually…

I’ve started playing with Generics some lately (as if I needed something else to keep me busy) and they really aren’t that hard to figure out with the right examples. (Start simple…)

A lot of folks have been using the Swap example, which is simple enough to be fairly obvious when you look at it. (If you haven’t seen it, I’m not going to pilfer it, just go look here and come back… I’ll wait.)

So anyway, that got me thinking about more complex implementations, such as in Game Development. Specifically, how can I benefit from using these in HA!? Would I rewrite code to take advantage of them, or just keep them in mind when spewing forth new code? It seems to me like my inventory management (backpack) code is a good place to consider an overhaul with generics. The code kinda sucks as is anyway, IMO, so why not re-write it?

The inventory code works like this. Basically I have an arraylist that contains objects of all different types (boots, swords, gloves, armor, etc…) Occasionally, I need to fish it out and do something with it, such as assign it to a specific slot in the equipped inventory.

If you’ve never played HA! then I’ll explain how it happens: When the hero wishes to equip an item, I open the backpack (metaphor) and dig through EVERY item, looking to see what type it is. I display any items that match the type allowed by the inventory slot the hero wishes to equip. This sounds like a good candidate for some generic functions. Right now it’s a big ugly mess. The routine that digs through the backpack (I called it ShowBackpack… pretty clever, no?) has to compare a value I passed in (its actually an enum, not an “object” ItemType.Boots, ItemType.Helmet, etc…)

            Case ItemType.Helmet
                Dim thing As Object
                WriteAt(0, yPos, "Helmets:", ConsoleColor.DarkYellow)
                yPos += 1
                WriteAt(0, yPos, "--------", ConsoleColor.DarkYellow)
                yPos += 1

                WriteAt(1, yPos, "No items of this type.")
                For Each thing In m_Hero.Equipment.BackPack
                    If thing.type = ItemType.Helmet Then
                        intQty = thing.quantity
                        Dim strItem As New StringBuilder

                        strItem.Append(startLetter & "] " & thing.Name)
                        If intQty > 1 Then strItem.Append(" (Qty: " & thing.quantity & ")")
                        strItem.Append("               ")
                        WriteAt(1, yPos, strItem.ToString)

                        yPos += 1
                        startLetter = Chr(Asc(startLetter) + 1)
                    End If
                yPos += 2

All Items such as armor, weapons, boots, etc derive the the ITEMS base class. This class has a property called Type that uses the same ItemType Enum that I’m passing in above. (Hey, cut me some slack, I wrote this code YEARS ago…) I’ve got a case statement for every item type, but they all look a lot like this one. The main differences are the couple lines at the top that produce a heading and the line that refers to the specific enumerated value (If thing.type = ItemType.helmet Then)

So the question is, would this be suitable for generics, or just refactoring into more efficient code. I know I could eliminate the ItemType enum and just look up the actual object type (Helmets inherit from Armor which inherits from Item, etc…)

some research and experimentation later…

Ok, currently this is not a job for generics. The stuff I was looking to modify basically just does a lookup based on a type, as opposed to accepting data of various types. (If we go back to the swap example, that’s a good use for generics, since it lets you provide strings or integers or whatever…) This is definitely a job for refactoring though… 🙂

So where else in my code could I use generics? More on this later…

Tinggalkan komentar