lundi 16 mai 2016

Anyone experience with Open/LibreOffice Basic?

I've been dipping my toes in Open/LibreOffice Basic today for the first time, and I found it very frustrating. The syntax is ugly and unwieldy, and the documentation I found (at the OpenOffice wiki) is incomplete. That it balks when I accidentally write a semicolon at the end of a line is the least of my worries. I'm a competent programmer in C, C++, Java, Perl, so the kind of documentation I'm looking for is short, to the point and precise.

The concrete questions that I have right now are:
1) Are the logical operators conditional or not? I'm used to writing, e.g., an "AND" expression and not caring if the right-hand argument actually evaluates if the left-hand one is False.
2) Isn't there some way to initialize an array which I declare at the module level? I have two arrays in my program which are really constant arrays, so I'd like to fill them and declare them constant, but it's not clear to me how that can be done.
3) when I declare a Boolean variable at module level, is it guaranteed to be initialized as False? I know have one, called initDone, which tracks if the initialize function has been called which fills those arrays from the previous question.

In case you're interested, here is what I have so far:

Code:

REM  *****  BASIC  *****
Option Explicit

Private Const arrLen% = 12
Private initDone as Boolean
Private intDate(ArrLen%) as Date
Private intRate(ArrLen%) as Double
       
Sub InitArray
        If initDone Then
                Exit Sub
        End If
        Rem Array begins far enough in time for practical purposes
        intDate(0) = DateSerial(2009, 1, 1)
        intRate(0) = 9.50
        intDate(1) = DateSerial(2009, 7, 1)
        intRate(1) = 8.00
        intDate(2) = DateSerial(2011, 7, 1)
        intRate(2) = 8.25
        intDate(3) = DateSerial(2012, 1, 1)
        intRate(3) = 8.00
        intDate(4) = DateSerial(2012, 7, 1)
        intRate(4) = 8.00
        intDate(5) = DateSerial(2013, 1, 1)
        intRate(5) = 7.75
        intDate(6) = DateSerial(2013, 7, 1)
        intRate(6) = 8.5
        intDate(7) = DateSerial(2014, 1, 1)
        intRate(7) = 8.25
        intDate(8) = DateSerial(2014, 7, 1)
        intRate(8) = 8.15
        intDate(9) = DateSerial(2015, 1, 1)
        intRate(9) = 8.05
        intDate(10) = DateSerial(2016, 1, 1)
        intRate(10) = 8.05
        Rem Adding today to ease loops and calculations
        intDate(11) = Now()
        intRate(11) = intRate(10)
        initDone = True
End Sub

Function IsLeapYear( y as Integer ) as Boolean
        If y MOD 4 = 0 AND y MOD 100 <> 0 AND y MOD 400 = 0 Then
                IsLeapYear = True
        Else
                IsLeapYear = False
        End If
End Function

Function LeapYearAhead( d as Date ) as Boolean
        Dim        y as Integer
        y = Year(d)
        Rem First take care of the case we're on an actual leap date
        If Month(d) = 2 AND Day(d) = 29 Then
                LeapYearAhead = False
        Else
                Dim OneMarch = DateSerial(y, 3, 1);
                If d < OneMarch Then
                        LeapYearAhead = IsLeapYear(y)
                Else
                        LeapYearAhead = IsLeapYear(y+1)
                End If
        End If
End Function

Function LengthYearAhead( d as Date ) as Integer
        If LeapYearAhead(d) Then
                LengthYearAhead = 366
        Else
                LengthYearAhead = 365
        End If
End Function

Function BizInterest( mains as Double, mainsDate as Date, endDate as Date ) as Double
        InitArray
        Dim accMains as Double
        accMains = Mains
        Dim currInt as Double
        currInt = 0.0

        Dim begDate as Date
        begDate = DateValue(mainsDate) + 31

        Dim i% as Integer
        i% = 0
        Dim j% as Integer
        j% = 0
        While j% < arrLen% AND intDate(j%) < begDate
                j% = j% + 1
        Wend

        BizInterest = begDate - intDate(j%-1)
End Function


Don't mind the body of the main function BizInterest - I've been using it partly just to play and find out how to work with Date variables. Because, well, that's also poorly documented. The object of the function is to calculate the legal interest that is due on overdue invoices according to the Dutch legal rules, so I can incorporate this in my "Reminder" template. The two arrays are the rates over time of the Dutch legal interest rates and the dates the rates were set (always Jan 1st or July 1st).


via International Skeptics Forum http://ift.tt/1XurSuu

Aucun commentaire:

Enregistrer un commentaire