September 2003 - Posts

Ignoring Errors? Not if they're an Exception!

Brad Abram points out a number of reasons why Exceptions are better than error codes. One that I liked in particular:

Return values can be easily ignored, and often are. Exceptions, on the other hand, take an active role in the flow of your code. This makes failures reported as exceptions difficult to ignore, and improves the robustness of code.
Posted by PSteele | with no comments

Top Blogger.

I've enjoyed every single one of Raymond Chen's posts. His blog is one of the most informative and interesting -- and they make great history lessons! Kudos to Raymond!
Posted by PSteele | with no comments

What a coincedence!

Just last night, I made a post about my journey into full-time C# use. This morning, I see Eric Gunnerson has mentioned a new magazine called "C# Pro Magazine". I headed over there to check it out and what do I see -- an article titled "5 C# Tips for VB Developers".

I read through the article and was somewhat disappointed. It certainly has a few pointers that will help those who have never done C/Java-style programming. But I have to comment on a few things mentioned in the article.

One key difference between the languages is converting data types. VB.NET is more forgiving when it comes to converting between data types implicitly. For example, VB.NET automatically casts an integer to a string:
Dim i As Integer = 2
Dim s As String
s = i

Uh -- only if you let it. With 'Option Strict On' and 'Option Explicit On' -- which should always be used -- this code is immediately highlighted in the IDE as a problem. And it won't compile either.

This example continues with:

But C# won't even allow this assignment to compile. The compiler yields this error:

Cannot implicitly convert type 'int' to 'string'

This error occurs because C# is a strongly typed language.

Every .NET language is, by nature of running under the CLR, a strongly typed language.

More on converting data types:

C# lacks the convenient data-type conversion functions VB .NET provides, such as CInt, CDate, and CType. These functions ultimately are too forgiving because they let you convert natively incompatible data types, which can lead to obscure bugs in your code.

And the divide operator in C# lets you divide by zero resulting an an Exception. Does that make it a bad thing? No, it means you (the programmer!) need to write code properly. The VB.NET conversion functions are there for backwards compatibility and for convenience. If they aren't the right tool for the job, don't use them.

On optional parameters:

VB .NET also uses method overloading under the hood whenever you use the Optional keyword in a method signature.

No, it doesn't. If you look at the IL for a VB.NET method defined with an optional parameter, there is no overloading going on. The optional parameter is marked with "[opt]". This is just one of those CLR features that is implemented in VB.NET but not in C#. Here's an example:

Private Sub foo(ByVal i As Integer, Optional ByVal x As String = "abc")
If you look at this with ILDASM, you'll see a single signature (no overloading):
.method private static void  foo(int32 i, [opt] string x) cil managed

What the author should have stressed was that optional parameters, while still available in VB.NET, should be avoided and method overloading should be used instead. Why? In the COM world, where many Visual Basic developers will be coming from, optional parameters that are not included in a function call have their default values determined at runtime. In VB.NET, this determination is done at compile time. This is very different and extermely important to realize.

Consider the function 'foo' (defined above) exists in some DLL that our main application references. In COM, if my main app calls foo as:

foo(23)
The optional "x" parameter will default to "abc" at the time the method is executing. In VB.NET, the same call will result in the compiler resolving the default value during compile and actually making the call as:
foo(23, "abc")

The "gotcha" comes when you re-code the DLL with a new optional value. In COM, no problem, just replace the DLL (forget about all that COM registration stuff for now). The main application can remain untouched. In .NET, you'll have to recompile the main application for it to get the new value. With method overloading, you won't have this issue.

All in all, not a bad article, but definitely shows some bias against VB.NET. Phrases like "you'll find the C# syntax to be more efficient" and "VB .NET syntax starts to feel wordy and cumbersome" are one man's opinion and do nothing to help a VB.NET developer become more familiar with C#.

Posted by PSteele | 5 comment(s)

Sharpening my skills.

I've been a VB programmer for a long time. Therefore, I took to VB.NET pretty easily. I can pound out code pretty quickly in VB.NET, and I'm familiar enough with C# syntax that I can read C# code fluently. But the past year at my current job has involved a little bit of Java coding. Curly braces and semi-colons don't scare me, although I'm not a big fan of case-sensitivity. I've done a little bit of C# coding here and there, but I mainly use VB.NET.

However, one of the things that I like doing is getting down into the details of what .NET is doing. I use ILDASM a lot as it's a great tool for just that. What I'm not crazy about is all the "extra stuff" that I see in VB.NET executables. Granted, I've been a big supporter of VB.NET for a long time and I think it's a great language. But I had a little ASP.NET project to do the other night so I made the commitment to do it in C# and take it for a real test-drive.

Well, I really liked it! It took a little getting used to, but after a couple of hours working in it, you really forget about the differences in language -- you're justing expressing your .NET intentions differently. While I can't say I'm a convert at this point, I'll probably start doing more and more in C#.

Posted by PSteele | 4 comment(s)

Keeping your Enums and Lookup tables in sync

Steve Smith has developed a unit test to make sure your .NET enums (used during coding) match up wth their corresponding lookup tables (used for RI checking). Nice safety net.

Now how about someone creating a VS.NET add-in along with a few custom attributes to generate a lookup table based on an Enum? :)

Posted by PSteele | 1 comment(s)

Converting GMT dates

I have a DB2 database giving me a date in GMT format and I need to convert it to my local timezone. The .NET TimeZone class makes this sooooo easy:

Dim gmtDate As DateTime = DateTime.Parse(...)
Dim localTime As DateTime = TimeZone.CurrentTimeZone.ToLocalTime(gmtDate)
Posted by PSteele | 3 comment(s)

Microsoft will *NOT* send you a critical update.

Most of you reading this post already know this, but Microsoft will not send you a personal email with a critical update. I've been flooded with about 20 of these today. They are very authentic looking -- they resemble the Microsoft home page, same layout, fonts, trademark and copyright notices, the works. But the attachment is just another virus/worm.

Let friends/relatives who may not be as technical as you know this is out there. Don't let them get bit. More than likely, you'll have to clean up the mess! :)

Posted by PSteele | 3 comment(s)

GANG Meeting

Had a great GANG meeting tonight. Bob Crosley from Wise Solutions discussed "Deployment nightmares: How poor software deployments destroy schedules, projects and your customer's computer". He also demo'd Wise for Visual Studio.NET and it looked slick! It integrates directly into the IDE as a new project type and has a ton of nice point-and-click options. It also supports some complex stuff like database installs, virtual directory creation, GAC install and even nGen on the client. Nice.

After the meeting I got a chance to meet (face-to-face) with new Microsoft Technical Evangelist Alex Lowe. Alex has a ton of great ideas for the Great Lakes Region and I look forward to a lot of good things from him!

Posted by PSteele | with no comments

Have you even done unit testing?

Roy challenges you to ask the developer next to you: Have you ever done unit testing?

Posted by PSteele | with no comments
More Posts Next page »