Using option base 0 functions on base 1 array-VBForums
Results 1 to 3 of 3

Thread: Using option base 0 functions on base 1 array

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    52

    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

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,722

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,875

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.