Get a little VB in your C#

I’ve been doing some reading about differences between Visual Basic .NET and C# of late, and I’ve used both a bit now. I’m at a crossroads in my learning and am working to bridge the gap from forms- and console-based apps into the world of ASP .NET.

Frankly, I’m torn between VB and C# when attempting to pick a language to work with on my ASP .NET studies.
Here are a few articles I’ve been reviewing. Some old, some new.

Eventually, I think I settled on C# rather than VB. One thing I’m getting  a bit tired of is the excessive typing in VB. It seems that even simple statements can be quite lengthy.

For instance…

VB -

Dim strX as string = "123"

C# -

string strX = "123";

A small difference, but one that adds up over several hundred lines of code. Intellisense helps, but is really just a band-aid for the issue.

VB also lets you be sloppy and get away with it – anyone heard of “option strict”… albeit, C# can be too detail oriented for my liking – case sensitivity…*sigh*

My opinion, in general, follows with the idea that VB is simply an old language that is purely for “Microsoftish” stuff, while C# is built from the ground up to attract Java and C-based programmers. It seems everything I run into related to programming nowadays – Android SDK, iOS SDK, Apex for Salesforce.com, etc. is using something that is similar to C# syntax. I’m wishing I wouldn’t have stopped using C# after grad school now! Fortunately, I do think learning more about one topic teaches about another – and programming is no different. Just as learning Spanish deepens your understanding of English, exploring VB helped me appreciate more of the C# syntax and structure.

As a final note, one VBish thing that rocks is the My. namespace – and C# doesn’t have it by default! This namespace allows easy access to the file system, registry, audio, etc. Perhaps there are better ways to accomplish some of this stuff, but it’s nice to know it’s there. I’m sure I’ll be exploring alternatives.

To use statements like “My.Computer” in C#:

  1. Import a reference to “Microsoft.VisualBasic”
    • Right-click References in Solution Explorer and select Add Reference…
    • Look on the .NET tab and find “Microsoft.VisualBasic” – I’d sort by Component Name to make this easier
  2. Add a “using” statement so you don’t have to fully qualify the names all the time
    • “using Microsoft.VisualBasic.Devices;”   -> will give you traditional access (like “Computer objX = new Computer();”)
    • “using My = Microsoft.VisualBasic.Devices;”   -> will give you a VBish feel (like “My.Computer objX = new Computer();”)
  3. Refer to these two posts for more information, if desired…
  4. Enjoy your VB crutch!  :)

PS – VB did away with needing a “& _” for line breaks…maybe C# will dump the “;” at the end of each line sometime?

Leave a Reply