Matthias Benkmann wrote:

I have a problem with a recursive function in one of my macros. The function is supposed to either return an array of strings or Empty (in case there's not enough input data). However, under some circumstances the function mysteriously returns Object/Null instead of Variant/Empty. I'd like to know if this is a bug in OOo (I tested 1.9.122 and 1.9.125) or some misunderstanding on my part. The following is a chopped down version of my code:

Sub Main
Dim n(1) as String
n(0) = " "
n(1) = " "
Dim a as Variant

f(Array(60,10,1,12,20,10,25,30), n(), 0, 76)

End Sub

Function f(splitInfo() as Variant, daten() as String, i as Long, j as Long) as Variant
If i > UBound(daten) Then
Exit Function
End If

Dim ret() as String

for k = 0 to UBound(splitInfo)
restLen = Len(daten(i)) - j
Dim splitPos as Long
splitPos = splitInfo(k)
If splitPos <= restLen Then
j = j + splitPos
Else
st = Right(daten(i), restLen)
i = i + 1
j = 0
sta = f(Array(splitPos - restLen), daten, i, j)

If IsNull(sta) Then
MsgBox("This is impossible!")
Exit Function
End if

End If
next k

f = ret()
End Function

As you can see there are only 2 ways out of the function f(). The first one is an "Exit Function" that is called without an assignment to the function's return value. On this code path the function is supposed to return Variant/Empty. The 2nd way out of the function is after the assignment f = ret() which sets the return value to an array of string. There should be no way for the function to return Variant/Null, so the condition IsNull(sta) should never be true. But strangely it is at some point. If I call the function directly from Main with the exact same parameters as the recursive call that returns Null, I get Empty as it should be. So it seems that Null is returned only in the recursive case.

Can someone give me a clue what's going on? Is this a bug? Should I file a bug report?

Matthias

I don't suppose that you can provide an example that causes the problem... I tried to use the supplied example and there are issues. I added some error handling and found out that errors were generated and variables are not defined. This is likely to affect the results. In the following code, in which I tried to NOT change the meaning of the variables, I added some error handling that is called.

Option Explicit

Sub Main
 Dim n(1) as String
 n(0) = " "
 n(1) = " "
 Dim a as Variant

 f(Array(60,10,1,12,20,10,25,30), n(), 0, 76)
End Sub

Function f(splitInfo() As Variant, daten() As String, i As Long, j As Long) As Variant
 Dim ret() As String
 Dim k As Long
 Dim splitPos As Long
 Dim restLen As Long
 Dim st As String
 Dim sta

 On Error Goto BadError

 If i > UBound(daten()) Then
   Print "Exit at point 1"
   Exit Function
 End If

 For k = 0 To UBound(splitInfo())
   REM dataen(i) is always of length 1 in this example.
   REM j starts off as length 76, so restLen is likely to spend some
   REM time with a negative value.
   restLen = Len(daten(i)) - j
   splitPos = splitInfo(k)
   If splitPos <= restLen Then
     j = j + splitPos
   Else
     REM We generate an error when restLen = -75.
     st = Right(daten(i), restLen)
     i = i + 1
     j = 0
     sta = f(Array(splitPos - restLen), daten, i, j)

     If IsNull(sta) Then
       Print "Exit at impossible point 2"
       Exit Function
     End if
   End If
 Next k

 'Print "Exit at point 3"
 f = ret()
 Exit Function

BadError:
 Print "There was a bad error"
 On Error Goto 0
 Print "LB = " & LBound(daten()) & " UB = " & UBound(daten())
 Print "i = " & i & " restLen = " & restLen
End Function

--
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.sxw
My Macro Book: http://www.hentzenwerke.com/catalog/oome.htm
Free Info:  http://www.pitonyak.org/oo.php


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to