O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Introduction

Since its introduction in 1991, Microsoft Visual Basic has enjoyed unprecedented success. In fact, in slightly more than a decade, it has become the world’s most widely used programming language, with an installed base of somewhere between three and five million developers (depending on the particular source you use and whether the estimate includes only the retail versions of the Visual Basic product or the hosted version of Visual Basic for Applications (VBA) as well).

The reason for this success is twofold. First, Visual Basic has excelled as a rapid application development (RAD) environment for corporate and commercial applications. Second, Visual Basic offers a programming language and development environment noted for its simplicity and ease of use, making it an extremely attractive choice for those new to programming.

With the release of its new .NET platform, Microsoft also released a new version of the Visual Basic language, Visual Basic .NET. VB.NET is a from-the-ground-up rewrite of Visual Basic that not only adds a number of new features, but also differs significantly from previous versions of Visual Basic. From a high-level view, two of these differences are especially noteworthy:

  • Until the release of VB.NET, Microsoft focused on creating a unified version of VBA, the language engine used in Visual Basic, which could serve as a “universal batch language” for Windows and Windows applications. With Version 6 of Visual Basic, this goal was largely successful: VB 6.0 featured VBA 6.0, the same language engine that drives the individual applications in the Microsoft Office 2000 suite, Microsoft Project, Microsoft FrontPage, Microsoft Visio, and a host of popular third-party applications such as AutoDesk’s AutoCAD and Corel’s WordPerfect Office 2000. With the release of VB.NET, this emphasis on a unified programming language has, for the moment at least, faded into the background, as the hosted version of Visual Basic continues to be VBA rather than VB.NET.

  • Since Version 4, Visual Basic had increasingly been used as a kind of “glue language” to access COM components and their object models, such as ActiveX Data Objects (ADO), Collaborative Data Objects (CDO), or the Outlook object model. Although VB.NET supports COM for reasons of backward compatibility, VB.NET is designed primarily to work with the .NET Framework rather than with COM.

You may be wondering why Microsoft would totally redesign a programming language and development environment that is so wildly successful. As we shall see, there is some method to this madness.

Why VB.NET?

When Visual Basic was introduced in 1991, Windows 3.0 was a fairly new operating system in need of application and utility software. Although Windows 3.0 itself had proven successful, the graphical applications that offered native support for Windows — and upon whose release the ultimate success or failure of Windows would depend — were slow in coming. The major problem was that C and C++ programmers, who had produced the majority of applications for the MS-DOS operating system, were faced with a substantial learning curve in writing Windows applications and adapting to Windows’ event-driven programming model.

The introduction of Visual Basic immediately addressed this problem by offering a programming model that was thoroughly consistent with Windows’ graphical nature. Although Windows marked a radical change in the way programs were written, C and C++ programmers continued to produce code as they always had: a text editor was used to write source code, the source code was compiled into an executable, and the executable was finally run under Windows. Visual Basic programmers, on the other hand, worked in a programming environment that its critics derisively labeled a “drawing program.” Visual Basic automatically created a form (or window) whenever the developer began a new project. The developer would then “draw” the user interface by dragging and dropping controls from a toolbox onto the form. Finally, the developer would write code snippets that responded to particular events (such as the window loading or the window being resized). In other words, Visual Basic’s initial success was due to its ease of use, which in turn reflected that Visual Basic offered a graphical programming environment that was entirely consistent with the graphical character of Windows itself.

To get some sense of the revolutionary character of Visual Basic, it is instructive to compare a simple “Hello World” program for Windows 3.0 written in C (see Example 1-1) with one written in Visual Basic (see Example 1-2). While the former program is over two pages long, its Visual Basic counterpart takes only three lines of code — and two of them are provided automatically by the Visual Basic environment itself.

Example 1-1. “Hello World” in C

// "Hello World" example
//
// The user clicks a command button, and a "Hello World"
// message box appears.
#include <windows.h> 

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
   {
   static char szAppName[] = "SayHello" ;
   HWND hwnd ;
   MSG msg ;
   WNDCLASSEX wndclass ;

   wndclass.cbSize        = sizeof (wndclass) ;
   wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
   wndclass.lpfnWndProc   = WndProc ;
   wndclass.cbClsExtra    = 0 ;
   wndclass.cbWndExtra    = 0 ;
   wndclass.hInstance     = hInstance ;
   wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION) ;
   wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW) ;
   wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH) ;
   wndclass.lpszMenuName  = NULL ;
   wndclass.lpszClassName = szAppName ;
   wndclass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION) ;
  
   RegisterClassEx(&wndclass) ;
   
   hwnd = CreateWindow(szAppName, "Hello World",
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        NULL, NULL, hInstance, NULL) ;

   ShowWindow(hwnd, iCmdShow) ;
   UpdateWindow(hwnd) ;

   while (GetMessage(&msg, NULL, 0, 0))
      {
      TranslateMessage(&msg) ;
      DispatchMessage(&msg) ;
      }
   return msg.wParam ;
   }

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, 
                         LPARAM lParam)
   {
   int wNotifyCode ;
   HWND hwndCtl ;
   static HWND  hwndButton ;
   static RECT  rect ;
   static int   cxChar, cyChar ;
   HDC          hdc ;
   PAINTSTRUCT  ps ;
   TEXTMETRIC   tm ;

   switch (iMsg)
      {
      case WM_CREATE :
         hdc = GetDC(hwnd) ;
         SelectObject(hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
         GetTextMetrics(hdc, &tm) ;
         cxChar = tm.tmAveCharWidth ;
         cyChar = tm.tmHeight + tm.tmExternalLeading ;
         ReleaseDC(hwnd, hdc) ;
         GetClientRect( hwnd, &rect ) ;

         hwndButton = CreateWindow("BUTTON", "&Say Hello", 
                      WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                      (rect.right-rect.left)/20*9,
                      (rect.bottom-rect.top)/10*4, 
                      14 * cxChar, 3 * cyChar,
                      (HWND) hwnd, 1,
                      ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;

         return 0 ;

      case WM_SIZE :
         rect.left   = 24 * cxChar ;
         rect.top    =  2 * cyChar ;
         rect.right  = LOWORD (lParam) ;
         rect.bottom = HIWORD (lParam) ;
         return 0 ;

      case WM_PAINT :
         InvalidateRect(hwnd, &rect, TRUE) ;
          
          hdc = BeginPaint(hwnd, &ps) ;
          EndPaint(hwnd, &ps) ;
          return 0 ;

      case WM_DRAWITEM :
      case WM_COMMAND :
         wNotifyCode = HIWORD(wParam) ;
         hwndCtl = (HWND) lParam ;
          
          if ((hwndCtl == hwndButton) && (wNotifyCode == BN_CLICKED))
             MessageBox(hwnd, "Hello, World!", "Greetings", MB_OK) ; 

          ValidateRect(hwnd, &rect) ;

          break ;

      case WM_DESTROY :
         PostQuitMessage (0) ;
         return 0 ;
      }
   return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
   }

Example 1-2. “Hello World” in Visual Basic

Private Sub Command1_Click(  )

MsgBox "Hello, World", vbOKOnly Or vbExclamation, "Hi!"

End Sub

While Version 1.0 of Visual Basic was relatively underpowered, Microsoft displayed a firm commitment to Visual Basic and worked very hard to increase its power and flexibility with each new release. By the time Version 3.0 was released, Visual Basic offered a programming paradigm that was completely intuitive, making it easy for novice programmers to get started and produce simple applications very quickly. At the same time, particularly through its ability to access the Windows Application Programming Interface (API) and through its support for add-on controls, Visual Basic had become a programming tool capable of creating applications of considerable sophistication and complexity.

Like VB.NET, Visual Basic Version 4.0, which was released in 1995 to support Microsoft’s 32-bit family of operating systems, was a complete rewrite of Visual Basic. It featured limited support for object-oriented programming in the form of class modules (CLS files) and the ability to generate not only Windows executables, but ActiveX DLLs (also known as COM components) as well.

In the periods shortly before and after the release of VB 4, the character of programming changed dramatically. The rise of the Internet as an application platform meant that standalone Windows applications were becoming less and less necessary. The increased prominence of distributed applications that assumed the presence of the Internet marked another change in programming paradigms. Yet Visual Basic’s real strength remained as it always had been: a great platform for developing standalone Windows applications.

This disparity between Visual Basic’s strengths and the prevailing programming paradigm, which emphasized distributed applications and the Internet, created something of a contradiction. On the one hand, Visual Basic excelled at graphically depicting the Windows interface. On the other hand, developers were creating fewer and fewer Windows interfaces. Instead, they were now using Visual Basic primarily to write source code that would eventually be compiled into middle-tier components. Ironically, a programming environment whose real strength and point of departure was its graphical character was now being used as a text editor, in very much the same way the first generation of Windows programmers used text editors to create C source code for Windows applications.

Moreover, as the popularity of the Internet grew, it became clearer that Visual Basic was not a particularly good platform for developing Internet applications. With VB 6, Microsoft introduced Web Classes as the preferred technology for Internet application development. Yet, the metaphor presented by Web Classes (which focused on separating a web application’s presentation from its programmatic functionality) was confusing to developers, and, as a result, Web Classes never became popular. While VB remained critically important for developing middle-tier components for distributed applications, both it and the Visual Basic community that grew up around it remained strangely isolated from the Internet as an application platform.

Numerous detractors have labeled VB.NET as an entirely new language with little relationship to previous versions of Visual Basic — a dubious innovation foisted on the Visual Basic community by Microsoft in an attempt to sell a new version of its development products. However, we don’t agree. Instead, we view the introduction of VB.NET as a logical and even necessary step forward in the development of Visual Basic as a premier programming language. The goal of VB.NET is to address the limitations of Visual Basic as a development environment and bring it into the Internet age so that it can remain the major platform for developing applications of all kinds. Very much like Visual Basic 1.0 offered a graphical interface that was suitable for Windows applications, VB.NET and Visual Studio .NET aim to provide a graphical interface that is suitable for developing web applications and for taking full advantage of the Internet as an application-development platform, as well as for developing Windows applications and components.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required