8
Answers

Missing the point re: Arrays

John Rogers

John Rogers

15y
2.2k
1

I am trying to write some code that maintains a running sum in an array...

I think I am missing the scope rules or do not understand the way that arrays are used ...

I am an experienced C (and many other language) programmer, but am new to C#, I did a few C++ programs 10 years ago.

In short, I think the OOP aspects have me hosed up.

I am trying to maintain a buffer in PlotDataSum, which gets PlotData1 added to it on every execution of a function called from a "read" button click.

This runs fine and I see the new data plotted from an array, but when I "turn-on" summation in that array, it appears (from debugging) that the PlotDataSum array arrives empty at each execution of the buffer read function, which means I am summing with nothing...I am trying to maintain this summation buffer until I decide to clear it...

What is the procedure to declare an array that has the required scope to not dissapear after every use...

I tried adding this array to an object of a class that pulls the same data from an external I/F...and referenced it using the obj.array[i] typical notation, whcih also worked the same...seemed to accept the last addition, but did not "remember" its sum until teh next time I tried to add something again.

So far I am finding C# is so "safe", it is VERY hard to get very simple things done...

A code sample is included below:

 

namespace critter

{

/// <summary>

/// Summary description for MainForm.

/// </summary>

public class MainForm : System.Windows.Forms.Form

{

public System.Windows.Forms.Button HelpBtn;

public System.Windows.Forms.CheckBox CBReadCont;

public System.Windows.Forms.Button SendRptsBtn;

etc...

//delacered as a local variable within the MainForm

double[] PlotDataSum = new double[275];

used later  in a  function associated with reading data...

// Plot the data...

for (i = 0; i < 275; i++)

{

PlotDataSum[i] += PlotData1[i];

}

if (CBEnableSum.Checked)

{

Plot1.PlotY( PlotDataSum );

}

else

{

Plot1.PlotY(PlotData1);

}

Any help would be appreciated.  I am clearly missing soemthing VERY basic about scope rules...

 

Thanks

 

Answers (8)