Jeff,
There's no difference between the three examples you gave, but the third
example can lead to more efficient code in the right situations.... Let me
explain:
Say you have an object which takes a long time to create an instance of
(MyReallySlowObject). If you are not 100% sure you are going to use this
object, then you can save this time by only creating it when it is needed.
For example:
Test 1: Wastes time by creating object needlessly if filename is empty
~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Function Test1(Filename as String) as Boolean
Dim objTest as new MyReallySlowObject
' Validate Filename
if Filename.Length = 0 then
' Empty Filename not allowed
return false
end if
' If we get here, do stuff
return objTest.DoStuff(Filename)
End Function
Test 2: Saves time when filename is empty
~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Function Test1(Filename as String) as Boolean
Dim objTest as MyReallySlowObject
' Validate Filename
if Filename.Length = 0 then
' Empty Filename not allowed
return false
end if
' If we get here, do stuff
objTest = New MyReallySlowObject
return objTest.DoStuff(Filename)
End Function
HTH,
Trev.