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
{
///
{
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

Jan MontanoRank #1,295 · 1.3K reputationPosted Jan 12, 2009, 4:54 PM EST
It will be not out of scope in anyway just as long as your MainForm is alive. Its values will not reset in anyway unless specified.
John Rogers16 reputationPosted Jan 12, 2009, 6:01 PM EST
Jan,
For some reason my "sum" gets zeroed before each use...so I just see that latest data added in... not the summation. I am not closing the MainForm, so I don't understand how it's being reset. I am thinkning about declaring another public array class with the array insisde and using/ referencing it that way...unless you or other readers have any other ideas.
John
Jan MontanoRank #1,295 · 1.3K reputationPosted Jan 12, 2009, 6:46 PM EST
John Rogers16 reputationPosted Jan 12, 2009, 7:01 PM EST
Jan MontanoRank #1,295 · 1.3K reputationPosted Jan 12, 2009, 7:27 PM EST
JAN DOT MONTANO AT GMAIL DOT COM
John Rogers16 reputationPosted Jan 13, 2009, 8:16 AM EST
I sent a zipped set of the files..
Thanks
JPR
John Rogers16 reputationPosted Jan 13, 2009, 1:21 PM EST
well...this is pretty embarrassing...I found a call that has been zeroing out the sum...so I have it fixed.
So no mystery, just a MISTAKE...
Please just delete the code I sent...and don't waste your time.
Thanks
JPR
Jan MontanoRank #1,295 · 1.3K reputationPosted Jan 13, 2009, 3:05 PM EST
Good for you, you found it. :)