array of classes problem?

javalin

New member
Joined
May 20, 2009
Messages
4
Programming Experience
5-10
Hiya

I've been developing (as a hobby!) a data-analysis application for gps based race data (inc RPM, acceleration, etc, etc, etc)

So I've created a gpsData class which will contain structures (array's/classes etc) for defining laps, individual data points etc in it.

I've made another class gpsPoint which is implemented as an array (aka all data points) in the gpsData class.

It seems to work except when I put data into a gpsPoint, specifically I put gpsSpeedMS (meters/second) into it via a property it should convert into MPH & KPH and put the data into three variables in the class. However when I read it out I get a steadily increasing number. 347393, .... or similar.

I've checked that the input data is ok, and traced the code through and cannot see a problem. Help? Am I doing this the right way?

Using VB 2008 Express SP1 and .Net 3.5

Cheers all,

James
 
and traced the code through and cannot see a problem. Help? Am I doing this the right way?
No.

BTW: WHAT you did wrong is a bit hard to judge, because without seeing your actual code (at least of the property you mentioned), we need some kind of mind-reading device that seems not publically available yet ...
 
Help? Am I doing this the right way?

Second no.

As Picoflop said, we need to see your code, but my guess is that when we see it, your code would be much better if you had used List (of T) instead of arrays (although that is my current opinion without seeing the code :D )
 
Hi Javalin, welcome to the forum, we are helpful, but please send some code and a good description.
 
Thanks chaps. Excerpts of code below!

This is the gpsClass. Its the main data lump for the logging session:

VB.NET:
Public Class ClassGPS

    ' session data
    Public CaptureRateHtz As Integer
    Private msFileName As String
    Private miFileVersion As Integer

    Private mlLapCount As Long
    Private mlDataPointCnt As Long
    Private mlTotalCircuitPoints As Long

    Public Points() As dataPoint

    Public Sub New()
        ' debug
        Debug.Print("New gps Class instance!")

        ' init variables
        ReDim Points(0)
        Points(0) = New DataPoint
        mlDataPointCnt = 0
    End Sub
.....
End Class

This is the datapoint class. Each item in the array is a single point of data captured.

VB.NET:
Public Class DataPoint
    ' gps data
    Dim mdGpsSpeedMPH As Single
    Dim mdGpsSpeedKPH As Single
    Dim mdGpsSpeedMS As Single

    ' circuit
    Dim miLapNo As Integer

    ' ADC Channels
    Dim mdsysADC() As Single

    ' digital Channels
    Dim mdsysDig() As Single

    Public Sub New()
        ' overloaded New function - is called when an instance of
        ' this class is created.
        ReDim mdsysADC(6)
        ReDim mdsysDig(4)
    End Sub

    Public Property gpsSpeedMS() As Single
        Get
            Return mdGpsSpeedMS
        End Get
        Set(ByVal Value As Single)
            mdGpsSpeedMS = Value
            'Debug.Print("is " & (Value * 3600))
            mdGpsSpeedMPH = (Value * 3600) / METERS_IN_MILE
            mdGpsSpeedKPH = (Value * 3600) / METERS_IN_KILOMETER
        End Set
    End Property

.....

End Class

The load routine is basically this - a loop through the CSV file matching the lines by the record time. If the time is "new" then create a new record, otherwise just add to the existing one. If you create a new one - it wants to be a copy of the previous one, then add the data from the file to it.

VB.NET:
      While myGPSDatFile.EndOfStream = False
            ' read
            maLine = Split(myGPSDatFile.ReadLine, ",")

                    ' check for gps-file-time
                    msGPSFileTime = maLine(1)

                    If msGPSFileTime = msOldGpsFileTime Then
                        ' add to existing record
                        Call ParseLine(maLine, riSession, mlPtr)

                    Else
                        ' expand the array
                        mlPtr = gpsData(riSession).NewDataPoint

                        ' copy the point
                        If mlPtr > 1 Then
                            gpsData(riSession).Points(mlPtr) = gpsData(riSession).Points(mlPtr - 1)
                        End If

                        ' add to new record
                        Call ParseLine(maLine, riSession, mlPtr)

                        ' count points
                        mlLoadedPointsCount += 1
                    End If
....

"ParseLine" reads a line in the file, and puts the data in the correct bits in the class:

VB.NET:
                With gpsData(riSession).Points(rlPtr)
                    .gpsSpeedMS = raLine(10) / 100
                    ....
                End With


And there is the problem. When you read back the ".gpsSpeedMPH" (via a read-only property - not shown) then you get accending numbers rather than the actual data.

I can trace it though and you get the correct number going all the way through the code correctly, but just when you read it back its wrong.

Thanks for any help!

James

Edit -> updated a bit
 
Last edited:
Since the code you posted does not seem to contain an error, your error probably is somewhere else.
Maybe in the readonly property? (not, if it's just "return mdGPSSpeedKPH)
Maybe in the loop where you read and output the value?
 
>Maybe in the loop where you read and output the value?
solved it. Ta chaps.

Turns out what I thought was SpeedMS was actually Time.... Hence the continually increasing numbers...

DOH!

James
 
Back
Top