Home > Articles > Programming > Windows Programming

Creating Structures in Visual Basic .NET

  • Print
  • + Share This
In Visual Basic .NET, structures are similar to classes in that they associate one or more members with each other. In this article, excerpted from Sams Teach Yourself Object-Oriented Programming with Visual Basic .NET in 21 Days, Richard Simon discusses this and other issues regarding structures in Visual Basic .NET.
This article is excerpted from Sams Teach Yourself Object-Oriented Programming with Visual Basic .NET in 21 Days (Sams Publishing, 2002, ISBN 0672321491).
This chapter is from the book

Visual Basic .NET has a wide range of types defined, by default, to use in your applications. It offers the traditional types for handling numbers and strings, and other types are provided by .NET to represent objects and collections.

Understanding the types in Visual Basic .NET and how they are used is important in designing objects that best use what .NET provides. It's also important to understand that all data types used in .NET languages are common between the different .NET programming languages so that objects can be shared among them.

Creating Structures

In Visual Basic .NET, structures are similar to classes in that they associate one or more members with each other. A structure is also similar to a class in that it can contain member data, properties, methods, and events. Structures do, however, have distinct differences from classes:

  • Structures aren't inheritable.

  • Structures are implicitly derived from System.ValueType.

  • A variable of a structure type directly contains its own copy of data.

  • Structures are never terminated. The CLR doesn't call the Finalize method on a structure.

  • Structures require parameters when they have nonshared constructors. However, all structures implicitly have a public New() constructor without parameters that initializes the members to their default value.

  • Declarations of a structure's data members can't include initializers.

  • The default access for members declared with the Dim statement is public.

  • Members can't be declared Protected in a structure.

  • Equality testing must be performed with a member-by-member comparison.

  • Structures can't have abstract methods.

You declare structures with the Structure statement. In previous versions of Visual Basic, you used the Type statement to declare a structure. When a structure is declared, it is implicitly derived from System.ValueType, which gives the structure the same properties as other value types. The following shows a simple structure declaration:

Structure MyStruct
  Public strName As String
  Public strAddr As String
  Public blnReg As Boolean
End Structure

Why Use a Structure?

Structures are useful in defining new value types that encapsulate a group of variables. For example, an employee can be represented as a structure that includes all the employee's information. The advantages to using a structure rather than a class as a value type are that a structure isn't allocated on the heap, and each instance of the structure has its own copy of the data. For example, if structure A is assigned to structure B, each has its own copy of the data, and any modifications to one don't affect the other. The same example with a class would assign only a reference to B, and any modifications to either one would be reflected in the other because they share the same memory.

If you are designing a new data type that represents a new data element and doesn't need to be extended through inheritance, a structure is a better choice.

Working with Enumerations

Enumerations are types that implicitly inherit from System.Enum and represent a set of values. The underlying type of an enumeration is an integral value and can be specified as either a Byte, Short, Integer, or Long. By default, enumerations are defined as Integer.

You declare an enumeration by using the Enum keyword, followed by the enumeration name and type. If no type is specified, Integer is the default. The following shows the declaration of the enumeration Color:

Enum Color
  Red
  Green
  Blue
End Enum

Each value defined in the Color enumeration receives an integer value starting with zero—for example, Red (0), Green (1), Blue (2). If you need to define an enumeration in which the values have specific associated integer values, assign the values as follows:

Enum Color
  Red = &HFF0000
  Green = &HFF00
  Blue = &HFF
End Enum

Now each enumeration value has a specific value. Assigning values is useful in enumerations in which each value is fixed, as it is with colors. Values are fixed and can't be changed at runtime.

Using enumerations is similar to using other variables. All enumeration values must be prefaced by the containing enumeration. For example, the following statement gives MyColor the value of Red:

MyColor = Color.Red

You can avoid prefacing enumerations by importing them with the Imports statement. In this case, the following code imports the Color enumeration and then makes the same assignment as before:

Imports MyApp.Color

MyColor = Red

You can define enumerations as part of a module, class, or structure. If an enumeration is part of a class or structure, you must preface it with the class or structure as follows:

Structure tst
  Private blnIsokay As Boolean
  Enum Color
    Red = &HFF0000
    Green = &HFF00
    Blue = &HFF
  End Enum
End Structure

You can display the value of an enumeration by either its name or its integral value. The following sample code segment shows how to display both the name and value of Color.Red in a message box:

MessageBox.Show(tst.Color.Red.ToString() + " = " _
  + CType(tst.Color.Red, Integer).ToString())
  • + Share This
  • 🔖 Save To Your Account