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:
Email: (not shown)
Message: (Required)
Math Question: 88 + 33 = ? followed by the letter: K 

About Me
More Blogs
Main Site
Posts: (year)
2023 (3)
     VB6 IDE Enhancements
     No Sleep
     A2W no ATL
2022 (4)
     More VB6 - C data passing
     Vb6 Asm listing
     Byte Array C to VB6
     Planet Source Code DVDs
2021 (2)
     Obscure VB
     VB6 IDE SP6
2020 (5)
     BSTR from C Dll to VB
     Cpp Memory Manipulation
     ActiveX Binary Compatability
     C libs for VB6
2019 (5)
     Console tricks
     FireFox temp dir
     OCX License
     Extract substring
     VB6 Console Apps
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)
     VB6 CDECL
     UDT Tricks pt2
     Remote Data Extraction
     Collection Extender
     VB6 FindResource
     DirList Single Click
     Reset CheckPoint VPN Policy
     VB6 BSTR Oddities Explained
     SafeArrays in C
     BSTR and Variant in C++
     Property let optional args
     Misc Libs
     Enum Named Pipes
     Vb6 Collection in C++
     VB6 Overloaded Methods
     VB6 Syncronous Socket
     Simple IPC
     VB6 Auto Resize Form Elements
     Mach3 Automation
     Exit For in While
2015 (16)
     C# self register ocx
     VB6 Class Method Pointers
     JS Debugger
     Duktape Debug Protocol
     QtScript 4 VB
     Vb6 Named Args
     vb6 Addin Part 2
     VB6 Addin vrs Toolbars
     OpenFile Dialog MultiSelect
     Duktape Example
     DukTape JS
     VB6 Unsigned
     .Net version
     TitleBar Height
     .NET again
     VB6 Self Register OCXs
2014 (25)
     Query Last 12 Mos
     Progid from Interface ID
     VB6 to C Array Examples
     Human Readable Variant Type
     ScriptBasic COM Integration
     CodeView Addin
     ScriptBasic - Part 2
     Script Env
     MSCOMCTL Win7 Error
     printf override
     History Combo
     Disable IE
     API Hooking in VB6
     Addin Hook Events
     FastBuild Addin
     VB6 MemoryWindow
     Link C Obj Files into VB6
     Vb6 Standard Dlls
     CStr for Pascal
     Lazarus Review
     asprintf for VS
     VB6 GlobalMultiUse
     Scintilla in VB6
     Dynamic Highlight
     WinVerifyTrust, CryptMsgGetParam VB6
2013 (4)
     MS GLEE Graphing
     printf for VB6
     C# App Config
     Tero DES C# Test
2012 (10)
     VC 2008 Bit Fields
     Speed trap
     C# Db Class Generator
     VB6 vrs .NET (again)
     FireFox Whois Extension
     git and vb6
     Code Additions
     Compiled date to string
     C# ListView Sorter
     VB6 Wish List
2011 (7)
     C# Process Injection
     CAPTCHA Bots
     C# PE Offset Calculator
     VB6 Async Download
     Show Desktop
     coding philosophy
     Code release
2010 (11)
     Dll Not Found in IDE
     Advanced MSScript Control
     random tip
     Clipart / Vector Art
     VB6 Callback from C#
     Binary data from VB6 to C#
     CSharp and MsScriptControl
     HexDumper functions
     Js Beautify From VB6 or C#
     vb6 FormPos
     Inline Asm w VB6
2009 (3)
     The .NET Fiasco
     One rub on computers
     Universal extractor