Bug 149151

Summary: Error in the Basic IIf function.
Product: LibreOffice Reporter: Vladimir Sokolinskiy <sokol>
Component: BASICAssignee: Not Assigned <libreoffice-bugs>
Status: NEW ---    
Severity: normal CC: aron.budea, edil, mikekaganski
Priority: medium    
Version: 6.0.0.3 release   
Hardware: All   
OS: All   
Whiteboard:
Crash report or crash signature: Regression By:
Bug Depends on:    
Bug Blocks: 127592    

Description Vladimir Sokolinskiy 2022-05-18 10:05:33 UTC
1.Run the next Basic script.

Sub Test1
  Dim i As Long, arr
  arr=IIf(i=0, Array(), Array(0))
End Sub

An error occurs that must not occur.

2.Run the next Basic script.

Sub Test2
  Dim i As Long, arr
  arr=IIf(i=1, Array(), Array(0))
  Msgbox IsArray(arr)
End Sub

Returns False, must be True.
Comment 1 Rafael Lima 2022-05-18 18:09:17 UTC
I can reproduce the error in LO 7.3. I also tried the following variation:

Sub TestA
    Dim i as Long, arr, a, b
    i = 0
    a = Array()
    b = Array(0)
    arr = Iif(i = 0, a, b)
End Sub

Which returned the error "Inadmissible value or data type. Index out of defined range." This does not seem to make sense since we're not trying to access internal data of the array.

This issue has something to do with the Array object. The following code runs without issues:

Sub TestB
    Dim i as Long, arr, a, b
    i = 0
    a = 3
    b = 4
    arr = Iif(i = 0, a, b)
End Sub

System info:
Version: 7.3.3.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 12; OS: Linux 5.13; UI render: default; VCL: kf5 (cairo+xcb)
Locale: pt-BR (pt_BR.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.3~rc2-0ubuntu0.21.10.1~lo1
Calc: threaded
Comment 2 Aron Budea 2022-07-10 01:47:46 UTC
Already in 6.0.0.3.
Comment 3 edil 2024-01-25 20:56:59 UTC
It seems that IIF evaluate both values before give a return value.
In PitonYack pag 77 : "The IIf (“Immediate If”) function returns one of two values based on a conditional expression.". May be array is not a valid value.
Workaround :
Sub TestA
    Dim i as Long, arr, a, b
    i = 0
    a = Array()
    b = Array(0)
    arr = b : If i = 0 Then arr = a
    ' arr = Iif(i = 0, a, b)
End Sub
Comment 4 Jean-Pierre Sanchez 2024-02-14 17:11:01 UTC
The following line raises an error
  arr = Iif(True, Array("A","B"), Array("B","A")) ' Raises an error

The following line does not work properly: arr = "A"
arr = Iif(False, Array("A","B"), Array("B","A")) ' 

IIF exposes the same issue when with functions returning an Array

Sub Main
	arr = Iif(true, getArray1(), getArray2())	
End Sub

Function getArray1() As Variant
	arr  = Array("A","B")
	getArray1 = arr
End Function

Function getArray2() As Variant
	arr  = Array("B","A")
	getArray2 = arr
End Function