Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Interface and Graphics (http://www.xtremevbtalk.com/-net-interface-and-graphics/)
-   -   Using Chart control to plot data points (http://www.xtremevbtalk.com/-net-interface-and-graphics/327599-using-chart-control-plot-data.html)

BigHairy 05-04-2015 11:50 AM

Using Chart control to plot data points
I am new to programming in general and I am trying to create a small application in VB 2013 to graph (and eventually perform a linear regression on) data points supplied by the user via textbox controls. I can get the graph to populate the first time, but after my "Graph Points" button is clicked a second time I keep getting errors. I have tried several different things and the error is either that 'object is not set to an instance of an object' or that my series already exists. Code as it currently stands (which is returning the object instance error) is below. Any suggestions?

Public Module GlobalVariables
    Public Series As String

End Module
Public Class Form1

    Private Sub Chart1_Click(sender As Object, e As EventArgs) Handles CalibrationChart.Click

    End Sub

    Private Sub buttonGraphPoints_Click(sender As Object, e As EventArgs) Handles buttonGraphPoints.Click
        Dim SeriesName As String = TextBox1.Text
        Dim keyvaluepairXYValues As List(Of KeyValuePair(Of Integer, Integer)) = New List(Of KeyValuePair(Of Integer, Integer))
        Dim ElementIndex As Integer = 0

        Do Until ElementIndex = 5 Or Me.Controls.Find("textVolts" + CStr(ElementIndex), True).Length = 0 _
            Or Me.Controls.Find("textFlow" + CStr(ElementIndex), True).Length = 0 Or Me.Controls.Find("textVolts" + CStr(ElementIndex), True)(0).Text = "" _
            Or Me.Controls.Find("textFlow" + CStr(ElementIndex), True)(0).Text = ""

            keyvaluepairXYValues.Add(New KeyValuePair(Of Integer, Integer) _
                          (CInt(Me.Controls.Find("textVolts" + CStr(ElementIndex), True)(0).Text), _
                          CInt(Me.Controls.Find("textFlow" + CStr(ElementIndex), True)(0).Text)))

            ElementIndex += 1
        ElementIndex = 0
        If CalibrationChart.Series.IndexOf(SeriesName) = -1 Then
        End If

        CalibrationChart.Series(SeriesName).ChartType = DataVisualization.Charting.SeriesChartType.Point

        For Each pair As KeyValuePair(Of Integer, Integer) In keyvaluepairXYValues
            'Dim key As Integer = pair.Key
            'Dim value As Integer = pair.Value

            CalibrationChart.Series(SeriesName).Points.AddXY(pair.Key, pair.Value)

            MsgBox("x" & ElementIndex & " = " & pair.Key _
              & vbNewLine & "y" & ElementIndex & " = " & pair.Value)
            ElementIndex += 1

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

hDC_0 05-06-2015 05:06 PM

Some debugging "hints" for someone new to programming..
Thanks, passel, for adding the proper formatting..

Just a guess...but I'm not feeling comfortable with this line inside the buttonGraphPoints_Click event:

In general, I don't dispose of things until the app's main form is ready to close down (using the Form Closing Event).

Be careful, though, on relying on the form closing event when using an old .Net framework version, per this page:

Prior to the .NET Framework 2.0, the Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application.
If you have validation code in either of these events that must be executed,
you should call the Form.Close method for each open form individually
before calling the Exit method.

Otherwise if you try to re-reference anything (after it's disposed/gone) while the program is still running, then it may yield an object not found error..

Another troubleshooting hint:
The "Private Sub buttonGraphPoints_Click" only has two parameters "sender" and "e".
Neither of these is actually used in the code contained within the sub.
So break out the Do loop and For Next loop into individual subs called from "Private Sub buttonGraphPoints_Click".
This may make it easier to trace what's going on.

Other than that it's hard to debug your code just from a snippet because other controls are involved,
(which means that other things may be happening outside this code snippet, effecting how it functions).

Speaking of hard to debug - global scoped variables (1, 2, 3).


Originally Posted by BigHairy
Public Module GlobalVariables
Public Series As String
End Module

Wikipedia has this to say:

They are usually considered bad practice precisely because of their non-locality:
a global variable can potentially be modified from anywhere
(unless they reside in protected memory or are otherwise rendered read-only),
and any part of the program may depend on it.
A global variable therefore has an unlimited potential for creating mutual dependencies,
and adding mutual dependencies increases complexity. See action at a distance.
Global variables also make it difficult to integrate modules because software written by others
may use the same global names unless names are reserved by agreement, or by naming convention.

In this StackOverFlow thread, they are a little more generous, citing Steve McConnell, the author of Code Complete.

Section 13.3 of the Code Complete book, though, talks about using access routines (page 340 onward).

Good (recommended) reading.
If you feel you have to use global variables, then it's good to know the risks and how to take precautions.

On a final side note..


Originally Posted by BigHairy
I am trying to create a small application in VB 2013 to graph (and eventually perform a linear regression on) data points supplied by the user

You do know that about Forecasting Formula (Chart Controls), which has, as it's first parameter, "RegressionType", that can be "Linear"?
This StackOverFlow thread has some relevant info also.

BigHairy 05-08-2015 08:28 AM

This entire response was hugely helpful. I have some more reading and work to do before I can totally debug and complete this project, but thank you for all the helpful links and insights!

All times are GMT -6. The time now is 01:40 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.