Sideway
output.to from Sideway
Draft for Information Only

Content

VB.NET Structures of Data
  Structures
 Related Sections
  How to: Declare a Structure
  To declare a structure
 See also
  Structure Variables
 Access to Structure Values
 Assigning Structure Variables
 See also
  Structures and Other Programming Elements
 Structures and Arrays
 Structures and Objects
 Structures and Procedures
 Structures Within Structures
 See also
  Structures and Classes
 Similarities
 Differences
 Instances and Variables
 See also
  Source/Reference

VB.NET Structures of Data

Structures

A structure is a generalization of the user-defined type (UDT) supported by previous versions of Visual Basic. In addition to fields, structures can expose properties, methods, and events. A structure can implement one or more interfaces, and you can declare individual access levels for each field.

You can combine data items of different types to create a structure. A structure associates one or more elements with each other and with the structure itself. When you declare a structure, it becomes a composite data type, and you can declare variables of that type.

Structures are useful when you want a single variable to hold several related pieces of information. For example, you might want to keep an employee's name, telephone extension, and salary together. You could use several variables for this information, or you could define a structure and use it for a single employee variable. The advantage of the structure becomes apparent when you have many employees and therefore many instances of the variable.

Related Sections

Data Types
Introduces the Visual Basic data types and describes how to use them.

Data Types
Lists the elementary data types supplied by Visual Basic.

How to: Declare a Structure

You begin a structure declaration with the Structure Statement, and you end it with the End Structure statement. Between these two statements you must declare at least one element. The elements can be of any data type, but at least one must be either a nonshared variable or a nonshared, noncustom event.

You cannot initialize any of the structure elements in the structure declaration. When you declare a variable to be of a structure type, you assign values to the elements by accessing them through the variable.

For a discussion of the differences between structures and classes, see Structures and Classes.

For demonstration purposes, consider a situation where you want to keep track of an employee's name, telephone extension, and salary. A structure allows you to do this in a single variable.

To declare a structure

  1. Create the beginning and ending statements for the structure.

    You can specify the access level of a structure using the Public, Protected, Friend, or Private keyword, or you can let it default to Public.

    Private Structure employee  
    End Structure  
    
  2. Add elements to the body of the structure.

    A structure must have at least one element. You must declare every element and specify an access level for it. If you use the Dim Statement without any keywords, the accessibility defaults to Public.

    Private Structure employee  
        Public givenName As String  
        Public familyName As String  
        Public phoneExtension As Long  
        Private salary As Decimal  
        Public Sub giveRaise(raise As Double)  
            salary *= raise  
        End Sub  
        Public Event salaryReviewTime()  
    End Structure  
    

    The salary field in the preceding example is Private, which means it is inaccessible outside the structure, even from the containing class. However, the giveRaise procedure is Public, so it can be called from outside the structure. Similarly, you can raise the salaryReviewTime event from outside the structure.

    In addition to variables, Sub procedures, and events, you can also define constants, Function procedures, and properties in a structure. You can designate at most one property as the default property, provided it takes at least one argument. You can handle an event with a SharedSub procedure. For more information, see How to: Declare and Call a Default Property in Visual Basic.

See also

Structure Variables

Once you have created a structure, you can declare procedure-level and module-level variables as that type. For example, you can create a structure that records information about a computer system. The following example demonstrates this.

VB
Public Structure systemInfo
    Public cPU As String
    Public memory As Long
    Public purchaseDate As Date
End Structure

You can now declare variables of that type. The following declaration illustrates this.

VB
Dim mySystem, yourSystem As systemInfo

Note

In classes and modules, structures declared using the Dim Statement default to public access. If you intend a structure to be private, make sure you declare it using the Private keyword.

Access to Structure Values

To assign and retrieve values from the elements of a structure variable, you use the same syntax as you use to set and get properties on an object. You place the member access operator (.) between the structure variable name and the element name. The following example accesses elements of the variables previously declared as type systemInfo.

VB
mySystem.cPU = "486"
Dim tooOld As Boolean
If yourSystem.purchaseDate < #1/1/1992# Then tooOld = True

Assigning Structure Variables

You can also assign one variable to another if both are of the same structure type. This copies all the elements of one structure to the corresponding elements in the other. The following declaration illustrates this.

VB
yourSystem = mySystem

If a structure element is a reference type, such as a String, Object, or array, the pointer to the data is copied. In the previous example, if systemInfo had included an object variable, then the preceding example would have copied the pointer from mySystem to yourSystem, and a change to the object's data through one structure would be in effect when accessed through the other structure.

See also

Structures and Other Programming Elements

You can use structures in conjunction with arrays, objects, and procedures, as well as with each other. The interactions use the same syntax as these elements use individually.

Note

You cannot initialize any of the structure elements in the structure declaration. You can assign values only to elements of a variable that has been declared to be of a structure type.

Structures and Arrays

A structure can contain an array as one or more of its elements. The following example illustrates this.

VB
Public Structure systemInfo  
    Public cPU As String  
    Public memory As Long  
    Public diskDrives() As String  
    Public purchaseDate As Date  
End Structure   

You access the values of an array within a structure the same way you access a property on an object. The following example illustrates this.

VB
Dim mySystem As systemInfo  
ReDim mySystem.diskDrives(3)  
mySystem.diskDrives(0) = "1.44 MB"  

You can also declare an array of structures. The following example illustrates this.

VB
Dim allSystems(100) As systemInfo  

You follow the same rules to access the components of this data architecture. The following example illustrates this.

VB
ReDim allSystems(5).diskDrives(3)  
allSystems(5).CPU = "386SX"  
allSystems(5).diskDrives(2) = "100M SCSI"  

Structures and Objects

A structure can contain an object as one or more of its elements. The following example illustrates this.

VB
Protected Structure userInput  
    Public userName As String  
    Public inputForm As System.Windows.Forms.Form  
    Public userFileNumber As Integer  
End Structure  

You should use a specific object class in such a declaration, rather than Object.

Structures and Procedures

You can pass a structure as a procedure argument. The following example illustrates this.

VB
Public currentCPUName As String = "700MHz Pentium compatible"  
Public currentMemorySize As Long = 256  
Public Sub fillSystem(ByRef someSystem As systemInfo)  
    someSystem.cPU = currentCPUName  
    someSystem.memory = currentMemorySize  
    someSystem.purchaseDate = Now  
End Sub  

The preceding example passes the structure by reference, which allows the procedure to modify its elements so that the changes take effect in the calling code. If you want to protect a structure against such modification, pass it by value.

You can also return a structure from a Function procedure. The following example illustrates this.

VB
Dim allSystems(100) As systemInfo  
Function findByDate(ByVal searchDate As Date) As systemInfo  
    Dim i As Integer  
    For i = 1 To 100  
        If allSystems(i).purchaseDate = searchDate Then Return allSystems(i)  
    Next i  
   ' Process error: system with desired purchase date not found.  
End Function  

Structures Within Structures

Structures can contain other structures. The following example illustrates this.

VB
Public Structure driveInfo  
    Public type As String  
    Public size As Long  
End Structure  
Public Structure systemInfo  
    Public cPU As String  
    Public memory As Long  
    Public diskDrives() As driveInfo  
    Public purchaseDate As Date  
End Structure  
VB
Dim allSystems(100) As systemInfo  
ReDim allSystems(1).diskDrives(3)  
allSystems(1).diskDrives(0).type = "Floppy"  

You can also use this technique to encapsulate a structure defined in one module within a structure defined in a different module.

Structures can contain other structures to an arbitrary depth.

See also

Structures and Classes

Visual Basic unifies the syntax for structures and classes, with the result that both entities support most of the same features. However, there are also important differences between structures and classes.

Classes have the advantage of being reference types — passing a reference is more efficient than passing a structure variable with all its data. On the other hand, structures do not require allocation of memory on the global heap.

Because you cannot inherit from a structure, structures should be used only for objects that do not need to be extended. Use structures when the object you wish to create has a small instance size, and take into account the performance characteristics of classes versus structures.

Similarities

Structures and classes are similar in the following respects:

  • Both are container types, meaning that they contain other types as members.

  • Both have members, which can include constructors, methods, properties, fields, constants, enumerations, events, and event handlers. However, do not confuse these members with the declared elements of a structure.

  • Members of both can have individualized access levels. For example, one member can be declared Public and another Private.

  • Both can implement interfaces.

  • Both can have shared constructors, with or without parameters.

  • Both can expose a default property, provided that property takes at least one parameter.

  • Both can declare and raise events, and both can declare delegates.

Differences

Structures and classes differ in the following particulars:

  • Structures are value types; classes are reference types. A variable of a structure type contains the structure's data, rather than containing a reference to the data as a class type does.

  • Structures use stack allocation; classes use heap allocation.

  • All structure elements are Public by default; class variables and constants are Private by default, while other class members are Public by default. This behavior for class members provides compatibility with the Visual Basic 6.0 system of defaults.

  • A structure must have at least one nonshared variable or nonshared, noncustom event element; a class can be completely empty.

  • Structure elements cannot be declared as Protected; class members can.

  • A structure procedure can handle events only if it is a SharedSub procedure, and only by means of the AddHandler Statement; any class procedure can handle events, using either the Handles keyword or the AddHandler statement. For more information, see Events.

  • Structure variable declarations cannot specify initializers or initial sizes for arrays; class variable declarations can.

  • Structures implicitly inherit from the System.ValueType class and cannot inherit from any other type; classes can inherit from any class or classes other than System.ValueType.

  • Structures are not inheritable; classes are.

  • Structures are never terminated, so the common language runtime (CLR) never calls the Finalize method on any structure; classes are terminated by the garbage collector (GC), which calls Finalize on a class when it detects there are no active references remaining.

  • A structure does not require a constructor; a class does.

  • Structures can have nonshared constructors only if they take parameters; classes can have them with or without parameters.

Every structure has an implicit public constructor without parameters. This constructor initializes all the structure's data elements to their default values. You cannot redefine this behavior.

Instances and Variables

Because structures are value types, each structure variable is permanently bound to an individual structure instance. But classes are reference types, and an object variable can refer to various class instances at different times. This distinction affects your usage of structures and classes in the following ways:

  • Initialization. A structure variable implicitly includes an initialization of the elements using the structure's parameterless constructor. Therefore, Dim s As struct1 is equivalent to Dim s As struct1 = New struct1().

  • Assigning Variables. When you assign one structure variable to another, or pass a structure instance to a procedure argument, the current values of all the variable elements are copied to the new structure. When you assign one object variable to another, or pass an object variable to a procedure, only the reference pointer is copied.

  • Assigning Nothing. You can assign the value Nothing to a structure variable, but the instance continues to be associated with the variable. You can still call its methods and access its data elements, although variable elements are reinitialized by the assignment.

    In contrast, if you set an object variable to Nothing, you dissociate it from any class instance, and you cannot access any members through the variable until you assign another instance to it.

  • Multiple Instances. An object variable can have different class instances assigned to it at different times, and several object variables can refer to the same class instance at the same time. Changes you make to the values of class members affect those members when accessed through another variable pointing to the same instance.

    Structure elements, however, are isolated within their own instance. Changes to their values are not reflected in any other structure variables, even in other instances of the same Structure declaration.

  • Equality. Equality testing of two structures must be performed with an element-by-element test. Two object variables can be compared using the Equals method. Equals indicates whether the two variables point to the same instance.

See also

 

Source/Reference


©sideway

ID: 200900023 Last Updated: 23/9/2020 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 151

Software

Application 198

Digitization 25

Latex 9

Manim 121

Numeric 19

Programming

Web 283

Unicode 494

HTML 65

CSS 58

ASP.NET 174

OS 389

DeskTop 7

Python 19

Knowledge

Mathematics

Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 21

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Physics

Electric 27

Biology 1

Geography 1


Copyright © 2000-2020 Sideway . All rights reserved Disclaimers last modified on 06 September 2019