Home

Introduction to Structures

 

The Structure of an Object

 

Introduction

An object is anything (house, person, car, street, lipstick, book, shoe, etc) that can be described using predefined characteristics. This means that, before describing an object, you must define the characteristics that would be used (to describe the object). This also means that an object follows a predefined structure.

HomePractical Learning: Introducing Classes

  1. Start Microsoft Visual Web Developer or Microsoft Visual Studio
  2. To create a web site, on the main menu, click File -> New Web Site... or New -> Web Site...
  3. Set the Language to Visual Basic
    Set the name to DepartmentStore1

Creating a Structure

Imagine you want to represent a triangle as a geometric object:

Triangle

You can declare each of its various parts as a variable. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Length As Double
    Dim Height As Double

    Length = 24.55 : Height = 20.75

    Response.Write("=-= Triangle Characteristics =-=")
    Response.Write("<br>Base:   " & Length)
    Response.Write("<br>Height: " & Height)
%>

</body>
</html>

If you want to treat the whole triangle as one object, you can create a structure for it. To create a structure, you can use the Structure keyword followed by a name. The name of a structure follows the rules we have applied so far for variable names. Here is an example:

Structure Triangle

As a name that represents a group of items, a structure has a body that would be used to define the items that compose it. The body of a structure starts after its name. 

To indicate the end of the structure, type End Structure. Therefore, a structure can be created as follows:

<script language="vb" type="text/vb" runat="server">

Structure Triangle
    
End Structure

</script>

Since a structure is a combination of other variables, you declare each variable inside of the body of the structure. Each item that composes the structure is represented as a complete variable declared with a name and a data type. By adding a Base and a Height variables, our structure would become: 

<script language="vb" type="text/vb" runat="server">

Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure

</script>

The items that compose a structure are called members of the structure. When creating the members of a structure, you can omit the Dim keyword:

<script language="vb" type="text/vb" runat="server">

Structure Triangle
    Base As Double
    Height As Double
End Structure

</script>

After typing the code of the structure, you must save it.

To create a structure in Microsoft Visual Web Developer or Microsoft Visual Studio:

  1. On the main menu, you can click Website -> Add New Item... Alternatively, in the Solution Explorer, you can right-click the name of the project and click Add New Item...
  2. In the Templates section of the Add New Item dialog box, set the name to the name of the structure you want to create and add the .vb extension
  3. Click Add

You will be asked to add the file in a folder named App_Code, which you should accept. If the folder doesn't exist yet, it would be created.

HomePractical Learning: Introducing Class Members

Imagine you want to write a (console-based) program for a department store and the customer has given you a preliminary catalog as follows:

Crinkled georgette dress with empire waist, sequined corsage. Raw-edge pieced appliques on skirt. Buttoned keyhole back; 23" from waist. Rayon; lined. Dry clean. Imported. New Wool Comfort Pants Pencil Skirt
Stock #: 437876
Crinkled Georgette Dress
Unit: $42.95
Stock #: 790475
New Wool Comfort Pants
$38.75
Stock #: 740797
Pencil Skirt
$25.75
Silver 1/10-ct. T.W. Diamond Heart-Link Bracelet Multistriped Organic Cotton Dress Shirt Modest Dress Heels
Stock: 608432
Silver 1/10-ct. T.W. Diamond Heart-Link Bracelet
$95.85
Stock #: 759470
Multistriped Organic Cotton Dress Shirt
$35.50
Stock #: 487046
Modest Dress Heels
$35.50

Each item in this catalog is represented by its Stock number, its name or description, and its price. Based on this, you can create a class that represents each item.

  1. To create a new structure, on the main menu, click Website -> Add New Item...
  2. Click Class
  3. Set the Name to StoreItem
  4. Click Add
  5. Read the message box and click Yes
  6. Change the document as follows:
     
    Imports Microsoft.VisualBasic
    
    Public Structure StoreItem
        Dim ItemNumber As Long
        Dim ItemName As String
        Dim UnitPrice As Double
    End Structure
  7. Save the file

Creating an Object Based on a Structure

 

Introduction

After creating a structure, to use it in a program, you can declare it as a variable. To do this, start with the Dim keyword, followed by a name for the variable, followed by As, and the name of the structure. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle
%>

</body>
</html>

HomePractical Learning: Creating an Object

  1. In the Solution Explorer, right-click Default.aspx and click Rename
  2. Type index.aspx and press Enter twice to display the file
  3. Chance it as follows:
     
    <%@ Page Language="VB"
             AutoEventWireup="false" 
             CodeFile="index.aspx.vb" 
             Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Department Store</title>
    </head>
    <body>
    
    <%
        Dim SampleItem As StoreItem
    %>
    
    </body>
    </html>
  4. Save the file

Accessing the Members of an Object

After creating an object, to access the member of a structure, type the name of the variable, followed by a period, followed by the name of the member you want. The member you are trying to use must be part of the structure. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle

    Tri.Base
%>

</body>
</html>

If you are using either Microsoft Visual Web Developer or Microsoft Visual Studio, after typing the period, the list of members would appear. If you know the name of the member, you can start typing it:

  • Once the desired member is highlighted, press the Space bar or Tab
  • If you see the name of the member in the list, you can double-click click it
  • If the list doesn't appear, press Ctrl + Space bar

If you don't want to use the list displayed by the Code Editor, press Esc. Once you have specified what member you want to use, you can assign it the desired value. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle

    Tri.Base = 24.55
%>

</body>
</html>

In the same way, you can access all the desired members of a structure and initialize them to complete the variable. By default, each member variable must be initialized on its own line. Here are examples:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle

    Tri.Base = 24.55
    Tri.Height = 20.75
%>

</body>
</html>

You may remember that you can use the colon operator to write more than one statement on the same line. In the same way, you can use it to initialize many member variables on the same line as long as you separate them with an empty space, a colon, and another empty. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle

    Tri.Base = 24.55 : Tri.Height = 20.75
%>

</body>
</html>

As done so far, after declaring the variable and initializing its member variables, you can use the object as you see fit. For example, you can display the values of the object by accessing each one of its member member variables. Here are examples:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle

    Tri.Base = 24.55
    Tri.Height = 20.75

    Response.Write("=-= Triangle Characteristics =-=")
    Response.Write("<br>Base:   " & Tri.Base)
    Response.Write("<br>Height: " & Tri.Height)
%>

</body>
</html>

You can also request the values of the members of a class from the user. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle

    Tri.Base = 44.62
    Tri.Height = 32.85

    Response.Write("=-= Triangle Characteristics =-=")
    Response.Write("<br>Base:   " & Tri.Base)
    Response.Write("<br>Height: " & Tri.Height)
%>

</body>
</html>

You can also involve the members of a structure in any operation you see fit, even if the other operands are locally declared variables or constant values as in the following code:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle
    Dim Area As Double

    Tri.Base = 28.44
    Tri.Height = 55.10
    
    Area = Tri.Base * Tri.Height / 2

    Response.Write("=-= Triangle Characteristics =-=")
    Response.Write("<br>Base:   " & Tri.Base)
    Response.Write("<br>Height: " & Tri.Height)
    Response.Write("<br>Area:   " & Area)
%>

</body>
</html>
 
 
 
 
 

Practical LearningPractical Learning: Using an Object

  1. To use a DepartmentStore object, change the Program.vb file as follows:
     
    <%@ Page Language="VB"
             AutoEventWireup="false" 
             CodeFile="index.aspx.vb" 
             Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Department Store</title>
    </head>
    <body>
    
    <%
        Dim SampleItem As StoreItem
        
        SampleItem.ItemNumber = 437876
        SampleItem.ItemName = "Crinkled Georgette Dress"
        SampleItem.UnitPrice = 42.95
    
        Response.Write("=-= Department Store =-=")
        Response.Write("<br>Item #: " & SampleItem.ItemNumber)
        Response.Write("<br>Item Name: " & SampleItem.ItemName)
        Response.Write("<br>Unit Price: " & SampleItem.UnitPrice)
    %>
    
    </body>
    </html>
  2. To execute the application, press Ctrl + F5. This would produce:
     
    Department Store
  3. Return to your programming environment

With a Structure's Instance

After declaring a variable of a structure, you can initialize it and access any of its members as you see fit. To initialize an object, we saw that you can access any or each of its member variables and assign each the appropriate value. Here are examples:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle

    Tri.Base = 24.55
    Tri.Height = 20.75

    Response.Write("=-= Triangle Characteristics =-=")
    Response.Write("<br>Base:   " & Tri.Base)
    Response.Write("<br>Height: " & Tri.Height)
%>

</body>
</html>

As an alternative, start a section using the With keyword followed by the name of the variable. Create a new line and type End With. The section between the With Variable line and the End With line is the body of the With statement. In that body, to access a member variable, type the period operator followed by the desired member. You can then initialize the member variable. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Structure Triangle
    Dim Base As Double
    Dim Height As Double
End Structure
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim tri As Triangle
    Dim Area As Double

    With
        .Base = InputBox("Enter Triangle Base:")
        .Height = InputBox("Enter Triangle Height:")
    
        Area = .Base * .Height / 2

        Response.Write("=-= Triangle Characteristics =-=")
        Response.Write("<br>Base:   " & .Base)
        Response.Write("<br>Height: " & .Height)
        Response.Write("<br>Area:   " & Area)
    End With
%>

</body>
</html>

Practical LearningPractical Learning: Using an Object With...

  1. To use the With operator, change the code as follows:
     
    <%@ Page Language="VB"
             AutoEventWireup="false" 
             CodeFile="index.aspx.vb" 
             Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Department Store</title>
    </head>
    <body>
    
    <%
        Dim SampleItem As StoreItem
        
        With SampleItem
            .ItemNumber = 437876
            .ItemName = "Crinkled Georgette Dress"
            .UnitPrice = 42.95
        End With
    
        Response.Write("=-= Department Store =-=")
        Response.Write("<br>Item #: " & SampleItem.ItemNumber)
        Response.Write("<br>Item Name: " & SampleItem.ItemName)
        Response.Write("<br>Unit Price: " & SampleItem.UnitPrice)
    %>
    
    </body>
    </html>
  2. Save the file 
  3. Get to the browser and refresh
  4. Return to your programming environment
 
 
   
 

Home Copyright © 2009-2013 FunctionX, Inc. Next