1. Home
  2. Computing & Technology
  3. Visual Basic

Procedures in VB.NET
The key elements of a procedure

By Dan Mabbutt, About.com

Attributes

The first thing that might be present in a procedure definition is an attribute. Attributes contain meta information that apply to the procedure. This is a way to provide information that both the compiler and the executable runtime can access and use. Attributes look like this in code:

<attributename()> Sub TestSubroutine( ....

If you open the hidden file Form1.Designer.vb (Click the Show All Files button at the top of the Solution Explorer window to see the file.) in a standard Windows application, you can see a lot of examples in the generated code provided by Visual Studio. For more, I recommend the article you can find on this site: Attributes in VB .NET.

Access Levels

The next thing that can appear in the syntax of most procedures is the access level. This can be one of:

  • Public
  • Protected
  • Friend
  • Private
  • Protected Friend

Access levels determine what code has the ability to read or write to the procedure. If you have a subroutine named Validate_Password, you might want to make sure that it can only be called in specific ways. A complete description of access levels isn't what this article is about, so I'll refer you to MSDN at Microsoft for more information.

Procedure Modifiers

Procedure modifiers are also optional, and they have everything to do with managing how identically named procedures are managed in the new Object Oriented VB.NET. There are seven possibilities:

  • Overloads
  • Overrides
  • Overridable
  • NotOverridable
  • MustOverride
  • MustOverride Overrides
  • NotOverridable Overrides

The OOP principal of polymorphism - literally, "many forms" - is handled by these keywords. This means that you can have two procedures with the same name that do completely different things:

Overloads Sub TestSubroutine(ByVal TestParameter As String)
   ' Code unique to this subroutine
End Sub

and

Overloads Sub TestSubroutine(ByVal TestParameter As Integer)
   ' Different code unique to this subroutine
End Sub

In the case of Overloads, VB.NET will figure out which one to call as long as they have a different signature. This is primarily the list of parameters passed to the procedure. (The signature also includes access level, passing mechanism, and return type.)

If you use Overrides instead, then VB.NET will look for another procedure that has exactly the same signature and replace it with this one.

Shared

The optional Shared keyword determines whether there is a unique instance of a procedure or a common instance that all the code that calls the procedure uses. I covered this keyword in the article Shared Members and Instance Members in VB.NET.

Shadows

The meaning of Shadows in Visual Basic is taken from the idea that one thing can "cast a shadow over" another and effectively hide it. So if an element shadows another, then the other isn't "seen" by the compiler and the first one is used. When applied to procedures, this is a lot like "Overrides" but Shadows has stronger rules that more clearly give the shadowing procedure more precedence. This is a subtle concept, so if you need to use this keyword, consult MSDN for the details.

Implements

To tie systems together, classes often just have the declarations for properties, procedures, and events and not the code. This is called an interface. Think of it as being like a baseball or football team. Everybody wears the same uniform and has a common purpose, but different team members have different roles. A procedure that contains the code for an interface must use the Implements keyword:

Public Sub TestSubroutine() Implements _
   ITestInterfaceClass.TestSubroutine

Handles

When an event is raised in VB.NET, like the Click event when a Button is clicked, the procedure that is called is identified by the Handles clause. And the same procedure can handles different events. The name of the procedure isn't really important. Here's an example taken from my article about programming a ten-key calculator where the same subroutine handles 11 different Click events.

   Private Sub keyClick( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles key1.Click, key2.Click, key3.Click, _
         key4.Click, key5.Click, key6.Click, _
         key7.Click, key8.Click, key9.Click, _
         key0.Click, keydecimal.Click

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Procedures in VB.NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.