Thursday, August 23, 2007

Yes, you can use C# controls in VB.NET code

I've been helping a student out using the Outlook-like Time Field Control I authored. She's been trying to port the code from C# to VB.NET. I've been trying my best to help her but unfortunatly it's been a long time (luckily) since I've had to deal with anything coded in VB.NET.

After a number of back and forths, I finally asked, "why exactly are you trying to port it over?" and her response was "surley i can't still use the control "as is" if its in C#?"

It seems that in all of her classes, no one failed to mention the CLS/Common Language Specification. She was even more surprised to hear that much of the CLR and .NET framework were authored in C#, even though she has been using it trouble-free in VB.NET.

Side note: If you are interested in why much of it is authored in C# I suggest you read Scott Guthrie's interview in the Architecture Journal.

This highlights one of the fundamental benefits of the .NET "world" - language neutrality. It doesn't matter if your coding in C#, VB.NET, Managed C++ or even for that matter, you can rest assured knowing that you can use components authored in any one of the supported .NET languages. Now that is productive!

Tuesday, August 21, 2007

Blackberry 8800 software update - where are you?

There is nothing more frustrating than a buggy handheld device - except for close to 5 months of bugginess! I've been a Blackberry user for years now and I've always been impressed with RIM's ability to crank out fixes and updates - that is, until the 8800. I picked mine up on March 31st, 2007 (no thanks to the 2 month hold-up), and within a few days had experienced a number of NullPointerExceptions, random other crashes, freezes, etc. "No worries, they'll fix it soon" I said - now, months later, I'm left wondering when they are going to fix all these issues!

I've heard rumors of a 4.3 handheld code for the 8800 - thats nice and all, but the 8800 has some serious flaws that deserve some fixes, sooner rather then later.

Am I the only one who has serious problems with this device?

Wednesday, August 15, 2007

C# ? operator - Nullable shorthand

The Nullable structure is a new generic structure to C# 2.0 which allows null values for value types (reference types obviously support this). This is useful in avoiding the magical "-1"'s and such.

The C# 2.0 compiler also provides a handy shorthand operator for this - ?.

To define a nullable integer you use one of the following statements:

Nullable<int> nullInt = new Nullable<int>();
Nullable<int>nullInt = new Nullable<int>(2); // Initialzie to 2


int? nullInt;
int? nullInt = 2;

Console.WriteLine(nullInt.HasValue ? nullInt.Value.ToString() : "NULL!");

Very handy!

Monday, August 13, 2007

Assigning an programs output to a variable in a batch file

This one took me longer than it should have to figure out. I had to write a batch file to integrate with our monitoring appliance - I can't really remember the last time I had to do this, but the number 6.22 comes to mind.

Anyways, the goal was to set a variable's value to the output (note: note the exit code) to the variable. You can't do this with the SET command so I had to result to using the FOR statement.

From a command prompt, it looks like:

for /f "tokens=*" %a in ('Program.exe "argument1"') do set __VARIABLENAME__=%a

Only one gotcha - in a batch file, you need to double up your % identifiers.

REM This is the start of the batch file
for /f "tokens=*" %%a in ('Program.exe "argument1"') do set __VARIABLENAME__=%%a
echo Result is %__VARIABLENAME__%
REM This is the end of the batch file

I'm glad thats over, back to C# for me!

Wednesday, August 08, 2007

System.Net.WebException from XmlDocument.Load

I had a really bizzare problem today - I was loading a local XML document via XmlDocument.Load and it was throwing a System.Net.WebException - The remote server returned an error: (503) Server Unavailable.

I was totally stumped, and then I hapened to open the XML document in question (it's actually an XHTML document), and I noticed that the first line was a DOCTYPE with a link to an external DTD.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

Sure enough, following the link gave me a 503 Server Unavailable error!

To resolve the problem I simply removed the DOCTYPE from the HTML document.

Tuesday, August 07, 2007

Choosing appropriate field lengths

For every project that I work on I seem to have the same argument with myself about field lengths (mainly when dealing with SQL) - just how long should they be? What is appropriate for first name, last name, and email address columns?

I used to start big, 100 characters for first/last name and 255 characters for email addressses. Recently I was revisiting my thinking of lengths so I decided to run some stats on the one of the larger DB's that I maintain. Here's what I found.

First Name: Max 27, Avg 6
Last Name: Max 27, Avg 6
Email: Max 76, Avg 22

The first thing that hit me was, geeze, a 76 character email address - that's a lot of typing! The second thing was that my column length allocations were on the really high (and unnecessary) end of the spectrum.

I always like to leave a bit of room because users always seem to do the unthinkable, so moving foward, my allocations look like:

First & Last name: 35
Email: 100

Since I mainly use variable length columns (varchar/nvarchar), storage is never really an issue, however when you start allocating 255 characters to each column it is easy to hit the table maximum (w/o BLOB's). Knowing that I can tone down my allocations gives me some re-assurance that I am less likely to reach the maximum when adding new columns down the road.

Thursday, August 02, 2007

Why don't things just work in Safari?!

When developing web applications, I primarily target Internet Explorer 6/7, and Firefox 1.5/2.0, but I do usually test things in Safari for layout purposes. While I rarley encouter any significant layout problems in Safari (other then its odd font rendering and super-thick anti-aliasing), the second I even think about a client-side component working I seemed to get slaped in the face, especially when it has something to do with callbacks.

I understand that Apple likes to do its own thing (ie: dock connector on the iPods - USB anyone?), but for god sakes, can't they support a similiar event model to another browser? Granted, Firefox and IE implement different event models too, but at least they are a bit more straight forward to abstract and work with.

And that is just with Safari 2.0...Don't even get me started about 3.0 (I'd have more to say if it didn't crash everytime I tried to run it on one of my workstations).

Whats most frustrating is that they've done a good job at supporting layout standards (they even pass the ACID2 test), but from a programming point of view, its a nightmare. And built in debugging tools? Useless - I get random null references on lines that don't exist and no errors when there actually is a problem.