
Problem: Evil Type Coercion
• VB 6.0 automatically converts between various types of data.
• This was done for performance and convenience reasons.
• However, it can cause some unexpected errors that occur at runtime, rather than at compile time.
• In the following code, VB type coercion is a convenient feature. Without it, we would have to explicitly
convert the text value to an integer.
'VB 6.0 Code
Dim iNum As Integer
iNum = txtAge.Text
• This type conversion “feature” can hide real programming errors until runtime.
• Consider the following calls to a method named EvilTest():
'More VB 6.0 Code
Sub SomeMethod()
Dim sString As String, iNumber As Integer
sString = "1"
iNumber = 2
' The sub call accidentally reverses the parameters.
' Ok this time...
EvilTest sString, iNumber
' But we will get a runtime error here!
' A compile error would be preferred.
sString = "Not OK this time!"
EvilTest sString, iNumber
End Sub
Sub EvilTest(ByVal iNum As Integer, ByVal sName As String)
'Do something...
End Sub
• VB supports the Option Strict statement to force explicit type conversion.
• Any attempt to assign variables that are not the same type causes a compiler error.
• This is true, even if the values can be easily converted. Consider the following:
'VB Code
Option Strict On
Public Class TestClass
Sub SomeMethod()
Dim aString As String
Dim aNumber As Integer
aString = "1"
aNumber = 2
' Both of these calls result in a compile error.
EvilTest(aString, aNumber)
aString = "Some string value"
EvilTest(aString, aNumber)
End Sub
Sub EvilTest(num As Integer, name As String)
'Do something
End Sub
End Class
• You can (and should) enable Option Strict on a project wide level.
• To do so, double click on the My Project icon within the Solution Explorer of Visual Studio.
• Once you have done so, click on the Compile tab and enable Option Strict.
Problem: Parameters Default to Pass by Reference (ByRef)
• In VB 6.0, procedure parameters are passed by reference by default.
• By reference arguments can be modified by the called method, and the caller will see the changes.
• This is because the called method receives a reference to the data allocated elsewhere.
• This is exactly the opposite of most other languages, where the default is passing behavior is by value.
• Passing by value is safer (in terms of type safety) and faster in remote calling scenarios.
'VB 6.0 Code
Function Add(Num1 As Integer, _ 'Defaults to ByRef
ByVal Num2 As Integer) _ 'Explicitly set to ByVal
As Integer
' Now the value of the Num1 parameter has changed in the
' calling code. This is probably not intended (or expected!).
Add = Num1 + Num2
Num1 = Num2
End Function
• In VB , all procedure parameters default to pass by value (ByVal)
• By value parameters are copies of the original data.
• If you do not specify ByVal or ByRef when building a parameter list, Visual Studio automatically inserts
ByVal when you hit the Enter key.
'VB Code
Function Add(num1 As Integer, _ 'Defaults to ByVal
ByVal num2 as Integer) _ 'Explicitly set to ByVal
As Integer
Add = num1 + num2
' Now this only modifies the local copy of the parameter.
' The calling code never sees this change.
num1 = num2
End Function
Problem: Initializing Variables
• VB 6.0 does not allow you to initialize variables when they are declared.
• Not only is this an annoyance, but also in the case of object variables, VB 6.0 accepts syntax that makes it
appear as if you are instantiating the object.
• Consider the following VB6 code:
• Recall that the object is NOT instantiated on this line. Instead, it is instantiated the first time the object
variable is referenced (the next line in this case).
• During compilation, each reference to the object must be surrounded with evaluation code to check if the
object is instantiated (which hurts performance).
'VB 6.0 Code
Dim I As Integer
I = 20
Dim oMyObj As CSomeClass
Set oMyObj = New CSomeClass
Dim oMyObj2 As New CSomeClass
oMyObj2.MyProp = 5
• VB allows you to initialize variables when they are declared.
• The final two lines do exactly the same thing. Both instantiate the object immediately.
'VB Code
Dim i As Integer = 20
Dim myObj As New SomeClass()
Dim myObj2 As SomeClass = New SomeClass()
• VB also allows you to initialize arrays using a similar syntax.
• All arrays in VB are dynamic; there are no fixed sized arrays.
• Also note that the lower bound is always zero.
• We will examine array syntax in more details later in this class.
• Notice however that VB makes use of curly bracket syntax to fill an array in a single line of code.
'VB Code
'Initialize a dynamic array indexed 0-3, with the following data.
Dim ints() As Integer = {10, 20, 30, 40}
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.
VB Overloading and Constructors
Table of Contents
Courseware
Training Resources
Tutorials