共用方式為


Learn about projects and solutions using Visual Basic

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

In this introductory article, we'll explore what it means to create a solution and a project in Visual Studio. A solution is a container that's used to organize one or more related code projects, for example a class library project and a corresponding test project. We'll look at the properties of a project and some of the files it can contain. We'll also create a reference from one project to another.

Tip

If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

We'll construct a solution and project from scratch as an educational exercise to understand the concept of a project. In your general use of Visual Studio, you'll likely use some of the various project templates that Visual Studio offers when you create a new project.

Note

Solutions and projects aren't required to develop apps in Visual Studio. You can also just open a folder that contains code and start coding, building, and debugging. For example, if you clone a GitHub repo, it might not contain Visual Studio projects and solutions. For more information, see Develop code in Visual Studio without projects or solutions.

Solutions and projects

Despite its name, a solution is not an "answer". A solution is simply a container used by Visual Studio to organize one or more related projects. When you open a solution in Visual Studio, it automatically loads all the projects that the solution contains.

Create a solution

We'll start our exploration by creating an empty solution. After you get to know Visual Studio, you probably won't find yourself creating empty solutions often. When you create a new project, Visual Studio automatically creates a solution to house the project if there's not a solution already open.

  1. Open Visual Studio.

  2. On the menu bar, choose File > New > Project.

    The New Project dialog box opens.

  3. In the left pane, expand Other Project Types, then choose Visual Studio Solutions. In the center pane, choose the Blank Solution template. Name your solution QuickSolution, and then choose OK.

    Blank solution template in Visual Studio

    The Start Page closes, and a solution appears in Solution Explorer on the right-hand side of the Visual Studio window. You'll probably use Solution Explorer often, to browse the contents of your projects.

Add a project

Now let's add our first project to the solution. We'll start with an empty project and add the items we need to the project.

  1. From the right-click or context menu of Solution 'QuickSolution' in Solution Explorer, choose Add > New Project.

    The Add New Project dialog box opens.

  2. In the left pane, expand Visual Basic and choose Windows Desktop. Then, in the middle pane, choose the Empty Project (.NET Framework) template. Name the project QuickDate, then choose the OK button.

    A project named QuickDate appears beneath the solution in Solution Explorer. Currently it contains a single file called App.config.

    Note

    If you don't see Visual Basic in the left pane of the dialog box, you need to install the .NET desktop development Visual Studio workload. Visual Studio uses workload-based installation to only install the components you need for the type of development you do. An easy way to install a new workload is to choose the Open Visual Studio Installer link in the bottom left corner of the Add New Project dialog box. After Visual Studio Installer launches, choose the .NET desktop development workload and then the Modify button.

    Open Visual Studio Installer link

Add an item to the project

We have an empty project. Let's add a code file.

  1. From the right-click or context menu of the QuickDate project in Solution Explorer, choose Add > New Item.

    The Add New Item dialog box opens.

  2. Expand Common Items, then choose Code. In the middle pane, choose the Class item template. Name the class Calendar, and then choose the Add button.

    A file named Calendar.vb is added to the project. The .vb on the end is the file extension that's given to Visual Basic code files. The file appears in the visual project hierarchy in Solution Explorer, and its contents open in the editor.

  3. Replace the contents of the Calendar.vb file with the following code:

    Class Calendar
        Public Shared Function GetCurrentDate() As Date
            Return DateTime.Now.Date
        End Function
    End Class
    

    The Calendar class contains a single function, GetCurrentDate, that returns the current date.

  4. Open the project properties by double-clicking My Project in Solution Explorer. On the Application tab, change Application type to Class Library. This step is necessary to build the project successfully.

  5. Build the project by right-clicking on QuickDate in Solution Explorer and choosing Build. You should see a successful build message in the Output window.

Add a second project

It's common for solutions to contain more than one project, and often these projects reference each other. Some projects in a solution might be class libraries, some executable applications, and some might be unit test projects or websites.

Let's add a unit test project to our solution. This time we'll start from a project template so we don't have to add an additional code file to the project.

  1. From the right-click or context menu of Solution 'QuickSolution' in Solution Explorer, choose Add > New Project.
  1. In the left pane, expand Visual Basic and choose the Test category. In the middle pane, choose the Unit Test Project (.NET Framework) project template. Name the project QuickTest, and then choose OK.

    A second project is added to Solution Explorer, and a file named UnitTest1.vb opens in the editor.

    Visual Studio Solution Explorer with two projects

Add a project reference

We're going to use the new unit test project to test our method in the QuickDate project, so we need to add a reference to that project. The reference creates a build dependency between the two projects, meaning that when you build the solution, QuickDate is built before QuickTest.

Add test code

Project properties

(Optional) Run the test

Next steps

If you want to further explore Visual Studio, consider creating an app by following one of the Visual Basic tutorials.

See also