Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

programmatically adding plots

I'd like to programmatically add a plot to NI's scatterGraph. I try this code, but it won't compile where the "ref" call is:

        public NationalInstruments.UI.ScatterPlot currentPlot = null;

        private void AddPlot(ref NationalInstruments.UI.IScatterGraph sg, Color c)
        {
            NationalInstruments.UI.ScatterPlot currentPlot
                    = new NationalInstruments.UI.ScatterPlot(sg.XAxes[0], sg.YAxes[0]);
            scatterGraph.Plots.Add(currentPlot);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AddPlot(ref scatterGraph, Color.LightSalmon);
        }

If I try removing the "ref", the AddPlot() function works, but "currentPlot" becomes null after that function exits. How do I make this work?
0 Kudos
Message 1 of 3
(3,615 Views)
Nevermind, I figured it out.
0 Kudos
Message 2 of 3
(3,594 Views)
Hi bmihura,

The Code wont compile because the method AddPlot() expects a reference to an object of type IScatterGraph and you are providing it with a reference to object of type ScatterGraph.  Not using 'ref' would fix this problem(and there is no requirement to use the 'ref' keyword here).
And 'currentplot' is becoming null because you have declared two 'currentplot' variables. One is the class variable and the other is the local variable in the method AddPlot(). You are changing the local variable in the AddPlot() method and the class variable 'currentplot' is never set. Do not declare another 'currentplot' variable inside the method AddPlot().
Hope this helps.
Thanks,
Mohammed Haseeb|Measurement Studio|National Instruments
0 Kudos
Message 3 of 3
(3,592 Views)