Question Why are rows removed from this datatable?

geerizzle

Member
Joined
Oct 14, 2013
Messages
5
Programming Experience
1-3
I'm writing a program which retrieves historical stock price data from yahoo as a csv. On my form load (for testing for now) then I retrieve all data for a stock as a CSV file & convert to a datatable.

When passed to the mainChart.createChart sub a function is called which takes this datatable and removes rows based on date range, then returns a datatable which is used as a a chart datasource. Works Great!

Problem comes when redefining the date range set by my form buttons. Somehow the dtxInitial datatable has had the same rows removed as for the dtxActive one? I don't see how! So if I increase the range there are rows missing & it fails.

Any ideas why the below isn't working?

(I know I could load the whole datatable into the Chart and handle ranges with the axis min/max, but with a total of 28000 data points + other indicators it is pretty slow.)

Thanks :encouragement:

VB.NET:
Class mainChart


    Private dtxInitial As DataTable


    Public Sub createChart(f As DateTime, t As DateTime, dtxNew As DataTable, firstUse As Boolean)  'MAIN CHART 
        If firstUse = True Then dtxInitial = dtxNew


        With Main.chartPrice
            .DataSource = returnDTRange(f, t)
        End With


    End Sub

    Public Sub updateChart(f As DateTime, t As DateTime)
        createChart(f, t, dtxInitial, False)
    End Sub


    Private Function returnDTRange(f As DateTime, t As DateTime) As DataTable

        msgBox(dtxInitial.Rows.Count) ' First instance = 7000 ROWS, but then returns lower values
        Dim dtxActive As DataTable
        dtxActive = dtxInitial        'I suspect I should do this different somehow?
        Dim trow as integer


        For r = 0 To dtxActive.Rows.Count - 1
                If dtxActive.Rows(r)("Date") < t Then tRow = r : Exit For
        Next

        For i = 1 To tRow
            dtxActive.Rows.RemoveAt(0)
        Next


        For r = 0 To dtxActive.Rows.Count - 1
            If dtxActive.Rows(r)("Date") < f Then fRow = r : Exit For
        Next
        For i = fRow To (dtxActive.Rows.Count - fRow) + fRow - 1
            dtxActive.Rows.RemoveAt(fRow)
        Next


        Return dtxActive
    End Function



End Class


Public Class Main
Dim xChart As New mainChart


'Then in my form on load it triggers:

Private Sub doAll()  'THIS WORKS FINE
        Dim fDate = DateTime.Now.AddYears(-5)
            Dim tDate = DateTime.Now
            Dim dtNew As DataTable = yahoo2.callStocks.getHistorical("AAPL", 2012)
            xChart.createChart(fDate, tDate, dtNew, True)
End Sub

'Buttons to set chart time range
 Private Sub butC2Y_Click(sender As Object, e As EventArgs) Handles butC2Y.Click
        xChart.updateChart(DateTime.Now.AddYears(-2), DateTime.Now)
 End Sub

Private Sub butC3Y_Click(sender As Object, e As EventArgs) Handles butC3Y.Click
        xChart.updateChart(DateTime.Now.AddYears(-3), DateTime.Now)
End Sub





End Class
 
Last edited:
Hi and welcome to the Forum,

You are getting this issue since a DataTable is a Reference Type and not a Value Type. What this basically means is that when you say:-

VB.NET:
Dim dtxActive As DataTable
dtxActive = dtxInitial        'I suspect I should do this different somehow?

You are saying, create a variable called dtxActive and POINT that variable to dtxInitial. By then interacting with the dtxActive object you are actually making any physical changes in the dtxInitial object.

To get round this you just need to create a copy of your initial table. i.e:-

VB.NET:
Dim otherDT As DataTable = initialDT.Copy

Hope that helps.

Cheers,

Ian
 
Back
Top