1. Home
  2. Computing & Technology
  3. Visual Basic

How a VB.NET Structure Is Different From a Multidimensional Array
Structure or Array? Decisions, Decisions!!

By Dan Mabbutt, About.com

A structure and a multidimensional array have a lot in common in Visual Basic. Often, you can use either one and your program will work just as well either way. But there are some clear differences and understanding them will help you understand how to manage information inside a program.

If you need more basic information about structures and arrays, you might want to try the main tutorial on the site, Visual Basic .NET 2008 Express - A "From the Ground Up" Tutorial. One of the lessons in that tutorial, Collections of Things covers arrays.

A structure looks like this in VB.NET.

Public Class Form1
   Structure Outer
      Dim Inner1 As String
      Dim Inner2 As Integer
      Dim Inner3 As Object
   End Structure
   Private Sub Whatever1()
      Dim StructInstance As Outer
      StructInstance.Inner1 = "About VB"
      StructInstance.Inner2 = 10
      StructInstance.Inner3 = Color.Red
   End Sub
End Class

Note that a structure is actually a new data type. It's almost the same thing as a class and follows a lot of the same rules. For example, a structure can't be declared inside a subroutine or function. You can read my article Modules, Structures, and Classes for more details about that. Since you have to instantiate a type before you can use it in VB.NET, that's exactly what the code example does.

A multidimensional array looks like this in VB.NET.

Public Class Form1
   Private Sub Whatever2()
      Dim MultArray(10, 20, 30) As String
      MultArray(5, 10, 15) = "About VB"
   End Sub
End Class

This doesn't look anything like a structure and it's not a new datatype. It's actually just an alternative way of declaring variables. By that, I mean that instead of using an array, you could simply declare a whole list of different variables:

Dim MultArray_0_0_0 as String
Dim MultArray_0_0_1 as String
... etc

Welllll ... In theory you could declare the same variables that way, but you certainly couldn't write the same code. One of the advantages of arrays is that you can calculate the array index:

MultArray(Count * 4, CInt(Size / 28), IQ) = "About Visual Basic"

A disadvantage of most arrays is that you have to declare them to be the same datatype. Note that the structure in the example includes three datatypes. All of the elements of the array are strings. You can combine both, however, to get the advantages of both.

Dim MultArray(10) As Outer
MultArray(1).Inner1 = "About VB"
MultArray(1).Inner3 = Color.Blue

If you need different types of data in the same array, you can also declare the array as type Object and then put anything in it. But you'll find this is a lot less efficient. (It's much like the old VB6 Variant data type.) Or, you could use a collection instead of an array.

Structures can be very powerful and multilevel ways of organizing data. For example, it's possible to have a complex nested structure like this if you need one:

Structure Outer
   Dim StartVar As String
   Structure Middle1
      Dim Inner1 As String
      Dim Inner2 As Integer
      Dim Inner3 As Object
   End Structure
   Dim MiddleVar As Object
   Structure Middle2
      Dim Inner1 As String
      Dim Inner2 As Integer
      Dim Inner3 As Object
   End Structure
   Dim EndVar As String
End Structure

Why would you ever need such a thing? Learning to use structures by coding one for complex numbers is an article that gives you an example to answer that exact question.

Another example of a specialized structure is the Enum structure of constants. The article Enum - A Building Block of Visual Basic explains how they work.

More Visual Basic Quick Tips
Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Quick Tips
  5. Visual Basic Structures Versus VB.NET Multidimensional Arrays

©2009 About.com, a part of The New York Times Company.

All rights reserved.