Over the weekend I've spent a significant amount of time working in Visual Studio 2005 with VB.net. The more I work with it, the more I like the IDE, and some of the cool things that I can do in it. This post, though, is going to focus on some of the differences between VBA and .Net that I'm playing with.
First thing to notice in importance is the existence of Option Strict. As VBA'ers, we're used to using Option Explicit (at least you should be), but Option Strict adds another dimension to your code. As I believe in following best practices, I am coding with both Option Explicit (OE) and Option Strict (OS) set to On.
It should also be noted that while I am quite proficient with VBA, I am certainly not an expert on VB.Net, and I am not trying to pass myself off as such. It's very likely that I will get some things wrong in this, and if I do, feel free to comment on the post. I'm doing this to learn, and blogging to share my experiences with those who have both gone before, and may choose to come after me on this road.
Okay, so here we go...
Early vs Late Binding
In VBA, we have the option of Early or Late binding. Generally, I develop using late binding, mainly because I work in an environment of mixed Office versions. There is nothing worse than having an Excel 2003 user open a file, have the references upgraded, and the 97 user can no longer get in. In VB.Net, with OS on, you must use Early binding.
In addition, OS demands very strict Type casting. I'm used to the following to create an instance of Word in VBA:
Visual Basic:
-
oWord = CreateObject("Word.Application")
This does not work in VB.Net though, as "Option Strict On disallows implicit conversions from 'Object' to 'Microsoft.Office.Interop.Word.Application'."
Instead, we need to use the following to explicitly cast the Type:
Visual Basic:
-
'At the top of the module:
-
-
Imports Word = Microsoft.Office.Interop.Word
-
-
'In your procedure
-
-
oWord = CType(CreateObject("Word.Application"), Word.Application)
Functions...
In VBA, we'd use our Right() function to return x number of characters from the right of a string. In VB.Net, we need to preface that with something more:
Visual Basic:
-
Strings.Right(text, chars)
Type Casting
Probably the biggest issue that I've run into is casting types. VB.Net (OS on) will NOT convert data from one type to another. This is evident in the binding example above, as well as many other things. Fortunately Option Strict kicks up little error messages to tell you what's up, and after a bit you can generally make some sense of how to fix them. Usually it involves a converting something to a type that we're used to... CStr, CBool, etc... What really threw me, though, was how to type data as an Object. The secret is the Imports statement... For example, look at the MessageBox constants (more on this below).
With a msgbox, you can't just use the constants like vbYesno anymore. You need to use Microsoft.Office.Core.MsgBoxStyle.YesNo. Now that's a bit of a pain, but there is an easier way. Thanks to Mike Rosenblum, who explained this to me in great detail, you can do this:
At the top of your module, insert the following statement:
Visual Basic:
-
Imports Microsoft.Office.Core
Now you can refer to that constant as:
MessageBox Constants
In addition to the issue with Type Casting above, the messagebox also changed a bit in that you don't add your options together in .Net as we did in VBA. Let's look at the differences:
Visual Basic:
-
'VBA version
-
-
Select Case MsgBox("Are you sure you want to do this?", _
-
vbYesNo + vbQuestion, "Are you sure?")
-
Case Is = vbYes
-
Confirm = True
-
Case Else
-
Confirm = False
-
End Select
-
-
'VB.Net Version
-
-
Select Case MsgBox("Are you sure you want to do this?", _
-
MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Are you sure?")
-
Case Is = vbYes
-
Confirm = True
-
Case Else
-
Confirm = False
-
End Select
Notice that it is an Or statement, not a +. This seems weird, but I have tested it, and it does work. Seems counter intuitive, I'll grant, but what are you going to do.
Final Word
Well, that's my observations so far on language differences. There's way more, obviously, and I'm sure I'll share more as time goes along.