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:

2 comments:

Vincent Vacations said...

Hey Joe,

I found your blog after doing a search for vb.net structures.

I had a quick question that's been giving me some problems.

Here is my code:

Public StructureInstance1() As upload_table


Structure upload_table
Dim uploadfile_id As Integer

End Structure

Now, I'd like to see if anything is in the structure

If there is nothing in the structure, the following gives me an error:

For Each sub_item As upload_table In StructureInstance1

Next

How do I make sure there is something in the structure before this code executes?

Thank you for any help!

Joe Kunk said...

Your structure has "uploadfile_id" which is a value type (integer). That field will always initialize to zero as a result. Below is some code that does not error that may help you get closer to what you are trying to achieve. Note that it requires the .Net framework version 2.0 due to the use of the System.Collection.Generic namespace.

Public StructureInstance1() As upload_table

Structure upload_table
Dim uploadfile_id As Integer
End Structure

Private Sub Demo()
Dim StructureInstance1 As New List(Of upload_table)
StructureInstance1.Add(New upload_table)
For Each sub_item As upload_table In StructureInstance1
Debug.Print(sub_item.uploadfile_id.ToString + vbCrLf)
Next
MessageBox.Show("Demonstration Completed")
End Sub