The Structure of an Object
|
|
|
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.
|
Practical
Learning: Introducing Classes
|
|
- Start Microsoft Visual Web Developer or Microsoft Visual Studio
- To create a web site, on the main menu, click File -> New Web
Site... or New -> Web Site...
- Set the Language to Visual Basic
Set the name to DepartmentStore1
Imagine you want to represent a triangle as a
geometric object:
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:
- 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...
- 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
- 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.
Practical
Learning: Introducing Class Members
|
|
- To create a new structure, on the main menu, click Website -> Add New Item...
- Click Class
-
Set the Name to StoreItem
- Click Add
- Read the message box and click Yes
- 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
|
- Save the file
Creating an Object Based on a
Structure
|
|
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>
Practical
Learning: Creating an Object
|
|
- In the Solution Explorer, right-click Default.aspx and click Rename
- Type index.aspx and press Enter twice to display the file
- 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>
|
- 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>
No comments:
Post a Comment