how to implement the function iif

In this category you can exchange your programming questions and solutions.
Post Reply
Angel
Posts: 87
Joined: Sat Nov 19, 2011 9:34 am

how to implement the function iif

Post: # 1237Post Angel
Tue Mar 15, 2016 3:23 pm

`Function iif(condition as string, value1 as integer,value2 as integer)

Object frmForm as Form
  Caption="Form"
  Width=640
  Height=480
  Center
   `Other objects
  Object btnButton as Button
    Tag=0
    Top=10
    left=10
    `Width=100
    `Height=30
    Caption="Button1"
    Hint="This is a button"
    ShowHint=True
    `Cursor=crHandPoint
    `Bitmap.loadFromFile("bitmap.bmp")
    OnClick=btnButton_OnClick
  End Object
  Object btnButton2 as Button
    Tag=0
    Top=btnButton.top+btnButton.height
    left=10
    `Width=100
    `Height=30
    Caption="Button1"
    Hint="This is a button"
    ShowHint=True
    `Cursor=crHandPoint
    `Bitmap.loadFromFile("bitmap.bmp")
    `OnClick=btnButton_OnClick
  End Object

  `End other objects
  `Onshow=frmForm_Onshow
  `Onclose=frmForm_Onclose
End Object
`frmForm.WindowState=WsMaximized
frmForm.Showmodal
Sub btnButton_OnClick
  dim a as integer,b as integer
  a=10
  b=5
  btnbutton2.enabled=iif("a>b",true,false)
End Sub
`===============================================
Function iif(condicion as string,valor1 as integer,valor2 as integer) as integer
  if val(condicion)=true then result=valor1 else result=valor2
End Function

Angel
Posts: 87
Joined: Sat Nov 19, 2011 9:34 am

how to implement the function iif

Post: # 1238Post Angel
Tue Mar 15, 2016 7:42 pm

unfortunately it does not work. Any help?

Sergio M.
Posts: 6
Joined: Thu Jan 28, 2016 2:36 pm

how to implement the function iif

Post: # 1239Post Sergio M.
Thu Mar 17, 2016 4:19 pm

Hi Angel
I think that in fnxbasic val("abc") always return 0
for val(string) the string must be "1234"(numeric).
regards

Sergio M.
Posts: 6
Joined: Thu Jan 28, 2016 2:36 pm

how to implement the function iif

Post: # 1240Post Sergio M.
Thu Mar 17, 2016 4:41 pm

btnbutton2.enabled=iif(a,b)
`=====================================
Function iif(valor1 as integer,valor2 as integer) as boolean
  if valor1 > valor2 Then result = True Else result = False
End Function

Angel
Posts: 87
Joined: Sat Nov 19, 2011 9:34 am

how to implement the function iif

Post: # 1241Post Angel
Fri Mar 18, 2016 4:42 am

Hello Sergio:
Thanks for your answer. Your resolution is valid only for the specific case of the condition a> b. I want a general solution. For example, btnsave.enabled = iif (tabeditor (tabindex) = 2 and rcheditormodified = true, true, false).
For any type of condition,>, <, <>, =, simple or complex.
Best regards.

Sergio M.
Posts: 6
Joined: Thu Jan 28, 2016 2:36 pm

how to implement the function iif

Post: # 1242Post Sergio M.
Fri Mar 18, 2016 1:53 pm

Sorry Angel
I don`t understand what you want to do, in your example you need to have a boolean value; you can pass to the function all the values you want, manage them inside and have the result.
otherwise maybe you want a function that return the datatype
you demand ?
In this case I don`t know if it is possible. ciao.
(excuse me for sintax you know that I don`t speak english)

Marco
Site Admin
Posts: 245
Joined: Sat Sep 15, 2018 8:41 pm

how to implement the function iif

Post: # 1243Post Marco
Sun May 08, 2016 8:33 pm

Hi, i did not tested it but you can try something like this

function iff(cond as string, val1 as integer,val2 as integer) as integer
  result=0
  select case cond
    case ">":if val1>val2 then result=1
    case "<":if val1<val2 then result=1
    case "=":if val1=val2 then result=1
    case ">=":if val1>=val2 then result=1
    case "=>":if val1>=val2 then result=1
    case "<=":if val1<=val2 then result=1
    case "=<":if val1>=val2 then result=1
  end select
end function

best regards

Angel
Posts: 87
Joined: Sat Nov 19, 2011 9:34 am

how to implement the function iif

Post: # 1244Post Angel
Mon May 16, 2016 4:31 pm

Thanks Marco.

Marco
Site Admin
Posts: 245
Joined: Sat Sep 15, 2018 8:41 pm

how to implement the function iif

Post: # 1245Post Marco
Mon May 16, 2016 8:16 pm

Hi, just see i forgot <> you can just added it to it.
It is also possible to create == or other use boolean functions to to this routine.
best regards

Angel
Posts: 87
Joined: Sat Nov 19, 2011 9:34 am

how to implement the function iif

Post: # 1246Post Angel
Wed May 18, 2016 11:12 am

Your code for IIF function works fine. Thanks Marco.
Example for two simultaneous conditions:
Object frmForm as Form
  Caption="Form"
  Width=640
  Height=480
  Center
  `Other objects
  Object btnButton1 as Button
    Tag=0
    Top=10
    left=10
    `Width=100
    `Height=30
    Caption="Button1"
    Hint="This is a button1"
    ShowHint=True
  End Object
  Object btnButton2 as Button
    Tag=0
    Top=btnButton1.top+btnButton1.height+5
    left=10
    Caption="Button2"
    Hint="This is a button2"
    ShowHint=True
    OnClick=btnButton2_OnClick
  End Object
  `End other objects
End Object
frmForm.Showmodal
`============================================================================================================================================
Sub btnButton2_OnClick
     dim a as integer, b as integer, c as integer, d as integer
     a=19:b=15
  c=19: d=18
     btnbutton1.enabled=iff(">",a,b,"=",c,d)
End Sub
`============================================================================================================================================
Function iff(cond1 as string, val1 as integer,val2 as integer,cond2 as string, val3 as integer,val4 as integer) as integer
  result=0
  select case cond1
    case "="
                 select case cond2
                       case "="
                             if val1=val2 and val3 =Val4 then result=1
                       case "<>"
                             if val1=val2 and val3 <>Val4 then result=1
                       case ">"
                             if val1=val2 and val3 >Val4 then result=1
                       case "<"
                             if val1=val2 and val3 <Val4 then result=1
                       case ">="
                             if val1=val2 and val3 >=Val4 then result=1
                       case "<="
                             if val1=val2 and val3 <=Val4 then result=1
      end select
    case "<>"
                 select case cond2
                       case "="
                             if val1<>val2 and val3 =Val4 then result=1
                       case "<>"
                             if val1<>val2 and val3 <>Val4 then result=1
                       case ">"
                             if val1<>val2 and val3 >Val4 then result=1
                       case "<"
                             if val1<>val2 and val3 <Val4 then result=1
                       case ">="
                             if val1<>val2 and val3 >=Val4 then result=1
                       case "<="
                             if val1<>val2 and val3 <=Val4 then result=1
      end select
    case ">"
                 select case cond2
                       case "="
                             if val1>val2 and val3 =Val4 then result=1
                       case "<>"
                             if val1>val2 and val3 <>Val4 then result=1
                       case ">"
                             if val1>val2 and val3 >Val4 then result=1
                       case "<"
                             if val1>val2 and val3 <Val4 then result=1
                       case ">="
                             if val1>val2 and val3 >=Val4 then result=1
                       case "<="
                             if val1>val2 and val3 <=Val4 then result=1
      end select
    case "<"
                 select case cond2
                       case "="
                             if val1<val2 and val3 =Val4 then result=1
                       case "<>"
                             if val1<val2 and val3 <>Val4 then result=1
                       case ">"
                             if val1<val2 and val3 >Val4 then result=1
                       case "<"
                             if val1<val2 and val3 <Val4 then result=1
                       case ">="
                             if val1<val2 and val3 >=Val4 then result=1
                       case "<="
                             if val1<val2 and val3 <=Val4 then result=1
      end select
    case ">="
                 select case cond2
                       case "="
                             if val1>=val2 and val3 =Val4 then result=1
                       case "<>"
                             if val1>=val2 and val3 <>Val4 then result=1
                       case ">"
                             if val1>=val2 and val3 >Val4 then result=1
                       case "<"
                             if val1>=val2 and val3 <Val4 then result=1
                       case ">="
                             if val1<val2 and val3 >=Val4 then result=1
                       case "<="
                             if val1>=val2 and val3 <=Val4 then result=1
      end select
    case "<="
                 select case cond2
                       case "="
                             if val1<=val2 and val3 =Val4 then result=1
                       case "<>"
                             if val1<=val2 and val3 <>Val4 then result=1
                       case ">"
                             if val1<=val2 and val3 >Val4 then result=1
                       case "<"
                             if val1<=val2 and val3 <Val4 then result=1
                       case ">="
                             if val1<val2 and val3 >=Val4 then result=1
                       case "<="
                             if val1<=val2 and val3 <=Val4 then result=1
                 end select
  end select
end function

Post Reply