An Overview of Project Templates
You can choose from several kinds of project templates when creating a new Visual Basic .NET project. This section introduces each briefly and indicates where you can find examples of these kinds of projects in this book.
The Windows Application Project Template is the basic template for Windows applications. This project is created containing a single blank WinForm. Examples of Windows applications are provided throughout this book. Many are small applications demonstrating isolated topics. For more in-depth material on building Windows applications, see Chapter 15, "Using Windows Forms," and Chapter 16, "Designing User Interfaces."
The EmptyForm.sln described earlier in the chapter illustrates what the Studio IDE will provide for you when you begin a Windows Application project.
A class library project creates a DLL containing .NET classes. Class libraries in Visual Studio .NET are created similarly to COM libraries in Visual Basic 6 but aren't COM libraries. Rather, a class library in .NET is an assembly designed to work with the .NET Framework.
Set the project name when you create a new class library project. This ensures that the project, root assembly, and namespace are all set correctly.
Refer to the solution ClassLibraryDemo, which contains a trivial Windows application and a class library. The class library is defined in the namespace ClassLibraryDemo, which can be determined from the general view of the Common Properties in the Property Page dialog box. The TestClassLibraryDemo Windows application contains a reference to the class library. From there we can instantiate objects defined in the class library. (Refer to Chapter 7, "Creating Classes," for an example of a class library.)
Creating a Class Library
The basic steps for creating a class library begin with selecting the Class Library Project Template from the New Project dialog box. Implement classes in the class library as you would anywhere else. Build the library. When you add a reference to it from another application, you can create instances of classes in the library as if they were classes defined in the application.
Adding a Reference to and Using a Class Library
These are the steps you will need to take when using a class library:
Your application now has a reference to that class library. All these steps are very similar to the process in Visual Basic 6.
Having the reference to the library, you can create instances of classes in the library with the Dim statement and the fully qualified namespace and class path , or you can import the namespace and then refer directly to the class. Here is an example of both:
The imports statement is placed at the top of the source file. When you want to create a class from the ClassLibraryDemo, the following example works:
Dim MyVar As New LibraryClass()
Notice that the namespace isn't prefixed to the name of the class, LibraryClass. Also notice the parentheses at the end of the class, LibraryClass(). This is consistent with the constructor-calling convention used in other languages.
Alternatively, you can use the fully qualified namespace and class name to declare variables in a class library.
Dim MyVar As New ClassLibraryDemo.LibraryClass()
There are some similarities in syntax for variable declaration and object creation, and there are some revisions that reflect changes in the underlying language.
If you want to create custom components , define a class library project and add the following statement to the class definition.
Windows Control Library
The Control Library template contains a UserControl, an empty container control for creating visual components. UserControl is the original container control class carried over from Visual Basic 6.
ASP.NET Web Applications
Web applications allow you to create applications for the Web using WebForms, consistent with how you build applications for Windows.
Web applications are one of the keys to .NET. Use Web Application projects where you would have used the sluggish Visual Interdev and ASP in the past.
Web applications take care of the complexities of defining the application server, the aspx files, and the HTML. When you are defining a Web application, the design-time environment is almost identical to the environment for Windows applications. This results in a much simpler and more consistent Web application development experience.
Web Application Development Preparation
To create, test, and debug Web applications, you will need to do a little preparation. You will need Internet Information Server (IIS) installed on your workstation or on your network. This is a prerequisite to installing VS .NET if you plan to build Web applications on that PC. You will also need to be in the Debug Users or Administrators group to debug your application. (If you are stuck on the Internet settings, how to configure user permissions, or some other problem, check with your system administrator.)
Creating a Demo Web Application
In Visual Studio .NET, choose File, Project, Web Application. The location for your application will be the URL of your Web server (IIS). Provide a name for the Web application, and click OK. By default you should get a blank application with one WebForm named WebForm1.aspx. The toolbox should have a Web Forms tab. These are controls imported from System.Web.UI.WebControls namespace.
Drag and drop a text box and button onto the WebForm (shown in Figure 1.7), and change the text property of the button to GoTo. (All of this works identically to making the same modifications on a WinForm.) Double-click the button. The IDE will generate an OnClick handler and open the WebForm1.aspx.vb source code in the editor. Add the code as shown in Listing 1.9 to redirect the user to the URL typed into the TextBox.Text property.
Figure 1.7. The design-time view of a Web application in Visual Studio .NET.
Listing 1.9 Using the Response class to redirect a Web application to another URL
1: Protected Sub Button1_Click(ByVal sender As System.Object, _ 2: ByVal e As System.EventArgs) Handles Button1.Click 3: 4: Response().Redirect(TextBox1().Text) 5: 6: End Sub
Press F5 to run the application. (Place a breakpoint on line 4 if you want to step into the code when the user presses the GoTo button.) When you press F5, the application will compile into a DLL and Internet Explorer will open with the WebForm. Enter a URL into the text box and click GoTo. You should be redirected to the Web site represented by the URL text.
What I hope you gather from this demo application is that Microsoft has made significant improvements to the process of developing Web-enabled applications. By writing VB code and presenting a design-time environment almost identical to that of standard Windows applications, programmers can leverage their VB programming skills when building Web applications.
ASP.NET Web Service
If Visual Basic .NET only brought us a whole object-oriented language and Web applications built with WebForms and ASP.NET, Visual Basic .NET would be a marvel indeed. But wait, there is much more.
There has been a general march toward a microkernel operating system for many years now. A microkernel operating system theoretically is an operating system where most of the services of the system are centrally located, and there is just enough of the operating system to boot the workstation. Theoretically, this cuts down on distribution, configuration, maintenance, and possibly other kinds of costs associated with owning computers.
Web services may or may not be intentionally designed as another step toward a microkernel, but Steve Balmer, CEO of Microsoft, was recently quoted as saying that client applications will probably be browser-based. Considering Balmer's position, this statement is more akin to goal setting than prognostication.
If the client is a browser and browsers are connected to the Internet and Internet server applications, we are already moving toward a time and place where computer resources may reside anywhere in the world.
This is what Web Services do for us. Web Services are a technology based on SOAP and XML technology. The basic idea behind a Web Service application is that the Web Service can reside anywhere and be used by any client application anywhere. Using a standard protocol like XML is a hedge to increase the likelihood that any and all software tool vendors can jump on board.
The Web Service Project Template creates an empty Web Service. Although VS .NET makes programming Web Services possible and easier, there is enough here that I will refer you to the chapters related to building Web Services. Chapter 18, "Using and Implementing Web Services," introduces Web Services and provides a first-look example. Chapter 19, "ASP.NET Web Programming," discusses the technologies that Web Services depend on to work. Chapter 20, "Writing to the Event Log," covers Disco (the Web Services Discovery Tool) and how to find and use existing Web Services.
Web Control Library
The Web Control Library Project Template is the basic project for building controls that can be used on WebForms with Web Applications.
The Empty Project Template creates a solution with a single Windows Application project containing no forms, modules, or references.
Empty Web Project
The Empty Web Project Template creates an empty Web solution and project.
New Project in Existing Folder
The New Project in Existing Folder Template creates a blank Windows Application project without creating a new folder, named after the project.
Enterprise Edition Projects
There are several project template types available only in the Enterprise Edition of VS .NET. Each is covered briefly in the subsections that follow.
Create a Database Project by choosing File, New, Project, Other Projects, Database Project. The database project stores references to the database and allows you to run scripts and queries.
The Database Project allows you to manage and create queries, triggers, stored procedures, modify the database, database references, and contains a visual query designer. If you have used Access, the views in the Database project will look superficially familiar.
Visual Basic Simple Distributed Application
The Visual Basic Simple Distributed Application template creates a Windows Application project that includes empty classes for all the various aspects of distributed applications that you might include.
The basic template includes a WebForm, a WinForm, a class for managing database access, another class for managing business rules, and a few other empty classes.
You can use all these features in a distributed application, but more than likely you will use one presentation layer, perhaps a business layer, and a persistence layer. Thus you more than likely will use either WebForms or WinForms but probably not both.
We won't cover a project specifically started from Simple Distributed Application template, but we will cover all the various aspects included in a distributed application, including WebForms, WinForms, class libraries, and database programming.
Visual Basic Distributed Application
The Visual Basic Distributed Application template creates a blank project anticipating the potential that an advanced distributed enterprise system may need several applications or components in a given category.
The kinds of projects added to the project template are defined by a policy file. The purpose of the policy file is to describe the application types and components that describe the technology that will be used for a given application type and act as a technology guide.
The Analyzer Project creates a solution for managing a Visual Analyzer session and resultant profiling data. Effectively using the Visual Analyzer and profiling is an important subject but beyond the scope of this book.
Visual Studio .NET Add-In
You can select from the Visual Studio .NET Add-In or Shared Add-In. These project templates are defined under the Extensibility Projects subfolder in the New Projects dialog box.
Visual Studio .NET has added macros and an Automation Extensibility Model for customizing and extending Visual Studio itself. One of the characteristics of a first-class tool is that the tool can be used to extend itself. Visual Studio fits that bill.
Refer to Chapter 4, "Macros and Visual Studio Extensibility," for more information on add-ins.
The Shared Add-In project allows you to select from a wide variety of application hosts , including Word, Visual Studio, Visual Studio Macro IDE, Visio, Project, PowerPoint, Outlook, FrontPage, Excel, and Access.
Shared Add-Ins can be used in any of the hosts you select during the wizard creation phase.