>Hi Cetin
>
>Today, you helped me with a great design to improve my Inlist() function. The result was as follow:
>
>
> ' VFP inlist equivalent
> ' expO1 Item where all the items will be tried to look into
> ' expO2 Param array for a list of items
> Public Function Inlist(ByVal toItem As IComparable, ByVal ParamArray toItems() As IComparable) As Boolean
> Return Array.IndexOf(toItems, toItem) <> -1
> End Function
>
>
>Tonight, I found something strange. I have the following code:
>
>
> If oApp.Inlist(loRow("Type"), 1, 3) Then
> cHtml = cHtml + "313"
> Else
> cHtml = cHtml + "405"
> End If
>
>
>The value of loRow("Type") is 3. I also show it by adding a line just before the condition and it is in fact showing 3. But, it goes in the 405 condition. If I change to this:
>
>
> If oApp.Inlist(3, 1, 3) Then
> cHtml = cHtml + "313"
> Else
> cHtml = cHtml + "405"
> End If
>
>
>It works as expected. Can you see something that could be in effect here to obtain that weird behavior when using a datarow?
>
>If I do this:
>
>
> If oApp.Inlist(CType(loRow("Type"), System.Int32), 1, 3) Then
> cHtml = cHtml + "313"
> Else
> cHtml = cHtml + "405"
> End If
>
>
>It works as expected. But, I don't want to be forced to make that conversion everytime I want to verify for numeric values in Inlist(). The field Type is of Numeric 1 from a VFP table.
Hi Michel,
It looks weird at first but numeric 1 (System.Decimal) is not equal to integer 1. Silly computers:)
If you had 1.0,3.0 in your list it would do. I tried to write a generic version of InList in VB < vbg >
Private Function InList(ByVal searchFor As Object, ByVal ParamArray paramList() As Object) As Boolean
Try
Dim local As Object = Convert.ChangeType(searchFor, paramList(0).GetType())
Return (Array.IndexOf(paramList, local) <> -1)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return False
End Function
This is not "generic" as I wrote yesterday (I mean it is not utilizing System.Collections.Generic with strong type support).
Second what I don't really like is that it uses ParamArray with object type. Objects might be from different types and no check for that. ie: Consider these calls:
1) If InList(loRow("Type"), 1,3,DateTime.Now,"2.00") Then
2) If InList(loRow("Type"), "1.00",3,DateTime.Now,"2.00") Then
3) If InList(loRow("Type"), DateTime.Now, 1, 3) Then
and let loRow("Type") was decimal 1,2,3 or 5.
1) 1 and 3 would be in list.
2) 1 and 2 would be in list
3) InvalidCastException
IOW first parameter in ParamArray is assumed to be the same for all array elements. It still covers VFP Inlist IMHO (with one exception - it lets you compare a decimal to a string as in case# 2).
Anyway I couldn't do something more elegant in VB:)
Cetin