March 3, 2021
Hot Topics:

VB.NET Uncovered: Big Changes

  • By Karl Moore
  • Send Email »
  • More Articles »

Let's look at writing a little of our own VB.NET code now.

To get started, first switch back to Design view.

  • Click View, Designer

I've brought you back here so you can see that writing code for, say, a Button is just as easy in VB.NET as it was in VB6. Erm, if VB6 had Buttons. Which it didn't. But if it did, it wouldn't be difficult. Oh boy.

  • Double-click on Button1

Dij` vu? Hello!

Your cursor should currently be flashing in a code window between the following:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)End Sub

Now whatever code sits between these two lines is fired whenever Command1 is clicked.

Top Tip: To respond to other Button events, or the events of your Form, select the Class and Method names using the combo box just above the code window. You can also fire off a separate sub in response to an event, using the Handles keyword to see the help for more information.

Let's add some code now...

  • Add the following code to the Click event of Button1:
MessageBox.Show("Hey... hehe... you clicked me and it tickles!")
  • Hit F5 and test your code

OK, so maybe it's not quite the technological revolution Bill Gates promised you. But we're getting there.

Top Tip: As we mentioned in the firstinstalment, MessageBox.Show is the new VB.NET method of displaying a messagebox. It replaces the old MsgBox statement.

Now, you may have heard about these things called 'Namespaces' in VB.NET.

Namespaces are a cool way of organising functionality. They're essentially a bunch of classes built into the entire .NET Framework - and every .NET programming language has access to these.

For example, there are dozens of functions we VB developers are used to in the Microsoft.VisualBasic namespace.

To see what I'm talking about, go to the code window and type Microsoft followed by the period (full-stop). You should be presented with two options - Win32 and VisualBasic. If you type VisualBasic and press the period once more, you'll get a list of all the functions we coders are acquainted with.

So, if you wanted to use the StrReverse function we know from our VB6 days, you could execute a chunk of code like this:

MessageBox.Show(Microsoft.VisualBasic.StrReverse("Karl Moore"))

Eeeek! I hear you scream. But that's so much longer than just using the ol' StrReverse keyword we previously had. And I agree.

First off, let me explain that this 'organising' of classes has a number of advantages. For a start, uhm, it's organised! You also avoid naming conflicts and gain a common ground throughout all languages.

However by default, all commands in the Microsoft.VisualBasic 'namespace' are 'imported' by default, meaning you only have to use their name and can skip the Microsoft.VisualBasic prefix.

Top Tip: A 'namespace' is imported by either specifying 'Imports Whatever.Whatever' within your form or component - or by specifying Imports in the Project Properties.

Another Top Tip: Don't get confused when people talk about 'Imports'. It's just a way to save you typing all those lengthy prefixes. In fact, it's very much like the 'With' statement you already know and love. You do love it, don't you?

So if we don't need to use the Microsoft.VisualBasic 'namespace' - why did I even bother to mention it? Uhm, good point. In fact, you could do a lot of your programming without even realising you'reusing namespaces.

However it's good to know, after all, there's isn't only the Microsoft.VisualBasic namespace. There's the System namespace too ... and that beast has about a zillion commands under it. Try typing it in and you'll see what I mean.

So - sometimes you might want to use the functionality from a namespace that isn't imported by default. And then you'll need to either 'import' it or reference it using the full 'namespace path'.

When would you need to do this? Gee, don't look at me.

Perhaps if you're working with graphics, you'll be interested in the System.Drawing.Graphics namespace. Or if you're manipulating databases, you'll want to peruse System.Data.ADO. Or maybe if you're looking to preserve old Visual Basic code, you'll want to check out the Microsoft.VisualBasic.Compatibility.VB6 namespace.

Confused? You will be.

Let's recap. Namespaces are a naming scheme to help organise the various classes available to our application. And you can access the commands of some namespaces that are 'imported' by default without having to type all those whopping great prefixes.

Don't forget, new namespaces will step onto the scene. And although we've covered some of the most important right here on this page, your 'VB professionalism' may start to rest on exactly how much you know about the existing namespaces.

So, you now know about namespaces. And in my mind, that is the biggest change in VB.NET.

It's a weird concept. It's difficult to understand. It's a weird concept. Did I already say that? Well, I wanted to say it again. It's weird. Oh, and again.

But if that has 'clicked', then hurrah! You can now do virtually everything you could back in the ol' VB6 days! And you're well on your way to becoming a VB.NET guru.

Top Tip: At this stage, you might want to compile your test .EXE application, if only for the experience. Use the Build menu to do this - note that your .EXE will be placed in the BIN subfolder of your project.

Next up, I'm going to run through what I deem to be the major changes to actual coding syntax. It won't make bedtime reading, but will give you a few pointers as to what to watch out for. So let's continue...

Page 5 of 7

This article was originally published on October 21, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date