Gustav Brock
Gustav at cactus.dk
Thu Jun 2 07:13:28 CDT 2005
Hi SD We use this function to check that: Public Function IsReDimmed(ByRef varArrayToCheck) As Boolean ' Checks a dynamic array to see if it has been ReDim'med. ' This is to prevent errors before applying LBound() or UBound(). ' ' Returns False if varArrayToCheck is dynamic and ' never has been ReDim'med or just has been erased. ' ' 2001-08-11. Cactus Data ApS, CPH. Gustav Brock. Dim booIsReDimmed As Boolean On Error GoTo Err_IsReDimmed If IsArray(varArrayToCheck) = True Then ' Check if varArrayToCheck is ReDim'med. ' LBound() will fail if varArrayToCheck not has been ReDim'med. booIsReDimmed = LBound(varArrayToCheck) Imp True ' No error raised; varArrayToCheck is ReDim'med. Else ' varArrayToCheck is not an array. End If IsReDimmed = booIsReDimmed Exit_IsReDimmed: Exit Function Err_IsReDimmed: Select Case Err Case 9 ' Subscript (array index) out of range. ' varArrayToCheck is dynamic and not ReDim'med. Case Else ' Other error. End Select ' Exit function returning False. Resume Exit_IsReDimmed End Function /gustav >>> accessd666 at yahoo.com 06/02 1:13 pm >>> Hi group, one of our programmers came to me with a question regarding an error when the UBOUND() of an array was retrieved. The error occures when an array has been DIMmed, without specifing the size, but not REDIMmed. In the sample below I've created a simple example. When the function is called a byref array is to be filled with values. However the line: for lngCntr = 0 to UBOUND(r_alngMyArray) Will always fill since it hasn't been ReDimmed. I suggested a workaround: 1 - use the function IsInitialized() and check if the function returns -1. If so the array isnt't intitialized. However, is there an official way of handling this error/problem/occurence? Thnx Regards, SD -------------------- EXAMPLE -------------------- Main Dim alngValues() as long Call RetrieveSomething(alngValues()) End Main Public Function RetrieveSomething(byref r_alngMyArray() as Long) dim lngCntr as long for lngCntr = 0 to UBOUND(r_alngMyArray) Redim PRESERVE r_alngMyArray(lngCntr) r_alngMyArray(lngCntr) = "THIS DOES NOT WORK!" next lngCntr End Function ------------------ WORKAROUND ------------------ Function IsInitialized(byref r_alngInitialized) as Long On Error Goto error_not_initialized IsInitialized = UBOUND(r_alngInitialized) Exit Function Goto error_not_initialized IsInitialized = -1 End Function