tags:

views:

732

answers:

8

So as a direct result of this global financial hoohar I'm going to start a new job as a VB.net developer tomorrow. Up to this point I've been developing in C# (bit of java, vb6, sql, tibco, etc. here and there)

So the question is this, what are the gotchas to look out for and does anyone have any good advice on writing good vb.net code?

(Any other advice on coping with a salary / prospects drop welcome but not essential ;-))


Just a quick update, company seems really good, current code base appears to be of a very high quality. Am starting to adjust to the VB way of doing things (can’t stop myself adding semicolons everywhere though!). Thanks again for the helpful advice everyone.

+3  A: 

VB.Net is .Net language, its the same as C# but with vb flavor. I faced the same case as you from 4 years ago, what i did is just writing with vb.net, i was afraid but when i started writing i found that i am very good, its the same as C#, just in the first few lines you will write ";" and "{}" but after few minutes, you will feel its normal.

So my advice don't feel that you will write VB.Net, you will write .Net but with new style.

May be you will need to know the very easy parts: How to write a function How to Declare a variable How to create an event

And sure there are more differences between C# and VB.Net than just the syntax, but you will got them very fast your self.

Also a few converters can help you coding faster:

Convert VB.Net to C#, C# to VB.Net Tools

Amr ElGarhy
+2  A: 

Check out this link; I believe it's pretty much the definitive guide: http://msmvps.com/blogs/kathleen/archive/2008/07/25/what-a-c-coder-should-know-before-they-write-vb-updated.aspx

Greg Beech
+2  A: 

When I made a similar switch I found this little side by side comparison particularly useful: http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

Cargowire
A: 

I found this reference handy when I was switching from VB to C#, I expect it would be just as helpful the other way around. So that might help with the syntactic piece of the transition you're headed through. Not the hardest part of the transition for sure, but hopefully it can help.

Timothy Carter
+7  A: 

Just one little detail as caveat. If you declare an array in VB.NET, the number between the brackets means the upper limit, not the count of elements:

Dim monthNames(11) As String
Console.Write(monthNames.Count)

Output is "12".

not

Dim monthNames(12) as String
Console.Write(monthNames.Count)

Output is "13".


And I recommend you read this blog post written by Kathleen Dollard.

What a C# Coder Should Know Before They Write VB

Her first advice is:

1) Get over the respect thing or quit before you start. VB.NET is a great language.

splattne
You can change this to the default C# (and almost any other language) behavior by setting the Option Base 0 option.
Tomas Lycken
`Option Base 0` is the default. You can make an array declaration slightly more readable by saying `Dim monthNames(0 to 11)`, which clearly states the bounds.
jleedev
+2  A: 

A special notice should be given to the My namespace: It is somehow missing from the C# nomenclature, so coming from there you might miss this important tool.

The My namespace in Visual Basic exposes properties and methods that enable you to easily take advantage of the power of the .NET Framework. The My namespace simplifies common programming problems, often reducing a difficult task to a single line of code. Additionally, the My namespace is fully extensible so that you can customize the behavior of My and add new services to its hierarchy to adapt to specific application needs.

The three central My objects that provide access to information and commonly used functionality are My.Application Object, My.Computer Object, and My.User Object. You can use these objects to access information that is related to the current application, the computer that the application is installed on, or the current user of the application, respectively.

An example from Performing Tasks with My.Application, My.Computer, and My.User:

' Displays a message box that shows the full command line for the
' application.
Dim args As String = ""
For Each arg As String In My.Application.CommandLineArgs
    args &= arg & " "
Next
MsgBox(args)
gimel
`Dim args = String.Join(…)` does the job just as well.
Konrad Rudolph
Example taken verbatim from man page. The point is "My.Application.CommandLineArgs", anyway.
gimel
+15  A: 

Option Strict

The most important thing to do in VB, in my (absolutely not humble) opinion is to use Option Strict On at all times (except, on a per-file basis, when non-strict typing makes sense, e.g. because you use PIA to interoperate with MS Office) and to enable it in the VS options.

Option Strict On, together with Option Explicit On, gives roughly the same behaviour as C#. Switched off, it removes a lot of type checks at compile-time and allows spurious, unnecessary and hard-to-debug implicit conversions between completely unrelated types.

Option Strict Off makes sense when working with COM API. Option Explicit Off never makes sense. It's stupid (and mainly there for VB6 compatibility).

Comparison: = versus Is

Another thing to look out for: equality vs. reference testing. In C#, you use == for both. In VB, you've got distinct operators:

Dim StringA = "Hello"
Dim StringB = Console.ReadLine()
Dim EqualContent = StringA = StringB
Dim EqualRefs = StringA Is StringB

Now depending on the user input, EqualContent may be True; EqualRefs will always be False. Beware that Is here is semantically equivalent to the following C# code (which nobody ever writes, usually):

var equalRefs = object.ReferenceEquals(stringA, stringB);

I actually think this is an advantage in VB over C#, but one rarely needed. The opposite of Is is IsNot. Another thing to pay attention to here is that the string comparison via the = operator actually calls a VB runtime method: Microsoft.VisualBasic.CompilerServices.Operators.CompareString.

This takes into account several other settings, especially the Option Compare setting which may be Binary (default, behaviour like in C#) or Text (case-insensitive comparison).

CType versus DirectCast and TryCast

The VB runtime is called in some other cases as well, one of them notably CType which is a general-purpose conversion operator in VB. I tend to avoid using the operator and I strongly advise anyone doing the same, in favour of other, more explicit conversions. The reasons for this is that CType tries several semantically very different conversions, when applied. This makes it hard to track what exactly is going on in the code, potentially introducing typing errors.

For one thing, CType allows parsing of strings for numbers. This is a concept better expressed through the NumberType.Parse operation, as in C#.

Instead of CType, I advise usage of DirectCast which is the equivalent of the C# cast, or TryCast which is the same as C#'s as conversion.

Another gotcha. When checking whether an object x has a certain type T, the following syntax has to be used:

If TypeOf x Is T Then …

Notice that this doesn't invoke the normal reference comparison operator Is. Rather, it uses an own operator construct TypeOf … Is …. You cannot write TypeOf … IsNot …, though. This is probably a bug in the specs.

Misc. …

There are a lot more differences, some useful (e.g. the differences in the Select Case statement) and some less (e.g. the Like operator for basic wildcard matching … just use regular expressions instead).

Some other questions relating to this:

Konrad Rudolph
+1 for a great post. I'd like to see Option Base 0 in there too, though. 1-based arrays is a huge gotcha for almost any non-VB programmer.
Tomas Lycken
...but of course I mean 1-based indices. Sigh...
Tomas Lycken
@Tomas: I'm not sure what you mean: we're talking about VB.NET here where `Option Base 0` is on by default – VB.NET does in fact *not* support any other base any more.
Konrad Rudolph
A: 

+1 for option strict.

I went from VB.Net to C# and was initially a little scared I wouldn't be able to pick it up. If you know the .Net framework you will be fine, just keep google handy for any syntax you are unsure of.

Good luck.

B