Technology Microsoft Software & solutions

How References and Namespaces Work Together in VB.NET

Getting back to the complaint from About Visual Basic readers, the first step in solving the problem is to use the Add ... button to add a reference to Microsoft.VisualBasic.Compatibility library. Once this is done, we can move on to namespaces. For this, we can use the Imported Namespaces listbox in Project Designer. (Or not. Once the reference is added to the project, it's possible to handle it all in code from that point on.)

In .NET, library are usually organized using namespaces. You can think of these as the "real" object libraries. References can be thought of as just the box they come in. Here's the "official" description from Microsoft:

Namespaces organize the objects defined in an assembly. Assemblies can contain multiple namespaces, which can in turn contain other namespaces. Namespaces prevent ambiguity and simplify references when using large groups of objects such as class libraries.

After you add the Microsoft.VisualBasic.Compatibility library to your project, two new entries will be added, but not selected, to the Imported Namespaces listbox in Project Designer. Scroll to the bottom and you'll see:
  • Microsoft.VisualBasic.Compatibility
  • Microsoft.VisualBasic.Compatibility.VB6

Although the "VB6" namespace is the only entry in the first one, it does illustrate the point that a class library is actually a tree structured container.

The concept of "importing" simply means that a namespace at a specific path can be searched automatically by Visual Studio or the Visual Basic compiler.

Strictly speaking, the "Imports" keyword isn't a Visual Basic statement. It's a compiler option. That's why it has to be placed before the other statements in your source code. All the choices that are available to you are listed in the Imported Namespaces listbox. When you click the checkbox to select one, then you can simply use the methods and properties in that namespace directly in your program code. (A Quick Tip all about Imports can be found here.)

Using the Microsoft.VisualBasic.Compatibility.VB6 namespace method to convert VB6 twips into VB.NET pixels as an example, all of these code blocks are equivalent:

Imports Microsoft Public Class Form1Private Sub Form1_Load( ... VisualBasic.Compatibility.VB6.TwipsToPixelsX(100)End Sub End Class Imports Microsoft.VisualBasic Public Class Form1Private Sub Form1_Load( ... Compatibility.VB6.TwipsToPixelsX(100)End Sub End Class Imports Microsoft.VisualBasic.Compatibility Public Class Form1Private Sub Form1_Load( ... VB6.TwipsToPixelsX(100)End Sub End Class Imports Microsoft.VisualBasic.Compatibility.VB6 Public Class Form1Private Sub Form1_Load( ... TwipsToPixelsX(100)End Sub End Class
Or, if the Microsoft.VisualBasic.Compatibility.VB6 namespace is checked in the Imported Namespaces listbox, you can just code this:

TwipsToPixelsX(100)

(Note: VB.NET upgrades the way screen forms are measured from the virtually unknown twips to the almost universal pixel.)

Related posts "Technology : Microsoft Software & solutions"

How to Choose a Picture for a User Account and Start Menu in Windows Vista

Microsoft

How to Add Tracfone to the Start Menu

Microsoft

How to Stop the Reboot on the Blue Screen

Microsoft

How Do I Tell What Version of Windows XP I Am Using?

Microsoft

How to Make a Boot CD for Windows Vista

Microsoft

How to Move a Windows XP Installation

Microsoft

How to Install Ubuntu in VirtualBox

Microsoft

How to Fix a Problem With a System Idle Process

Microsoft

How to Add a VLK to Windows XP Home

Microsoft

Leave a Comment