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: 70 + 2 = ? followed by the letter: L 



About Me
More Blogs
Main Site
Posts:
Console tricks
FireFox temp dir
OCX License
Extract substring
VB6 Console Apps
VB6 UDTs
VB6 Debugger View As Hex tooltips
VB6 - C Share registry data
VB6 Addin Missing Menus
VB6 Class Init Params
VB6 isIn function
Python and VB6
Python pros and cons
download web Dir
vc rand in python
VB6 Language Enhancement
Register .NET as COM
VB6 CDECL
UDT Tricks pt2
Remote Data Extraction
Collection Extender
VB6 FindResource
CDO.Message
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
EXPORT FUNCDNAME Warning
VB6 Syncronous Socket
Simple IPC
VB6 Auto Resize Form Elements
Mach3 Automation
Exit For in While
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
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
MS GLEE Graphing
printf for VB6
C# App Config
Tero DES C# Test
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
C# Process Injection
CAPTCHA Bots
C# PE Offset Calculator
VB6 Async Download
Show Desktop
coding philosophy
Code release
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
The .NET Fiasco
One rub on computers
Universal extractor