option strict and late binding

learning

Active member
Joined
Aug 31, 2009
Messages
28
Location
Michigan
Programming Experience
5-10
Is it good practice to program with option strict on? And, if so, can you tell me how to avoid late binding errors in situations like this?

VB.NET:
        Public Function Subtract(ByVal arr1 As Array, ByVal arr2 As Array) As Array ' arr2 - arr1
            Dim ul As Integer
            Dim ll As Integer
            Dim lpc As Integer
            Dim tempArr As Array
            ul = arr1.GetUpperBound(0)
            ll = arr1.GetLowerBound(0)
            If ul = arr2.GetUpperBound(0) And ll = arr2.GetLowerBound(0) Then
                tempArr = Array.CreateInstance(GetType(Single), ul + 1)
                For lpc = ll To ul
                    tempArr(lpc) = arr2(lpc) - arr1(lpc)
                Next
                Return tempArr
            Else
                tempArr = Array.CreateInstance(GetType(Single), 1)
                Return tempArr
                MsgBox("Error - Array sizes are different")
            End If
        End Function

The portion of the code that gets the late binding error is here and I'm wondering how to avoid it.

VB.NET:
                For lpc = ll To ul
                    tempArr(lpc) = arr2(lpc) - arr1(lpc)
                Next
 
Yes, it is good practice to always have Option Strict On except in the rare cases that you specifically need it. Even then, it should be On at the project level and Off only in the files that need it. Even then, you should use partial classes to include only the code that specifically need Option Strict Off in the files where it's Off and the rest of the code gets compiled with Option Strict On.

In your case, the issue appears to be that you are using the Array type. My question is why? If your array is to contain Single values then why aren't you creating a Single array? This:
Dim tempArr As Array

'...

tempArr = Array.CreateInstance(GetType(Single), 1)
Should be this:
Dim tempArr As Single()

'...

tempArr = New Single(0) {}
Your parameters and return type should be Single() rather than Array as well, assuming that they are indeed Single arrays. If they're not, how can you assume that you can subtract elements and the result be able to go into a Single array?
 
Thank you. I will be leaving Option Strict On from now on and will proceed to clean up some things. As always, I appreciate the help.
 

Latest posts

Back
Top