InternetUnicodeHTMLCSSScalable Vector Graphics (SVG)Extensible Markup Language (xml) ASP.Net TOCASP.NetMiscellaneous Feature ASP.NET Scripting Visual Basic .NET TOCVB .NET Language Referencena VB.Net Keywords Draft for Information Only
Content
VB.NET Structures of Data
VB.NET Structures of DataStructuresA 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
Data Types How to: Declare a StructureYou 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
See also
Structure VariablesOnce 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. VBPublic 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. VBDim 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 ValuesTo 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. VBmySystem.cPU = "486" Dim tooOld As Boolean If yourSystem.purchaseDate < #1/1/1992# Then tooOld = True Assigning Structure VariablesYou 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. VByourSystem = 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 ElementsYou 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 ArraysA structure can contain an array as one or more of its elements. The following example illustrates this. VBPublic 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. VBDim 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. VBDim allSystems(100) As systemInfo You follow the same rules to access the components of this data architecture. The following example illustrates this. VBReDim allSystems(5).diskDrives(3) allSystems(5).CPU = "386SX" allSystems(5).diskDrives(2) = "100M SCSI" Structures and ObjectsA structure can contain an object as one or more of its elements. The following example illustrates this. VBProtected 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 ProceduresYou can pass a structure as a procedure argument. The following example illustrates this. VBPublic 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. VBDim 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 StructuresStructures can contain other structures. The following example illustrates this. VBPublic 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 StructureVB 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 ClassesVisual 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. SimilaritiesStructures and classes are similar in the following respects:
DifferencesStructures and classes differ in the following particulars:
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 VariablesBecause 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:
See also
Source/Reference
©sideway ID: 200900023 Last Updated: 9/23/2020 Revision: 0 Ref: ![]() References
![]() Latest Updated Links
![]() ![]() ![]() ![]() ![]() |
![]() Home 5 Business Management HBR 3 Information Recreation Hobbies 8 Culture Chinese 1097 English 339 Travel 18 Reference 79 Computer Hardware 254 Software Application 213 Digitization 37 Latex 52 Manim 205 KB 1 Numeric 19 Programming Web 289 Unicode 504 HTML 66 CSS 65 SVG 46 ASP.NET 270 OS 431 DeskTop 7 Python 72 Knowledge Mathematics Formulas 8 Set 1 Logic 1 Algebra 84 Number Theory 206 Trigonometry 31 Geometry 34 Calculus 67 Engineering Tables 8 Mechanical Rigid Bodies Statics 92 Dynamics 37 Fluid 5 Control Acoustics 19 Natural Sciences Matter 1 Electric 27 Biology 1 |
Copyright © 2000-2025 Sideway . All rights reserved Disclaimers last modified on 06 September 2019