Class data variable question

K

KCT

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.
 
S

Someone

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.

Declare counter outside the function as static variable
 
B

Bruce Wood

You have two choices, depending upon the effect you want to achieve.

1. Declare the variable as a private field in your class:

public class DataClass
{
private int _counter = 0;

public static void DataUpdate(Form1 frm)
{
this._counter++;
// code here to update a field on the form
}
}

this will give you a different counter for each instance of DataClass
that you instantiate. However, this probably isn't what you want.

2. You probably want one counter for your whole program, in which case
follow "Someone"'s advice:


public class DataClass
{
private static int _counter = 0;

public static void DataUpdate(Form1 frm)
{
DataClass._counter++;
// code here to update a field on the form
}
}

This will give you one global counter for all instances of the class
DataClass within one run of your program.
 
V

Vagabond Software

KCT said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top