Saturday, February 03, 2007

Basics.Net: Structures

The fundamental data type in .Net is a value type. In the .Net 2.0 framework, these include SByte, Byte, Int16, Int32, UInt32, Int64, Single, Double, Decimal, Char, Boolean, IntPtr, and DateTime. These are the types that developers initially become familiar with when learning the language. See more on C# value types, VB.Net value types


Each of these logically represent a single value, are a fixed size in memory of 16 bytes or less, are kept on the high-performance stack, and the assignment of the value to another variable makes another copy of the data.


A very useful extension of the value type is the known as a Structure (VB.Net) or struct (C#). These are essentially custom classes that, like all classes, can have constructors (the new operator), public/private properties, and public/private methods. The private variables within the structure must consist solely of value types and not exceed 16 bytes total in size.


Like primitive value types, structures logically represent a single value, are a fixed size in memory of 16 bytes or less, are kept on the high-performance stack, and the assignment of the value to another variable makes another copy of the data.


Structures allow you to create high-performance single-value custom data types with custom behaviors and characteristics enforced by its methods and properties. Structures can be combined in a [reference type] custom class to create a rich application object.


For example, an Employee class might contain a structure for Age that enforces a minimum value of zero and maximum value of 100, a structure for zip code that enforces a valid U.S. zip code, a structure for email that enforces a properly formatted email address, etc. You may wish to create a library of common structures to be used by multiple applications.


Finally, below is an source code example of a simple structure, a more complex structure, and and example of using them both. The source code is presented in both C# and VB.Net.




Click here to view C# Struct Code:


Click here to view the C# Code to use the struct:


Click here to view the VB.Net Structure Code:


Click here to view theVB.Net Code to use the structure:

Back to Basics

As Vice-President of the user group GLUGnet (Greater Lansing User Group for .Net), I work with the other Board members to help bring in guest speakers each month that explore the latest technologies. Focusing on what is new and interesting is appropriate for a monthly 90-minute presentation, and we will continue to do so.

That said, many of our members are newer to the .Net platform and would appreciate and benefit from more introductory material to help them successfully traverse what is clearly a substantial learning curve. The curse of a rich framework and a sophisticated development environment is that there is a lot to learn!

To address this need, I will start posting here a series of "Basics.Net" blog posts that will explain core concepts, with examples in both VB.Net and C#. These posts will be "bite-sized" in that each will cover only a single concept and can be reviewed and committed to memory in 15 minutes or less. Whenever possible, the significance of the concept will be included, making it easier to mentally organize the concept within the greater .Net concept space. I am confident that you will find these posts to be a fresh and useful treatment of the concepts.

I am happy to offer this service to our GLUGnet members or any other member of the .Net community. I am happy to accept suggestions for topics that are of interest, so please let me know of any topics or concepts that you would like to have covered.

Joe Kunk