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:
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.
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>
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:
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> |
|
|||||||||||||||||||||||||||||||||||||||||||||
|
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>
|
|
|||||||||
|