You are on page 1of 4

Lab 3: Building a Microsoft Visual Studio .

NET Component

Objectives
After completing this lab, you will be able to:
• Create a component in Microsoft Visual Studio .NET.
• Add a reference to a component in Visual Studio .NET.

Lab 3.1: Create a component in Microsoft Visual Studio .NET.

Open an existing Visual Studio .NET solution


Open the solution LabSessions that you have created during Lab 2

Creating a Class
1. Right click the LabSessions solution file , click on Add and, then on New
Project . Choose the Project Types as Visual Basic Projects and the
Templates as Class Library. Set the Location as “C:\LabSessions\” and
name it as Lab03ClassLib.

To create a new Class


1. Right-click the Lab03ClassLib project in Solution Explorer.
2. Click Add.
3. Click Add Class.
4. Set the name of the class as Test.vb.
Click on OK.

To create the Component Methods


1. In Solution Explorer, double-click the Test.vb file to open it.

Add the following code

Structure BenefitInfo
Dim strName As String
Dim strPage As String
End Structure

2. In the Test class, create a method named GetInfoList that creates an array
of Info structures, fills in the structures with the information that is shown in
the following table, and then returns the array to the calling component.
StrName strPage

Dental dental.aspx
Medical medical.aspx
Life Insurance life.aspx

Your code should look like the following:

Public Function GetInfoList() As Info()


Dim arInfo(2) As Info
arInfo(0).strName = "Dental"
arInfo(0).strPage = "dental.aspx"
arInfo(1).strName = "Medical"
arInfo(1).strPage = "medical.aspx"
arInfo(2).strName = "Life Insurance"
arInfo(2).strPage = "life.aspx"
Return arInfo
End Function

3. Save your changes.


4. Right-click Lab03ClassLib in Solution Explorer and then click Build to build
the project.
Lab 3.2 : Calling the Component

Open an existing Visual Studio .NET solution


Open the solution LabSessions that you have created during Lab 2

Add a new project file for this lab activity


1. On the File menu, point to Add New Project, and then click New Project.
2. In the New Project dialog box, on the Project Types list, click Visual Basic Projects
3. In the Templates list, click ASP.NET Web Application.
4. Set the Location to http://localhost/LabSessions/Lab-03.
5. Right-click the project Lab-03 and click on Set as StartUp Project.

Add a new ASP.NET Web Form


1. On the Project menu, click Add Web Form.
2. In the Add New Item dialog box, change the default name to Test.aspx,
and click Open.

Add a reference to the Component


1. Right-click the Lab-03 project in Solution Explorer and then click Add
Reference.
2. In the Add Reference dialog box, on the Projects tab, double-click the
Lab03ClassLib project, and then click OK.

Note: The component is added to the References folder in Solution Explorer.

Instantiate the Component


1. Open the test.aspx.vb code-behind page and write the following code in the
Page Load event to instantiate the Lab03ClassLib Component.

The code looks as follows

Dim cl As New Lab02ClassLib.Test()


Dim bi As Lab02ClassLib.Test.Info
Response.Write("<table border=1><tr><td>Benefit Name" + _
"</td><td>Web Page</td></tr>")

For Each bi In cl.GetInfoList


Response.Write("<tr><td>" & bi.strName & "</td><td>" + _
bi.strPage & "</td></tr>")
Next

Response.Write("</table>")

2. Right-click the test.aspx page in Solution Explorer and then click Build and
Browse.

A browser window opens in Visual Studio .NET that displays the test.aspx
page. test.aspx displays all of the options from the component.
Make a change in the component
1. Edit the Test.vb file in the Lab03ClassLib project.
2. Increase the size of the array by one, as shown in the following code:
Dim arBenefits(3) as BenefitInfo

3. Add another benefit to the array, as shown in the following code:


arInfo(3).strName = "Retirement Account"
arInfo(3).strPage = "retirement.aspx"
4. Build the component.

6. Refresh the browser that is displaying the test.aspx page. You should see
the new option. Notice that you did not have to rebuild the Web application,
because it automatically uses the new dynamic-link library (DLL).

You might also like