Archive for the ‘Visual Basic’ Category

ASP.NET MVC & VB – Where are the examples

July 3, 2012 3 comments

I finally got around to teaching myself ASP.NET MVC (using 4.0 / razor / Entity Framework 4.1 / VS 2012 RC) and found myself in a bit of a bind today. Despite the fact that I am a VB.NET MVP I have been working in C# just so I can keep my chops up with the language. I never get the chance at work to use C# so I force myself to do all personal projects in C#. The short of it is that I love MVC 4 and especially the razor syntax. Very powerful, very flexible. I have been catching on quickly enough and have found ample examples on the web to do what I need, which is very comforting to know that there is a huge amount of support for what is still not the mainstream web technology for MS (yet!).

And then I started up a test project at work to show some colleagues. It was in VB.

The experience could not have been more night and day. Examples of razor code segments in VB on the Googlewebs is like finding a needle in a haystack. To clarify, I was searching for specific answers — if I just did a general search like “MVC4 VB.NET” then I would find basic tutorials easy enough. But I’m not looking for encompassing tutorials. I’ve already started getting into the details of implementation so I need to solve specific issues. Unfortunately that sort of content appears to be blanketly in C# and I have to say that this is disheartening to a professed VB-enthusiast. For instance, I was trying to find the syntax for declaring a WebGrid HTML helper in razor (recall that I am new to MVC) so whatever I had used back a few weeks ago was half-fading from memory. Not a problem though – I’ll just find a quick sample from some forum thread or article just to get the ball rolling – a mix of search terms that included “MVC3”, “MVC4”, “VB.NET”, “webgrid” and “razor” I eventually had to give up around my twelfth result and translate a C# example instead. Honestly, I would have done this much earlier but I was just amazed at the lack of VB coverage. I pride myself on finding things fast through search – even with technologies I’ve never even seen before. This however was debasing. (For the record is a good reference that has ample dual language examples.)

So what’s with the near absence of VB code samples anywhere – in MS documentation or in developer forums. I can only conclude that ASP.NET MVC 3/4 is not a technology widely adopted by VB.NET developers. I know there are plenty of VB.NET developers out there but I suspect that the perception that VB’ers are mainly stuck on Windows client applications (Winforms mostly) may have some truth to it. Windows development does constitute about 90% of the content in the MSDN Visual Basic General forum forum. It’s what I have done primarily for the last seven years. The VB language team at Microsoft, through the effort of achieving “language parity”, has done a stellar job of elevating the language to the position of true equal to C# in terms of power and flexibility (with a very few exceptions) with the onset of .NET 4.5. Its detractors have little more than perception and job market opportunities to point to for C# superiority. Unfortunately for VB those things count for a lot. Too bad though. Vb.NET is a great language that, together with the power of the Visual Studio IDE, can offer blinding fast speed for the experienced developer as well as clear and simple readability. As evidence I offer case-insensitivity, auto-completion, code prettification and the lack of superfluous curly braces.

One last gripe with my brief exploration of MVC 4 with VB.NET. When trying to create an out of the box “basic” solution on my home machine (VS 2012 RC) I received the following error from the IDE at the tail end of generating the solution:

MVC 4 Error

I believe this may be behind my inability to access the WebGrid class in code on the razor form – thank goodness my real project is done in C#. I’ll have to assume this is some issue with VS 2012 RC (since the project I did at work was with VS 2010 SP1) but this whole business with VB has got me paranoid now. The second time I started a VB MVC 4 project at work using VS 2010 I noticed another issue. With VB defaults set to Option Strict On (which is recommended) some of the auto-generated code fails to compile because of loose casting. My guess is that the generation code was written for C# first and then ported to VB CodeDOM parser without a proper review of project context. This just suggests to me that VB was an afterthought. Kinda pisses me off, really.

Categories: MVC, Uncategorized, Visual Basic Tags: , ,

Why the default form instance does more harm than good

February 2, 2012 1 comment

First things first. The default form instance is a feature of VB.NET for versions 2.0 and up, applicable only to WinForms projects, and provides a forms management functionality that is in keeping with the code “feel” of VB6, the predecessor of VB.NET. Functionally, this feature allows for the programmer to use the name of a form to directly reference instance members on that form’s class (i.e. controls, properties). This feature made sense when working with forms in VB6

This of course flies in the face of object oriented design (and the .NET framework’s design patterns for that matter) which leaves the onus on the developer to be in control of his own object instances. In essence it introduces a non-OO principle into an OO language — and this is the starting point of my argument that in net sum, this is a harmful feature… at least in the way it is presented in the default WinForms Visual Studio template (I’ll clarify that later).
Read more…

When to use custom exceptions

June 12, 2011 Leave a comment

Have you ever found yourself in a situation where there seems to be no exception type that is a fit for the exception you want to throw? In smaller, stand-alone projects it is easy to just ignore the problem and use a plain old, generic Exception. After all, you’re the only one who’s going to see it, right? For larger, more complex projects or for class libraries that will be consumed by other assemblies that may not be written by you it can be useful or necessary to throw exceptions that have targeted meaning. The solution is to make your own, and the .NET framework has provided us the means. Read more…

Parsing large numbers to words (english)

May 8, 2011 1 comment

I’ve occasionally seen a request for this functionality pop up in the forums over the years. A developer is given the task to write out in words a number the way it may be spoken by a person. Actually, a more common use for this functionality is with bank checks where we write out the amount in words. So $1,234.56 would be written out as One Thousand Two Hundred Thirty Four Dollars and Fifty Six Cents. It’s amusing that the people who search for this functionality in code are surprised and even annoyed that the .NET framework does not have this built in… as if the framework does not have enough stuff already, right? A while back I came up with a solution for this based on a forum request (see the revised code at the bottom). I thought I would share this here again as a separate blog post. Actually, I decided to upgrade the code a tad just for the sake of never leaving good enough alone 😉
Read more…

The null object pattern in use

April 16, 2011 Leave a comment

What is the null object pattern and why should you care? Well, if you’re a masochist who likes to add superfluous validation code to any give project or if you are an obsesive compulsive about adding such superfluous code… then I suppose you wouldn’t care. However, for the rest of us this can be a quite useful design pattern that can lead to simpler code that is more maintaianable and better testable. That covers the why, so here is the what in a nutshell.

Design patterns provide an architectural or algorithmic set of steps or procedures to solve specific programming design issues. This design pattern addreses the common issue with .NET development where an instance of a class, or a reference object, is tested for having a null reference before using the object in code. Why would you do this in the first place? Consider that if you attempt to access a method or a property on a reference object that is null (or Nothing in VB.NET) you will encounter a nasty NullReferenceException. This is not the type of error you would want to introduce to your user, but even if you are savvy enough to catch such an error before it crashes your application you are still posed with the issue of restoring the normal flow of operation for that application in light of this unexpected error. Read more…