Here's a little test to explore the way that value types behave differently than reference types when using .NET.

Enjoy!


1) Passing an instance of a Class to a method ByVal.

What's printed out:

   a) 5

   b) 15

Public Class MyNumberClass
    Public Value As Integer
End Class

Module MainModule

    Sub Main()
        Sample1()
    End Sub

    Sub Sample1()
        Dim oNumber As New MyNumberClass()
        oNumber.Value = 5
        Sample1_Add(oNumber)
        Console.WriteLine(oNumber.Value)
    End Sub

    Sub Sample1_Add(ByVal num As MyNumberClass)
        num.Value = num.Value + 10
    End Sub

End Module


2) Passing an instance of a Structure to a method ByVal.

What's printed out:

   a) 5

   b) 15

Public Structure MyNumberStructure
    Public value As Integer
End Structure

Module MainModule

    Sub Main()
        Sample2()
    End Sub

    Sub Sample2()
        Dim oNumber As New MyNumberStructure()
        oNumber.value = 5
        Sample2_Add(oNumber)
        Console.WriteLine(oNumber.value)
    End Sub

    Sub Sample2_Add(ByVal num As MyNumberStructure)
        num.Value = num.Value + 10
    End Sub

End Module


3) Passing an instance of a Structure to a method ByRef.

What's printed out:

   a) 5

   b) 15

Public Structure MyNumberStructure
    Public value As Integer
End Structure

Module MainModule

    Sub Main()
        Sample3()
    End Sub

    Sub Sample3()
        Dim oNumber As New MyNumberStructure()
        oNumber.value = 5
        Sample3_Add(oNumber)
        Console.WriteLine(oNumber.value)
    End Sub

    Sub Sample3_Add(ByRef num As MyNumberStructure)
        num.value = num.value + 10
    End Sub

End Module


4) Passing an instance of a Class to a method ByVal. Method creates new instance of the class.

What's printed out:

   a) 5

   b) 15

   c) 30

Public Class MyNumberClass
    Public value As Integer
End Class

Module MainModule

    Sub Main()
        Sample4()
    End Sub

    Sub Sample4()
        Dim oNumber As New MyNumberClass()
        oNumber.Value = 5
        Sample4_Add(oNumber)
        Console.WriteLine(oNumber.Value)
    End Sub

    Sub Sample4_Add(ByVal num As MyNumberClass)
        num.Value = num.Value + 10
        Dim oNewNumber As New MyNumberClass()
        oNewNumber.Value = 30
        num = oNewNumber
    End Sub

End Module


5) Passing an instance of a Class to a method ByRef. Method creates new instance of the class.

What's printed out:

   a) 5

   b) 15

   c) 30

Public Class MyNumberClass
    Public value As Integer
End Class

Module MainModule

    Sub Main()
        Sample5()
    End Sub

    Sub Sample5()
        Dim oNumber As New MyNumberClass()
        oNumber.Value = 5
        Sample5_Add(oNumber)
        Console.WriteLine(oNumber.Value)
    End Sub

    Sub Sample5_Add(ByRef num As MyNumberClass)
        num.value = num.value + 10
        Dim oNewNumber As New MyNumberClass()
        oNewNumber.Value = 30
        num = oNewNumber
    End Sub

End Module


6) Assigning variables to instances of a Class.

What's printed out:

   a) 1

   b) 2

Public Class MyNumberClass
    Public value As Integer
End Class

Module MainModule

    Sub Main()
        Sample6()
    End Sub

    Sub Sample6()
        Dim oNumber1 As New MyNumberClass()
        Dim oNumber2 As MyNumberClass

        oNumber1.Value = 1

        oNumber2 = oNumber1
        oNumber2.Value = 2

        Console.WriteLine(oNumber1.Value)
    End Sub

End Module


7) Assigning variables to instances of a Structure.

What's printed out:

   a) 1

   b) 2

Public Structure MyNumberStructure
    Public value As Integer
End Structure

Module MainModule

    Sub Main()
        Sample7()
    End Sub

    Sub Sample7()
        Dim oNumber1 As New MyNumberStructure()
        Dim oNumber2 As MyNumberStructure

        oNumber1.value = 1

        oNumber2 = oNumber1
        oNumber2.value = 2

        Console.WriteLine(oNumber1.value)
    End Sub

End Module


8) Structures cannot have constructors or methods. (True or False)


9) Passing an instance of a class to a method ByVal makes a copy of the data. (True or False)


10) Assigning a structure variable to another structure variable makes a copy of the data. (True or False)


11) Bonus: What does the following code write out?

a) oNumClass.Value=100, oNumber1.Value=1, oNumber2.Value=2

b) oNumClass.Value=100, oNumber1.Value=2, oNumber2.Value=3

c) oNumClass.Value=200, oNumber1.Value=1, oNumber2.Value=2

d) oNumClass.Value=200, oNumber1.Value=2, oNumber2.Value=3

e) oNumClass.Value=210, oNumber1.Value=1, oNumber2.Value=2

f) oNumClass.Value=210, oNumber1.Value=2, oNumber2.Value=3

Public Class MyNumberClass
    Public Value As Integer
End Class

Public Structure MyNumberStructure
    Public Value As Integer
End Structure

Public Structure MyExtendedNumberStructure
    Public Value As Integer
    Public NumClass As MyNumberClass
End Structure

Module MainModule

    Sub Main()
        Sample11()
    End Sub

    Sub Sample11()
        Dim oNumClass As New MyNumberClass()
        oNumClass.Value = 100

        Dim oNumber1 As New MyExtendedNumberStructure()
        oNumber1.Value = 1
        oNumber1.NumClass = oNumClass

        Sample11_Add(oNumber1)

        Dim oNumber2 As MyExtendedNumberStructure
        oNumber2 = oNumber1
        oNumber2.Value = 2
        oNumber2.NumClass.Value = 200

        Sample11_Add(oNumber2)

        Console.WriteLine("oNumClass.Value={0}, oNumber1.Value={1}, oNumber2.Value={2}", oNumClass.Value, oNumber1.Value, oNumber2.Value)
    End Sub

    Sub Sample11_Add(ByVal num As MyExtendedNumberStructure)
        num.Value += 1
        num.NumClass.Value += 10
    End Sub

End Module


Scroll Down to find the answers...

 

...

 

...

 

...

 

...

 

...

 

...

 

Answers:

  1. b    (15)
  2. a    (5)
  3. b    (15)
  4. b    (15)
  5. c    (30)
  6. b    (2)
  7. a    (1)
  8. False
  9. False
  10. True
  11. e   (oNumClass.Value=210, oNumber1.Value=1, oNumber2.Value=2)