Option Strict Disallows Late Binding

P

Paul Ilacqua

The code below Fails to compile with option strict on... with off it is
fine. Where is the problem?

Function Find_In_List(ByVal alIn As ArrayList, ByVal iIndex As Integer,
ByVal sFindVal As String) As String
If alIn Is Nothing Then
Return ("")
End If
For i As Integer = 0 To alIn.Count - 1
If alIn.Item(i)(iIndex).ToString = sFindVal Then '---- Fails
Here
Find_In_List = alIn.Item(i)(0).ToString & "|" &
alIn.Item(i)(1).ToString & "|" & alIn.Item(i)(2).ToString
Exit For
End If
Next

End Function

Thanks
Paul
 
F

Family Tree Mike

Paul said:
The code below Fails to compile with option strict on... with off it is
fine. Where is the problem?

Function Find_In_List(ByVal alIn As ArrayList, ByVal iIndex As Integer,
ByVal sFindVal As String) As String
If alIn Is Nothing Then
Return ("")
End If
For i As Integer = 0 To alIn.Count - 1
If alIn.Item(i)(iIndex).ToString = sFindVal Then '---- Fails
Here
Find_In_List = alIn.Item(i)(0).ToString & "|" &
alIn.Item(i)(1).ToString & "|" & alIn.Item(i)(2).ToString
Exit For
End If
Next

End Function

Thanks
Paul

The problem is that you are using ArrayList, rather than, for example,
List (of String). ArrayList is holding Objects in your case. This is
frowned upon unless you are forced to use .Net version 1.1.
 
J

Jack Jackson

The code below Fails to compile with option strict on... with off it is
fine. Where is the problem?

Function Find_In_List(ByVal alIn As ArrayList, ByVal iIndex As Integer,
ByVal sFindVal As String) As String
If alIn Is Nothing Then
Return ("")
End If
For i As Integer = 0 To alIn.Count - 1
If alIn.Item(i)(iIndex).ToString = sFindVal Then '---- Fails
Here
Find_In_List = alIn.Item(i)(0).ToString & "|" &
alIn.Item(i)(1).ToString & "|" & alIn.Item(i)(2).ToString
Exit For
End If
Next

End Function

Thanks
Paul

It looks to me like you have two problems here.

First, alIn.Item(i) is defined as type Object, and you are indexing
that object. You need to cast alIn.Item(0) to whatever it really is.
You could use the generic List(Of ) instead to tell the compiler
explicitly what is in the list.

Second, once you get the indexed value, you convert it to a String
with ToString and then compare that with an Integer. Assuming it
really contains an Integer, you need to cast it to an Integer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top