Section 1.1. Why Visual Basic .NET?


1.1. Why Visual Basic .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 Windowsand upon the release of which the ultimate success or failure of Windows would dependwere 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 being resized or a button control being clicked. Visual Basic's initial success was due to its ease of use, especially the simplicity of its 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 pre-.NET Visual Basic (see Example 1-2). While the former program is over two pages long, its Visual Basic counterpart takes only three lines of codeand two of them are provided automatically by the Visual Basic environment.

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, "Greetings" 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. Professional developers now had an additional language selection beyond the usual choices of C and C++.

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.

At about this same time, the character of programming in general changed dramatically. The rise of the Internet as an application platform meant that programmers needed to do more than write single-user, locally installed, standalone Windows applications. The increased prominence of distributed applications that assumed the presence of the Internet marked a huge change in programming focus. Visual Basic continued to be a great tool for implementing Windows desktop applications, and it was a reasonable choice for developing middle-tier components, but those strengths didn't translate easily into situations that required more direct interaction with the Web.

This disparity between Visual Basic's strengths and the new distributed and disconnected programming paradigm 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 more and more applications that ignored the Windows interface completely. When it came to the Internet, programmers were now using Visual Basic to write source code that would eventually be compiled into middle-tier components. Ironically, a programming environment whose real strength was its graphical character was now being used as a text editor, in very much the same way that the first generation of Windows programmers used text editors to create C source code for graphical 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 in VB. 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 the .NET-era Visual Basic offering as an entirely new language with little relationship to previous versions of Visual Basica dubious innovation foisted on the Visual Basic community by Microsoft in an attempt to sell a new version of its development products. However, that argument ignores one of the main reasons why Visual Basic, or any language, exists: to develop software applications in the most effective and efficient manner possible. The introduction of Visual Basic .NET was a logical and even necessary step forward in the development of Visual Basic as a premier programming language. .NET addresses the limitations of Visual Basic as a development language and brings it into the Internet age so that it can remain a major platform for developing applications of all kinds. Just as Visual Basic 1.0 offered a graphical interface that was suitable for Windows applications, the .NET flavors of Visual Basic and Visual Studio provide a graphical interface that is suitable for developing both desktop and web-based applications. No longer a glorified text editor, Visual Basic (built on the object-oriented foundation of .NET) can now take full advantage of the Internet as an application-development target and will continue to be a tool of choice for developing Windows applications and components.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net