Posts

Showing posts from 2007

How to Determine if a Column Exists in a DataReader

I recently had to work on something which required me to query a database, open the results in a DataReader, and check for the existence of a particular column. While the DataReader doesn't have a method to do this, with just a few steps I was able to accomplish this task. Here's how. First, the DataReader has a method named GetSchemaTable() , which returns a DataTable consisting of information regarding the data contained in the DataReader. The first column in this DataTable is named ColumnName , and its value is (obviously) the name of the column. Knowing this, we can loop through the rows in the DataTable, checking the ColumnName column for a value that equals the name of the column we're looking for. To this end, I wrote a static method in one of my classes which takes the DataReader and the name of the column being sought: (C#) public static bool HasColumn(DbDataReader Reader, string ColumnName) {    foreach (DataRow row in Reader.GetSchemaTable().Rows)  ...

Design Patterns: Abstract Factory

The Abstract Factory pattern is defined as "A pattern which provides an interface for creating families of related or dependant objects without specifying their concrete classes." This is a pattern I'm currently making use of in one of the games I'm developing, Galactica 2180 , which is based on the old Battlestar Galactica TV show from the 1970's. Part of the game involves creating spaceships for each of the different factions in the game. It is in this regard that I'm making use of the Abstract Factory pattern. The game makes use of several engine objects to handle game dynamics: combat, diplomacy, production, etc. The production engine is responsible for queueing object requests by the different factions, such as spaceships, ground vehicles, and so on. But because each faction has their own types of these objects, a generic approach was needed. In this example, we will focus strictly on spaceship creation. For starters, I created an interface to be...

How To Determine What Properties An Object Has At Runtime With .NET Using Reflection

Image
One of the neat things about the .NET Framework is the System.Reflection namespace. In Microsoft's words, it "contains classes and interfaces that provide a managed view of loaded types, methods, and fields, with the ability to dynamically create and invoke types." In other words, you can interact with and manipulate objects at runtime without having any foreknowledge of what the objects actually are. I'm currently working on two games in my spare time in which this functionality could come in handy. In particular, I'm developing a strategy game based on Battlestar Galactica (the original series, not the remake). The interface includes a star map where the player can view planetary systems and spacecraft. Part of the plan for the game is to allow the player to double-click on any of the ships which are visible on the map and be presented with a window displaying the ship's stats. However, all ships are not created equal: I have a Spacecraft base class, ...