-
Mar 1st, 2015, 10:41 AM
#1
Thread Starter
Member
Using option base 0 functions on base 1 array
Hello,
I have a library of functions in a module designed under Option base 0
My project is basically base 1 . What would be the way to use an array base 1 bay calling a function designed under base 0, without tweaking the function?
Thanks
-
Mar 1st, 2015, 11:40 AM
#2
Re: Using option base 0 functions on base 1 array
Are you saying you want to pass a 1-based array to a function that expects 0-based? And you don't want to a) change your array and b) change the function receiving the array?
If either answer to the last two questions are "No", then do that: either change your code to use 0-based or tweak the receiving function to not hard-code 0 as the LBound, but use the LBound property during calculations, i.e.,
Code:
For x = LBound(passedArray) To UBound(passedArray)
vs For x = 0 to UBound(passedArray)
Another option is the use of APIs, where you can temporarily change the LBound before passing it to your function
Code:
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ByRef Ptr() As Any) As Long
' sample code; not applicable for string arrays
Dim srcArray(1 To 20) As Byte
Dim aPtr As Long
CopyMemory aPtr, ByVal VarPtrArray(srcArray), 4&
CopyMemory ByVal aPtr + 20, 0&, 4& ' change to 0-bound
Call libTest(srcArray) ' pass array to library function
CopyMemory ByVal aPtr + 20, 1&, 4& ' change back to 1-bound
The above code has limitations as coded. It does not handle source arrays that are passed by reference and does not handle string arrays nor does it handle multi-dimensional arrays. Consider it a last resort
Edited: Is your library compiled and do the library functions assume 0-bound or do they check for 0-bound? If compiled & assumption vs validation, then what does the function declaration look like for one of them that accepts arrays? This could be a simple solution where you tweak the function parameter in the Declare statement and change the way you call the function. Overall, very minor change & more robust
Last edited by LaVolpe; Mar 1st, 2015 at 11:56 AM.
-
Mar 1st, 2015, 01:21 PM
#3
Re: Using option base 0 functions on base 1 array
Or a longer term solution: stop using arrays based anywhere but 0 except in special cases. Usually they are just far more trouble than they are worth in the long run.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|