"KCT" <KC@spamfree.com> wrote in message
news:qL********************@giganews.com...
The following class member function (DataClass.DataUpdate) is called by an
event handler at periodic intervals. I want to increment the counter
(Counter) each time it's called, but I don't know how to keep the variable
from being re-set to zero each time the class member function is called.
public class DataClass
{
public static void DataUpdate(Form1 frm)
{
int Counter = 0;
Counter++;
// code here to update a field on the form
}
}
I realize this is a very basic question. Any help would be greatly
appreciated.
Try this class:
namespace testcls
{
public class DataClass
{
// initializes to zero by default in the constructor
private int _counter;
public DataClass()
{
}
public int Counter
{
get { return _counter; }
set { _counter = value; }
}
public void DataUpdate()
{
// perform one iteration of update stuff
Counter++;
}
}
}
And here is a class I used to test the counter property (it is in VB):
Imports System
Imports testcls
Module Module1
Sub Main()
Dim test As New DataClass
Console.WriteLine(test.Counter.ToString)
test.DataUpdate()
Console.WriteLine(test.Counter.ToString)
End Sub
End Module
The output from this console application looks like this:
C:\dev\sln\testcon\bin>testcon
0
1
I hope that helps.
carl