Archive for the ‘Building software’ Category

Moving graphics with trigonometry

Notice: This is intended to involve only very basic concepts.
No matter the scope, most “game” programming these days involves moving pictures around on a computer screen, and performing this task can be very easy with a little basic math (specifically, trigonometry). Small two-dimensional graphics in games are usually called “sprites”, and in this exercise [...]

C# tip: Touching brain with reflection

In the interest of speed, I’ll start with a list of truths I won’t be discussing at length:

Some day you will need to address a problem in someone else’s libraries without the luxury of patching and recompiling their code.
Most people know .NET reflection allows you to interact with types you didn’t have access to at [...]

September 28, 2008 • Posted in: C# • No Comments

C# tip: Dependency Injection on the cheap

I want to keep this post short, so I won’t go into why you should design your code around interfaces, or why injecting dependencies at runtime is such a good idea. Rather, if you’re using .NET and want to quickly use Dependency Injection in your design, but are hesitant to adopt yet another framework [...]

Happy sandboxes and svn switch

I encourage everyone I work with to keep all of their development code in a source repository, even one-off dead-end prototype code they’re not going to check back into a main development tree. If you know you’re going to do destabilizing work, of course you’ll create a sandbox branch from the development branch and [...]

May 8, 2008 • Posted in: Building software • No Comments

C# tip: Formatting currencies

Another simple C# tip I keep forgetting: Need to format a decimal for a specific currency? What about only showing the whole dollar amounts? Use the Decimal.ToString() overload that takes a format string and a CultureInfo instance. Here’s how:

1
2
3
4
5
6
7
8
// CultureInfo is in System.Globalization namespace
[...]

January 16, 2008 • Posted in: C# • One Comment

In the Charlotte Business Journal

Strangely enough, I’ve been quoted in the latest issue of the Charlotte Business Journal. Paparazzi photos to follow.

C# tip: Think of anonymous methods as static

Anonymous methods in C# can make your code more readable, and they are very convenient. They also have magic powers. For example, when first using them they appear to magically persist local variables! Magic, you say? To show this let’s create a delegate and assign a anonymous method to it, and [...]

September 28, 2007 • Posted in: C# • 2 Comments

C# tip: Reading settings from a different App.config

If you’ve used a standard C# App.config, you’re familiar with the ConfigurationManager object in System.Configuration:

string myConfigValue = ConfigurationManager.AppSettings["myConfigKey"];

This reads, by default, from a file similar to your application’s executable; if your executable is “MyApplication.exe”, your config file is “MyApplication.exe.config”. If you want to read from a different file, or just [...]

September 14, 2007 • Posted in: C# • One Comment

C# tip: Checking for empty strings

A common task for any C# programmer is to check the contents of strings. Is a given string initialized, and if so, is it empty? Consider a common check:

if (”" == myString) { …

This technically works fine, assuming the string was initialized. The following check is theoretically a [...]

September 10, 2007 • Posted in: C# • 2 Comments

Safer C# String.Format()

Formatting strings using the String.Format method is straightforward and efficient, but there is a loose coupling between the number of replaceable tokens in your string and the number of strings you provide to insert. If you get the count wrong (which can often happen during maintenance cycles as strings change), your code must deal [...]

September 22, 2006 • Posted in: C# • No Comments

CAB notes: Using ZoneWorkspace

I’ve recently been playing with the Composite UI Application Block, and have had a bit of a problem finding documentation for using the ZoneWorkspace. As usual, after finding a decent demonstration, the steps are simple.

September 2, 2006 • Posted in: C# • 3 Comments

There is an MCSD in our midst

Congratulations to Larry for seeing it through and finishing his MCSD. I haven’t taken a Microsoft certification test in quite some time, and the 70-340 application security exam looks as though it might be very good.

June 18, 2006 • Posted in: C#, Quickpost • 2 Comments

Use PrivateFontCollection for unregistered fonts

To use a font that isn’t installed on a Windows system, use the PrivateFontCollection class to load the font from a file or from a resource. For example:

// Be sure to dispose your PrivateFontCollection
// and Font to avoid an easy leak!
System.Drawing.Text.PrivateFontCollection privateFonts = new PrivateFontCollection();
privateFonts.AddFontFile("mycustomfont.ttf");
System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
this.label1.Font = font;

This is a [...]

May 31, 2006 • Posted in: C# • 10 Comments

aximp.exe and tlbimp.exe

Easy COM interop in Visual Studio usually involves adding a reference to your project within the IDE, which magically produces interop assemblies in your project’s obj directory. These assemblies generally contain all the auto-generated marshalling crap you need, and life is good.
However, if you need to build your project on a build machine that [...]

April 3, 2006 • Posted in: C# • No Comments

Simple C# and Flash interop

I’ve recently promised Larry I’d share new insights about interoperability between C# code and Flash 8.
Flash is often assumed to be only a toy for web designers, but it’s actually a very powerfully programmable visualization tool in the hands of a technically capable artist. In the not-quite-immediate future for Windows programmers, the Windows Presentation [...]

February 11, 2006 • Posted in: C# • 16 Comments

Novice C# threading: WaitHandles

To safely start and stop code running in a background thread, an array of WaitHandles (one each for the start and stop events) and the WaitHandle.WaitAny method is often very handy. For example:

WaitHandle[] waitHandles =
new WaitHandle[] { m_stopEvent, m_startEvent };
[...]

November 3, 2005 • Posted in: C# • No Comments

C# events vs. delegates

After a conversation this morning about this exact subject, I came across this blog entry with a pretty nice description of the differences between C# delegates and events.

May 17, 2005 • Posted in: C# • No Comments

Auto-checking properties in C#

Say you have a C# object that needs all of its properties initialized before it will function properly. You need to check that the properties have been initialized before having any fun. If all of your properties are reference types (are nullable), and are all required, using a little relection in an IsInitialized [...]

May 6, 2005 • Posted in: C# • No Comments

Visual C# 2005: A Developer’s Notebook

O’Reilly “Developer’s Notebook” series is consistently good, presenting significant subject depth in a relatively small number of pages. My favorite so far has been the Niel Bornstein and Edd Dumbill Mono book (I’ve also been pleased with the Hibernate and SWT books). Now, Jesse Liberty has Visual C# 2005: A Developer’s Notebook on [...]

April 27, 2005 • Posted in: C# • No Comments

Sin City, C#, and drink

I finally got around to seeing Sin City. I loved Frank Miller’s graphic novels when I read them way back in school, and I thought they did a wonderful job bringing the look and attitude to the screen. I’m glad I caught it in the theater, unlike some other films lately.
Virtual PC 7 [...]

April 26, 2005 • Posted in: Apple, C#, Cinema • No Comments