By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
When you're developing for the .NET environment, you can expect that there will be differences from development projects for stand-alone environments. This tip, excerpted from InformIT, and a portion of a chapter of Sams Teach Yourself Object-Oriented Programming With Visual Basic In 21 Days, discusses one of those differences, the new data type called the structure.
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.
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.
Structure MyStruct Public strName As String Public strAddr As String Public blnReg As Boolean End 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.
To read the article from which this tip is excerpted, click over to InformIT. You have to register there, but the registration is free.
To get more information about Sams Teach Yourself Object-Oriented Programming With Visual Basic In 21 Days. Or to buy the book, click here.