«^»
5.9. Structure types

In C#, structure types are called structs. Chapter 11 of the 'C# Language Specification' says ‘Structs are particularly useful for small data structures that have value semantics. Complex numbers, points in a coordinate system, or key-value pairs in a dictionary are good examples of structs.’ So, if we want to represent points in 2D space in a program, it would probably be more appropriate to use a structure type rather than a class type.

A declaration of a structure type looks similar to that of a class type:

0087: Public Structure Point
0088:     Private iX As Integer
0089:     Private iY As Integer
0090:     Public Sub New(ByVal pX As Integer, ByVal pY As Integer)
0091:         iX = pX
0092:         iY = pY
0093:     End Sub
0094:     Public Property X() As Integer
0095:         Get
0096:             Return iX
0097:         End Get
0098:         Set(ByVal Value As Integer)
0099:             iX = Value
0100:         End Set
0101:     End Property
0102:     Public Function Distance() As Double
0103:         Return Math.Sqrt(iX * iX + iY * iY)
0104:     End Function
0105:     Public Overrides Function ToString() As String
0106:         Return iX & ":" & iY
0107:     End Function
0108: End Structure

Suppose we alter Module1 so that instead of it using the class called Point it uses this structure called Point. This will significantly affect the meaning of some of the statements.

For example, a value of a structure type is stored in the variable:

0111:         Dim tPoint As Point = New Point(100, 200)

And an assignment statement copies the value:

0117:         Dim tAnotherPoint As Point = tPoint