Call function when data is already available AND when data should be retrieved

daviddoria

Member
Joined
Jan 11, 2007
Messages
13
Programming Experience
Beginner
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:

VB.NET:
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
 
yes yes! Ah I didn't think to do it in the data designer (doh!) I was trying to do it with code. But that is exactly what I needed - I added a couple of different flil methods and now I'm back in business!

Thanks for all the help/suggestions/comments!!
 
I tend to avoid editing the original fill because it has an impact on the other generated queries and may render your app unable to edit the db. I'd always make a default FIll with no params, and then add X number of other FillByX with varying parameters

I have a tendency to stay away from the table adapters and create my own subs connected to stored procedures for filling my typed datasets but that is good advice and I'll remember for the future.
 
Back
Top