daviddoria 334 Posting Virtuoso Featured Poster

I have two functions: GetDayTime and GetLunchTime. From some places in the program, all I have is the associate name and a date and I want to determine these quantities. So I just query the database inside the function:

Public Function CalculateLunchTime(ByVal DateToCalculate As DateTime) As Double

        Dim daEvent As New TTDataSetTableAdapters.PunchEventTableTableAdapter
        Dim EventTable As New TTDataSet.PunchEventTableDataTable
        daEvent.Fill(EventTable)

        Dim SelectString As String = "associate='" & Name & "' and timefield>=#" & DateToCalculate & "# and timefield <#" & DateToCalculate.AddDays(1) & "# and reason='LUNCH'"
        Dim SortString As String = "timeField"
        
'Return the calculated time

    End Function

However, I then also have a "GenerateReport" function which loops through all of the associates, and a big range of dates and calls GetDayTime and GetLunchTime for all of these. An you can imagine, this is quite expensive! (ie VERY slow). Clearly I would want to query the database once to get all the information and then just pass arrays of the information to these two functions. Is it that simple - I just have to make a second version of these functions that takes arrays of the data as input? It just seems like a bad idea to me because of the "code reuse" idea - if a year later I have to make a change, I have to make the same change in two places, which always seems like a bad idea.

Any suggestions/advice?

Thanks,

Dave