XNA Game Studio 2.0 Released

So I've dropped the ball on XNA. I've been meaning to read and learn about XNA for a while now. Microsoft has done something truly revolutionary here. They're taking efforts to encourage the little guy to write games for their console. As far as I know, they're the first console to do this. Please correct me if I am wrong about that. I'll get to the point now. Microsoft released a new version of XNA Game Studio which is the development environment for working with XNA games. I'm a version behind...

XNA Game Studio Express works along side Visual Studio 2005. In previous version I don't think you were able to use anything except for the C# Express Edition. According to the XNA web site, the new version supports the following versions of Visual Studio.

  • Visual C# 2005 Express Edition
  • Visual Studio 2005 Standard Edition
  • Visual Studio 2005 Professional Edition
  • Visual Studio 2005 Tools for the Microsoft Office System
  • Visual Studio 2005 Team Edition for Software Architects
  • Visual Studio 2005 Team Edition for Software Developers
  • Visual Studio 2005 Team Edition for Software Testers
  • Visual Studio 2005 Team Edition for Database Professionals
  • Visual Studio 2005 Team Suite

They also have some nifty cool starter kits for those of you who hate beginning projects from scratch. They should also be very helpful while learning to develop games using XNA. There are also tutorials and articles on the site which should help us along while learning XNA.

For those of you who somehow missed XNA completely, it is a game development framework. It helps you develop games for Windows as well as Xbox 360.

Now that I've got an Xbox 360 I think I'll finally hop on board if I get the chance. I am only one major version behind on this one.

Static Methods and Interfaces in C#

Someone just commented on my C# interfaces article on ASP Alliance asking an interesting question, and I figure I will respond to it here since I can give a better response here. I don't want to clutter the article comments. Also I can add nifty cool pictures, format all of my text nicely, and write a heck of a lot more here.

This is the excellent question I was asked.

All my classes implementing my interface should have a static funtion.
Why can't I have a static function in an interface???

-Jan

This is how I responded in the article.

One important thing to understand about static methods is that they don't really need to be tied to classes. Static methods basically just exist for convenience.
An interface is as the name says an interface to something. Think of it like an automobile interface. There are defined ways of working with the car. They have a steering wheel a gas pedal and brakes. An interface defines that and all automobiles will have those, but an interface doesn't define anything about "cars in general" (static) it defines what will be the interface for individual cars (instances).

As I was saying interfaces are used for defining interfaces for code. I like my car analogy, but I think I'll use a sail boat analogy this time.

Think of writing an interface in C# called ISailable. This objects implementing this interface will be sailboats. So what does every sailboat need to be able to function. For simplicity I'll say that we just need to have sails and a wheel. All sailboats will also need to be able to sail. So now we've defined how our ISailable objects work. We can now create sailboats using this nice interface. Notice we haven't defined how large the sails are or what style of sails. We just said we need sails. We also did not define the wheel. Is it metal or wood? How large is it? We don't care in the interface the classes implementing the interface will each define those differently. Notice that different boats have very different types of sails.

We might define out ISailable interface in the following way. Keep in mind I am using a lot of simplicity here.

namespace Enrick.Interfaces
{
    public interface ISailable
    {
        void Sail(string destination);
        void Steer(string direction);
        void RaiseSails();
        void LowerSails();
    }
}

Then once we have that defined we are able to implement the interface and create our first sailable object which will of course be a sailboat.

namespace Enrick.Boats
{
    public class SailBoat : Enrick.Interfaces.ISailable
    {
        #region ISailable Members

        public void Sail(string destination)
        {
            // Write sailing logic here. It should be specific to this type of boat.
        }

        public void Steer(string direction)
        {
            // Write steering logic here for this exact type of boat.
        }

        public void RaiseSails()
        {
            // Write logic to raise these specific sails here.
        }

        public void LowerSails()
        {
            // Write logic to lower these specific sails here.
        }

        #endregion
    }
}

Static interfaces do not really apply to what we've defined though. We've just been talking about how to interface with individual sail boats. We haven't gotten to static methods really. This is because static methods aren't really necessary. Many languages have them, but they could just as easily be outside of the class and have the same functionality. Static methods are by definition methods which aren't specific to individual instances of classes. This means we could define these methods anywhere and achieve equivalent functionality. They also don't really apply with interfaces and the C# language specification doesn't allow static methods on interfaces because they don't really make sense with interfaces.

There are circumstances where someone might want to have a static method on an interface. Perhaps the static method was going to be a way of performing an action on an entire collection of the instances of the objects. Perhaps you have a fleet of ISailable ships that you will want to sail together. This would make sense to have a static method which would be associated with the class, but it isn't necessary. Instead we will create a static method elsewhere. It will not be tied to our interface. We can have it take a list of ISailable objects and then it can perform the sailing operation of our fleet.

public void SailFleet(List<ISailable> fleet, string direction)
{
    foreach (ISailable boat in fleet)
    {
        boat.Sail(direction);
    }
}

Notice here that I've defined a method which might have been static before on each of the boats and each one would have take a List of their instances, but that would be silly. This solution allows us to have methods which work with ISailable objects but they need not be defined as requiring an interface. Sadly there is not a way of having static methods on interfaces in C#.

I hope that answers some questions. As always, thanks for reading!

w00t! The Word of the Year is Awesome

Yes, you read that correctly, because Merriam-Webster's Word of the Year is w00t. Most of you have probably heard this interjection often exclaimed by geeks winning some sort of competition usually a multiplayer game. I know not where the word originated. I would assume an online computer game. Perhaps counter strike. I am quite impressed that Merriam-Webster would allow w00t into the contest. I would venture a guess that it is the only word in the competition which uses numbers and letters. Perhaps this is the beginning of a deluge of changes in the English language. The Internet has spawned changes faster than could probably have been imagined in the past. It takes writing over time for a written language to change, but the vast amount of writing which occurs every day on the Internet is an extreme. I expect we'll be seeing far more changes to the language we love in the next few years. What word will be next on the list. I can only wait and hope for 1337. Perhaps it will be next year's word of the year. It lacks letters completely, and overcoming that hindrance will surely pave the way for future Internet-based words.

w00t Merriam-Webster !5 1337!!!!1!

Great Conditional Logic

It is amazing some of the relics that can be found while code reviewing old code. Sometimes developers do things without thinking and make some great code. I am of course referring to some really funny code such as what I found a few minutes ago. I am always annoyed when boolean values are checked explicitly in conditional statements like the following code.

if (IsValid == true)

This bothers me because the true is completely useless. I would read this code as "If is valid is true", and that is kind of silly when I could say "If is valid".

if (IsValid)

This is just part of the annoyance in the code. Now I've cleaned it up a bit here, because it didn't really have a variable named IsValid. It was actually a variable named edit. We were not sure if edit meant that someone was allowed to edit or if it was in edit mode or what.

This was just slightly annoying. The really amazing part was in the else of this if statement. It had a great bit of else if logic. The code looked overall like this.

if (edit == true)
    // Lines of code here
else if (edit == false)
    // Lines of code here

I now wonder what possible other condition is there? In the else is there another state for edit? It is not a reference type, it is a value type so it can never be null. Just got to be careful of boolean operators there are so many other values for them other than true and false.

I hope everyone enjoyed this fun little code snippet.

Little Bobby Tables

One web comic I commonly read is xkcd. It is a great comic that has a lot of good computer as well as just nerdy jokes. It has a great comic about SQL injection attacks and why you need to sanitize your database inputs. This is a lesson in what to not name your child.

I hope that everyone who reads this gets this joke. Quite amazing. This comic is full of great stuff like this. Try to not waste your day reading this grade A material.