VB6 isIn function


Author: Dave
Date: 02.27.18 - 10:09pm



So I have been coding up a bunch of these variant helper functions that operate generically on a bunch of different variable types for my dzrt library.

Here is an isIn function that allows you to search strings, arrays, and collections for the needle you are looking for. It can optionally return the index of that item in an out variable. For strings index is string position. For arrays and collections it is the element index.

collections also support object arguments. I think this will be handy alternative to having to search arrays and collections manually everytime.

I will add this to my globals.cls which is a set of functions automatically added to the global namespace and accessible as a new language function as soon as you add a reference to the dzrt dll.

Another example of this type of function is a readfile function which can load a file into either a byte array or string based on the variable type you pass it. MD5 functions which can automatically work on files, strings and byte arrays etc. Handy shortcuts all and really making the most of the vb variant datatype.

Private Sub Form_Load()
    
    a = Array(1, 2, 3, 4, 5)
    Set b = New Collection
    b.Add 1
    b.Add 2
    b.Add 3
    
    Dim index
    
    Debug.Print "Number variant array test: " & isIn(4, a, index)
    Debug.Print index

    Debug.Print "Number collection test: " & isIn(3, b, index)
    Debug.Print index

    Debug.Print "String test: " & isIn("test", "this is my test string!", index)
    Debug.Print index
    
    Set b = New Collection
    b.Add New Form1
    b.Add Me
    
    Debug.Print "Object collection test: " & isIn(Me, b, index)
    Debug.Print index
    
    Debug.Print "double test: " & isIn(3.14, Array(7.15, 8, 3.14), index)
    Debug.Print index
    
End Sub


Function isIn(needle, haystack, Optional ByRef index) As Boolean Dim t As String, j As String, x Dim i As Long On Error GoTo hell 'string or variant are treated as strings and index = instr 'arrays will walk all elements, index will be found array index, search as above 'collections as above index col index, also supports object types.. 'other datatypes index = -1 t = TypeName(haystack) If t = "String" Or t = "Variant" Then index = InStr(1, haystack, needle, vbTextCompare) If index > 0 Then isIn = True ElseIf InStr(t, "()") > 0 Then 'its an array For i = LBound(haystack) To UBound(haystack) j = TypeName(haystack(i)) If j = "String" Or t = "Variant" Then index = InStr(1, haystack(i), needle, vbTextCompare) If index > 0 Then isIn = True index = i Exit For End If Else If needle = haystack(i) Then isIn = True index = i Exit For End If End If Next ElseIf t = "Collection" Then i = 0 For Each x In haystack i = i + 1 j = TypeName(x) If IsObject(needle) And IsObject(x) Then If ObjPtr(needle) = ObjPtr(x) Then isIn = True index = i Exit For End If ElseIf j = "String" Or t = "Variant" Then index = InStr(1, haystack(i), needle, vbTextCompare) If index > 0 Then isIn = True index = i Exit For End If Else If needle = x Then isIn = True index = i Exit For End If End If Next Else If needle = haystack Then isIn = True index = 0 End If End If Exit Function hell: isIn = False index = -2 End Function





Comments: (0)

 
Leave Comment:
Name:
Email: (not shown)
Message: (Required)
Math Question: 24 + 93 = ? followed by the letter: Q 



About Me
More Blogs
Main Site
Posts: (All)
2020 ( 2 )
2019 ( 5 )
2018 (6)
     VB6 UDTs
     VB6 Debugger View As Hex tooltips
     VB6 - C Share registry data
     VB6 Addin Missing Menus
     VB6 Class Init Params
     VB6 isIn function
2017 (6)
     Python and VB6
     Python pros and cons
     download web Dir
     vc rand in python
     VB6 Language Enhancement
     Register .NET as COM
2016 ( 22 )
2015 ( 16 )
2014 ( 25 )
2013 ( 4 )
2012 ( 10 )
2011 ( 7 )
2010 ( 11 )
2009 ( 3 )