HowDoICSharply
HowDoICSharply

Reputation: 557

Why do I have to initialize my chart outside the Form1_Load method? C#

I want to plot something in winforms in C#. I found a piece of code online to get me started that I have included below. I'm trying to understand how these winforms work, and one thing I was wondering about is the part in the Form1_Load method in which it says

chart = new Graph.Chart();

The variable chart has been initialized right in the beginning of the class, but I thought why not move it to be right above the command above. This doesn't work however, but I don't understand why.

It gives me this error, when I try to move it: "Error 1 'plotting.Form1' does not contain a definition for 'chart' and no extension method 'chart' accepting a first argument of type 'plotting.Form1' could be found (are you missing a using directive or an assembly reference?)"

using System;
using System.Drawing;
using System.Windows.Forms;
using Graph = System.Windows.Forms.DataVisualization.Charting;

namespace plotting
{
public partial class Form1 : Form
{
    Graph.Chart chart; //CAN I MOVE THIS TO (***)?
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        const int MaxX = 20;
        // Create new Graph
        //(***) DOESN*T WORK, IF I MOVE IT HERE
        chart = new Graph.Chart();
        chart.Location = new System.Drawing.Point(10, 10);
        chart.Size = new System.Drawing.Size(700, 700);
        // Add a chartarea called "draw", add axes to it and color the area black
        chart.ChartAreas.Add("draw");
        chart.ChartAreas["draw"].AxisX.Minimum = 0;
        chart.ChartAreas["draw"].AxisX.Maximum = MaxX;
        chart.ChartAreas["draw"].AxisX.Interval = 1;
        chart.ChartAreas["draw"].AxisX.MajorGrid.LineColor = Color.White;
        chart.ChartAreas["draw"].AxisX.MajorGrid.LineDashStyle = Graph.ChartDashStyle.Dash;
        chart.ChartAreas["draw"].AxisY.Minimum = -0.4;
        chart.ChartAreas["draw"].AxisY.Maximum = 1;
        chart.ChartAreas["draw"].AxisY.Interval = 0.2;
        chart.ChartAreas["draw"].AxisY.MajorGrid.LineColor = Color.White;
        chart.ChartAreas["draw"].AxisY.MajorGrid.LineDashStyle = Graph.ChartDashStyle.Dash;

        chart.ChartAreas["draw"].BackColor = Color.Black;

        // Create a new function series
        chart.Series.Add("MyFunc");
        // Set the type to line      
        chart.Series["MyFunc"].ChartType = Graph.SeriesChartType.Line;
        // Color the line of the graph light green and give it a thickness of 3
        chart.Series["MyFunc"].Color = Color.LightGreen;
        chart.Series["MyFunc"].BorderWidth = 3; 
        //This function cannot include zero, and we walk through it in steps of 0.1 to add coordinates to our series
        for (double x = 0.1; x < MaxX; x += 0.1)
        {
            chart.Series["MyFunc"].Points.AddXY(x, Math.Sin(x) / x);
        }
        chart.Series["MyFunc"].LegendText = "sin(x) / x";
        // Create a new legend called "MyLegend".
        chart.Legends.Add("MyLegend");
        chart.Legends["MyLegend"].BorderColor = Color.Tomato; // I like tomato juice!
        Controls.Add(this.chart);           
    }
}
}

Upvotes: 0

Views: 720

Answers (1)

Jiri P.
Jiri P.

Reputation: 420

The reason is that in the end of Form1_Load, you add the chart with

Controls.Add(this.chart);  

this qualifies the chart as a member of the class (MSDN), but you try to declare it in the Form1_Load method scope.

If you want to have the declaration in the method, you will have to change the last line to

Controls.Add(chart);  

Upvotes: 1

Related Questions