471,317 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Class data variable question

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.

Jan 7 '06 #1
3 1104
On Sat, 7 Jan 2006 14:23:48 -0500, KCT wrote:
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
Jan 7 '06 #2
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.

Jan 9 '06 #3
"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
Jan 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by Neil Zanella | last post: by
13 posts views Thread by Bryan Parkoff | last post: by
7 posts views Thread by bartek | last post: by
reply views Thread by Ed West | last post: by
70 posts views Thread by garyusenet | last post: by
20 posts views Thread by tshad | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.