Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Safe Ubound And Lbound
imacul8
post Aug 20 2006, 02:35 AM
Post #1


Member [Level 3]
******

Group: Members
Posts: 95
Joined: 21-May 06
From: Adelaide, Australia
Member No.: 24,017



Ever wanted to use LBound and UBound to get arrays boundaries without jumping over error message when the array is empty? These functions will replace the ordinary LBound and UBound procedures so you don’t need to worry about errors. I've also included a way to get the dimensions of an array. Just paste the following code into a module, and the problem is solved.

Inputs: SafeUBound and SafeLBound: [Address to the array], [What dimension you want to obtain]
ArrayDims: [Address To the array]
Returns: As expected from the ordinary functions, except that they will return -1 when the array is empty.
Assumes: You obtain the address to an array by passing it to the VarPtrArray API call. So if you want to get the boundaries of an array called aTmp, you need to call the functions like this:
lLowBound = SafeLBound(VarPtrArray(aTmp))
lHighBound = SafeUBound(VarPtrArray(aTmp))
lDimensions = ArrayDims(VarPtrArray(aTmp))
When dealing With String arrays that isn't allocated at design time, you *must* add the value 4 To the lpArray-paramenter:
lLowBound = SafeLBound(VarPtrArray(aString) + 4)

Side Effects: Since the return value is minus when the array is empty it's a big chance you will get problems with minus dimensioned arrays, but who use them anyway?

CODE
Option Explicit


Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long


Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)


Public Function SafeUBound(ByVal lpArray As Long, Optional Dimension As Long = 1) As Long
    Dim lAddress&, cElements&, lLbound&, cDims%


    If Dimension < 1 Then
        SafeUBound = -1
        Exit Function
    End If
    CopyMemory lAddress, ByVal lpArray, 4


    If lAddress = 0 Then
        ' The array isn't initilized
        SafeUBound = -1
        Exit Function
    End If
    ' Calculate the dimensions
    CopyMemory cDims, ByVal lAddress, 2
    Dimension = cDims - Dimension + 1
    ' Obtain the needed data
    CopyMemory cElements, ByVal (lAddress + 16 + ((Dimension - 1) * 8)), 4
    CopyMemory lLbound, ByVal (lAddress + 20 + ((Dimension - 1) * 8)), 4
    SafeUBound = cElements + lLbound - 1
End Function


Public Function SafeLBound(ByVal lpArray As Long, Optional Dimension As Long = 1) As Long
    Dim lAddress&, cElements&, lLbound&, cDims%


    If Dimension < 1 Then
        SafeLBound = -1
        Exit Function
    End If
    CopyMemory lAddress, ByVal lpArray, 4


    If lAddress = 0 Then
        ' The array isn't initilized
        SafeLBound = -1
        Exit Function
    End If
    ' Calculate the dimensions
    CopyMemory cDims, ByVal lAddress, 2
    Dimension = cDims - Dimension + 1
    ' Obtain the needed data
    CopyMemory lLbound, ByVal (lAddress + 20 + ((Dimension - 1) * 8)), 4
    SafeLBound = lLbound
End Function


Public Function ArrayDims(ByVal lpArray As Long) As Integer
    Dim lAddress As Long
    CopyMemory lAddress, ByVal lpArray, 4


    If lAddress = 0 Then
        ' The array isn't initilized
        ArrayDims = -1
        Exit Function
    End If
    CopyMemory ArrayDims, ByVal lAddress, 2
End Function


Hope this solves any problems u have of getting errors with empty arrays. smile.gif

Notice from BuffaloHELP:
Plagiarism is never tolerated in Trap17 forum. If you're going to copy the program code, do not leave out the copyright statement! This code is copyrighted. Source http://www.planetsourcecode.com/vb/scripts...&lngWId=-10


This post has been edited by BuffaloHELP: Aug 20 2006, 03:20 PM
Go to the top of the page
 
+Quote Post
Galahad
post Aug 21 2006, 12:44 PM
Post #2


Neurotical Squirrel
*********

Group: [HOSTED]
Posts: 585
Joined: 4-November 04
From: Novi Sad, Vojvodina
Member No.: 2,127



I was just about to write it was copied from PSC, but BuffaloHELP got here before me laugh.gif

Anyways, this code is good, since it uses Windows API, but this same code can be written in pure VB, with just a little error handling wink.gif

Here it is:
CODE

Private Function SafeLBound() As Long
On Local Error GoTo SafeLBound_Err

SafeLBound = LBound(Niz)

SafeLBound_Exit:
Exit Function

SafeLBound_Err:
If Err.Number = 9 Then SafeLBound = -1
Resume SafeLBound_Exit
End Function

Private Function SafeUBound() As Long
On Local Error GoTo SafeUBound_Err

SafeUBound = UBound(Niz)

SafeUBound_Exit:
Exit Function

SafeUBound_Err:
If Err.Number = 9 Then SafeUBound = -1
Resume SafeUBound_Exit
End Function

This code will work only with project-level, or form/module-level arrays, since building a function that checks arrays, would require a function for each data-type in Visual Basic, because useing arrays as function or procedure parameters, requires passinge them by refference (ByRef), so you can for example declare input parameter as array of variants, and pass array of strings... That would cause another run-time error smile.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. liposuction(7)
  2. Winxp-sp2 Firewall(9)
  3. Paypal.. Safe Or Not?(11)
  4. Is Paying With Paypal Safe?(35)
  5. Xoftspy! A good spyware remover & scanner(5)
  6. Is It Safe To Run A Computer Off A Non-grounded Line?(15)
  7. Plan To Upload Joomla(5)
  8. Security Not Safe(2)
  9. Safe_mode Disabled?(4)
  10. Scanning For Virus In Safe Mode(5)
  11. Java Applets(5)
  12. No Startup Response(9)
  13. Brainless Is Going To Ireland(0)
  14. How Safe Are Condoms?(24)
  15. I Need Free Php Hosting?( Suitable For Phproxy Safe Mode Off)(4)
  1. Disabling Safe Boot On Windows Xp(1)
  2. 2nd Method Of Disabling Safe Mode On Winxp(2)
  3. Over-the-counter Medications Safe For Pets(0)
  4. Cant Boot In Safe Mode [resolved](3)
  5. Sick Of Being Infected By Viruses, Spyware, Malware, Etc.?(4)
  6. Safe Mode Of Php Configuration(4)
  7. What Is The Advantage Of Safe Mode Off?(3)
  8. Are Artificial Sweeteners 'safe'?(4)
  9. Tips On Using The Internet Safely(5)
  10. Did You Know How Hard Workers Are Doing(3)
  11. Thieves Leave Note At Crime Scene: “(expletive) You And Your Safe”(7)
  12. How To Keep Your Pc Fast And Safe(3)
  13. No "safe" Music(7)


 



- Lo-Fi Version Time is now: 26th July 2008 - 09:40 AM