Value Types and Reference Types                                                        
Like any programming language, VB  defines a number of intrinsic data types. As you would expect, there are
types to represent whole numbers, strings, floating point numbers and booleans. All VB  types are based on the
Common Type System (CTS). You can also create custom types by defining classes, structures and
enumerations.

VB  types can be split into two different varieties: value and reference types. Value types derive directly from
System.ValueType, which in turn derives from System.Object. Value types include integers, characters, floating
point numbers, i.e. the intrinsic types. Reference types do not derive (directly or indirectly) from
System.ValueType.

The runtime allocates all value types on the stack. Hence when a value type goes out of scope, it is immediately
destroyed. Assignment of value types result in a full bit-by-bit copy.


The table below summarizes the differences between value and reference types:

































































You can create your own value type by defining a Structure.

Structure Point
   Public x As Integer
   Public y As Integer

   'Can overload the constructor. Note this does NOT
   'turn off the compiler-provided default ctor.
   Sub New(ByVal x As Integer, ByVal y As Integer)
      Me.x = x
      Me.y = y
   End Sub
End Structure

Here is some code consuming this structure:

Sub SomeMethod()
   'Using the default ctor, do not have to use new
   Dim p1 As Point
    
   p1.x = 1
   p1.y = 2
    
   'This also calls the default ctor.
   'NOTE: This does NOT allocate the structure on the heap!
   Dim p2 As Point = New Point()
    
   'This calls the overloaded ctor.
   Dim p3 As Point = New Point(1, 2)
End Sub   'NOTE: p1, p2, p3 all destroyed here!!!


Remember assigning two value types results in a bit wise copy of all the members.

Sub SomeMethod()
   'Use overloaded ctor for convenience only
   Dim p1 As Point = New Point(1, 1)
   Dim p2 As Point = New Point(2, 2)
    
   'Copy all of p2 into p1
   p1 = p2
    
   'Then change p1
   p1.x = 9
    
   'Display both points proving you have two distinct structures.
   Console.WriteLine("p1 = ( {0}, {1} )", p1.x, p1.y)
   Console.WriteLine("p2 = ( {0}, {1} )", p2.x, p2.y)
    
   Console.ReadLine()
End Sub










To illustrate how this differs from reference type assignment semantics, consider the following PointRef class.

'A reference type similar to the Point structure
Class PointRef
   Public x As Integer
   Public y As Integer
 
   Sub New(ByVal x As Integer, ByVal y As Integer)
      Me.x = x
      Me.y = y
   End Sub
End Class


Now we'll test reference assignment by running through the same test case we applied to the Point structure.

Sub SomeMethod()
   'Must new a reference type.
   Dim pRef1 As PointRef = New PointRef(1, 1) 'Call me Ishmael
   Dim pRef2 As PointRef = New PointRef(2, 2) 'Call me Ahab
    
   'You now have two PointRef objects in memory.
    
   'With this, you have two references pointing to 'Ahab'
   'Ishmael is orphaned and will be garbage collected eventually.
   pRef1 = pRef2
    
   'Now change 'Ahab' using pRef1
   pRef1.x = 9
    
   'Display both refs, proving they are pointing to 'Ahab'
   Console.WriteLine(" pRef1 = ( {0}, {1} )", pRef1.x, pRef1.y)
   Console.WriteLine(" pRef2 = ( {0}, {1} )", pRef2.x, pRef2.y)
    
   Console.ReadLine()
End Sub
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Value Types and Reference Types
Table of Contents
Courseware
Training Resources
Tutorials

  
  Intriguing Question

  Value Type (intrinsic data
  types, structures,
  enumerations)

  Reference Type (classes,
  interfaces, delegates, arrays,
  strings)

  Where is this type allocated?

  Allocated on the stack.

 Allocated on the managed   
 heap.

  How is a variable represented?

  Value type variables are true
  local instances.

  Reference type variables
  point to the memory
  occupied by the allocated
  instance.

  What is the ultimate base type?

   Must directly derive from
   System.ValueType.

  Can derive from any other
  class other than
  System.ValueType.

  Can this type function as a base
  to other types?

  No. Value types are always
  sealed and cannot be
  extended.

  Yes. If the type is not sealed,
  it may function as a base to
  other types.

  What is the parameter passing
   behavior?

  Variables are passed by value
  (i.e., a copy of the variable is
  passed into the called
  function).
  

  Variables are passed by
  reference (i.e., the address of
  the variable is passed into the
  called function).

  What are the construction
  semantics?

  Default ctor always zeros out
  memory. You cannot define
  your own default ctor. You
  can overload with other ctors.

  You can define multiple ctors
  or accept the default one,
  which simply zeros out the
  memory.

  Able to override
  Object.Finalize()?

  No. Value types are never
  placed onto the heap and
  therefore do not need to be
  finalized.

  Yes. But you will soon see
  why you may not want to.

  When do variables of this type
  die?

  When it falls out of scope.

   When the managed heap is
   full and needs to be garbage
   collected, or when you force
   a collection
   programmatically.

  How do I create a custom type?

  Define a Structure or   
  Enumeration.

  Define a class.

  What do they have in common?

  Can implement multiple interfaces, can contain shared (static)
  members, can contain fields, methods properties, events, and
  methods.