Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
IIf oddity

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
GTBecker



Joined: 18 Jan 2006
Posts: 610
Location: Cape Coral

Posted: 01 October 2016, 2:59 AM    Post subject: IIf oddity Reply with quote

With compiler v4.2.3 I find this odd behavior of IIf, extracted from Steve's DS1307functions code:
Code:

private strMonths as StringVectorData({
   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" })

public Function MonthName(byval n as byte) as String
    MonthName = iif((n>0 and n<13), strMonths(n), "?")
end function

sub main()
    dim i as byte
    for i= 0 to 14
        debug.print (n>0 and n<13); " ";
        debug.print MonthName(i)
    next
end sub

which yields:
?
Jan
?
Mar
?
May
?
Jul
?
Sep
?
Nov
?
?

If the (n>0 and n<13) test is executed outside of IIf, however, the behavior is correct:
Code:

private strMonths as StringVectorData({
   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" })

public Function MonthName(byval n as byte) as String
    dim tf as boolean
    tf = (n>0 and n<13)
    MonthName = iif(tf, strMonths(n), "?")
end function

sub main()
    dim i as byte
    for i= 0 to 14
        debug.print MonthName(i)
    next
end sub

yielding:
?
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
?
?

as expected.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2982
Location: Portland, OR

Posted: 01 October 2016, 17:52 PM    Post subject: Re: IIf oddity Reply with quote

GTBecker wrote:
With compiler v4.2.3 I find this odd behavior of IIf [...]
The issue has been confirmed and a correction is being developed. In the interim, one can get the correct behavior by introducing an intermediate variable thus:
Code:
Public Function MonthName(ByVal n as Byte) as String
    Dim idxOK as Boolean = ((n>0) and (n<13))
    MonthName = IIF(idxOK, strMonths(n), "?")
End Function


Another alternative is:
Code:
Public Function MonthName(ByVal n as Byte) as String
    MonthName = IIF(Not(n<1) and (n<13), strMonths(n), "?")
End Function
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2017 Elba Corp. All rights reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group