Archive for the ‘General Programming’ Category

Accessing the stage in Flex

Sunday, November 9th, 2008

When creating components in Flex, designers sometimes need to attach events to the main stage. Unless the application has reached creationComplete, the stage property of custom components will be null.

If you need to, for example, attach a MOUSE_MOVE event to the application stage from a component that doesn’t include a creationComplete override, you have two options.

Wait for Event.ADDED_TO_STAGE

Attach Through the System Manager Object

The system manager parents all displayable elements within the Flex application. It is an elegant way of accessing the stage through custom flex components which have not yet been added to it.

CommandEventHandler Event Won’t Fire for Button in ASP.NET Custom Control

Wednesday, September 24th, 2008

Problem: I created a custom control with a dynamic button and attached an event handler to that button.  When I run the control, clicking the button causes a postback but the event is not fired.

Solution: Changed the class inheritance from WebControl to CompositeControl.  Re-compiled and it worked like a charm.

What I Learned from MUDs

Friday, August 1st, 2008

The first bit of code I ever worked on was ROM 2.4, a text-based MUD server written in C.  At the time I didn’t know the first thing about programming, but through trial and error I was able to feel my way around the source code and slowly learn how the language worked and how to make the program do what I wanted.  Some experienced programmers called me the “snippet king” because I used a lot of pre-written additions to expand m game, but I see it as part of the learning process.  If I were to work on a MUD again, I now have the knowledge to do all of the programming myself - but if it weren’t for my baby steps back then I may never have become a programmer at all.

Text-based gaming is all but unheard of now that games like World of Warcraft and Everquest have taken hold.  In my opinion this is a crime because the worlds created through words were so detailed and so interactive that replacing them with pretty graphics makes for a much shallower experience.  It used to be possible to make life-long friends and really share interested, now the genre has been opened up to the lowest common denominator and there is so much swearing and name-calling that many older, more mature players don’t even bother.

I learned a lot of skills from my days playing MUDs that have helped me in my professional life:

  • Programming (obviously)
  • How to interact with others online
  • How to type very fast
  • How to write with personality
  • How to be thick-skinned (when people think they are anonymous, they are quicker to criticize and personally attack you, especially when you’re involved in content creation)

Optimize SQL Queries by Using Aliases

Monday, June 30th, 2008

When joining tables in SQL, we often use aliases to shorten table names. Consider this query joining order lines (details) with orders inside the database:

The above may work, but behind the scenes the database’s query analyzer has to associate each of the selected columns with their respective tables. Although this is a fast process, it can be skipped entirely by simply fleshing out the query like this:

Replacing / Adding Line Breaks in GridView Text

Tuesday, May 27th, 2008

The GridView is a powerful control for quickly and easily displaying tables of data. However, a raw dump of information is not always good - when displayed by a web browser, normal line breaks are simply rendered as spaces.

For long blocks of text, it may be desirable to have your GridView insert HTML line breaks into your data. This can be accomplished either programatically or declaratively.

Programatically

As a programmer, my first instinct is to try to solve the problem using code behind. I add a RowDataBound event handler to my GridView and create the command this way:

Although it works, it has several drawbacks:

  • This solution uses a magic number to cause the compiler to replace the third column in the row.  If the structure of the GridView were to change, this function may break
  • This solution requires the developer to be aware of the final layout of the GridView and to make the connection between the control’s declaration and its logical code.

Use The Design

By far, the better solution is to simply declare the formatting changes in the same place as the GridView.  Using a Template field, I can add line breaks to my message by adding this:

<%# Eval(”Message”).ToString().Replace(”\n”, “<br />”) %>

More Information

This solution assumes the contents of “Message” are not null.  For more information about this technique (including how to deal with null values), I recommend the ASP.NET message boards: http://forums.asp.net/p/1027728/1403884.aspx

Take Yourself Less Seriously, Manage Scope Creep

Tuesday, May 13th, 2008

Given the oportunity, your boss and your client would add so many requirements to your project that your only hope to meet your deadline was to forsake your personal life and donate your evenings and weekends to your job.

This is uncool.  Unless you’re in Houstin trying to bring home Jim Lovell, no one’s life but yours hangs in the balance of your work.  I urge everyone to ask themselves how important, really, is their function.  We tend ot build up walls around us and believe that our problems are big deals, but the reality is that in ten years we won’t remember what we did at work today.

So next time you’re asked to add “one more feature” to your prototype, decide if it’s best for the project, and if it’s possible for you.  It can be hard to say no, but humans weren’t intended to sit in front of the computer programming meaningless crap all day.  Go out and enjoy your life!

C#: Using Suffixes to Declare Data Literals

Thursday, May 1st, 2008

This isn’t new, but handy to have.  In order to tell the compiler which data type we’re using, we use suffix notation as follows:

  • Unsigned integer, U: e.g. 34506U
  • Long integer (signed), L: e.g. 5297532L
  • Unsigned long integer, UL: e.g. 30958UL
  • Float, F: e.g. 13.6F
  • Double, D: e.g. 14.3D
  • Decimal, M: e.g. 19.95M